# shellcheck shell=bash
# ==========================================================================
#             ____        _     _   _      _    ____      _
#            / ___| _   _| |__ | \ | | ___| |_ / ___|__ _| | ___
#            \___ \| | | | '_ \|  \| |/ _ \ __| |   / _` | |/ __|
#             ___) | |_| | |_) | |\  |  __/ |_| |__| (_| | | (__
#            |____/ \__,_|_.__/|_| \_|\___|\__|\____\__,_|_|\___|
#
#                    ---  IPv4/IPv6 Subnet Calculator  ---
#                   https://www.nntb.no/~dreibh/subnetcalc/
# ==========================================================================
#
# SubNetCalc - IPv4/IPv6 Subnet Calculator
# Copyright (C) 2024-2025 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: thomas.dreibholz@gmail.com


# ###### Bash completion for subnetcalc #####################################
_subnetcalc()
{
   # Based on: https://www.benningtons.net/index.php/bash-completion/
   local cur prev words cword
   if type -t _comp_initialize >/dev/null; then
      _comp_initialize || return
   elif type -t _init_completion >/dev/null; then
      _init_completion || return
   else
      # Manual initialization for older bash completion versions:
      COMPREPLY=()
      cur="${COMP_WORDS[COMP_CWORD]}"
      # shellcheck disable=SC2034
      prev="${COMP_WORDS[COMP_CWORD-1]}"
      # shellcheck disable=SC2034,SC2124
      words="${COMP_WORDS[@]}"
      # shellcheck disable=SC2034
      cword="${COMP_CWORD}"
   fi

   # ====== Parameters ======================================================
   if [ "${cword}" -le 1 ] ; then
      # Suggest IP addresses of local machine:
      local addresses
      if [ -x  /usr/sbin/ip ] ; then
         addresses="$( /usr/sbin/ip addr show | awk '/[ ]+inet/ { print $2 }')"
      elif [ -x /sbin/ifconfig ] ; then
         local addressesV4
         local addressesV6
         addressesV4="$(/sbin/ifconfig | awk '/[ \t]+inet / { a=$4 ; print $2 "/" sprintf("%d.%d.%d.%d", "0x" substr(a, 3, 2), "0x" substr(a, 5, 2), "0x" substr(a, 7, 2), "0x" substr(a, 9, 2)) }')"
         addressesV6="$(/sbin/ifconfig | awk '/[ \t]+inet6 / { print $2 "/" $4 }' | sed -e 's#%.*/#/#')"
         addresses="${addressesV4} ${addressesV6}"
      fi
      # shellcheck disable=SC2207
      COMPREPLY=( $(compgen -W "${addresses}" -- "${cur}") )
      return
   fi


   # ====== All options =====================================================
   local opts="
-n
-nocolor
-nocolour
-uniquelocal
-uniquelocalhq
-v
-version
"
   # shellcheck disable=SC2207
   COMPREPLY=( $( compgen -W "${opts}" -- "${cur}" ) )

   return 0
}

complete -F _subnetcalc subnetcalc
