# Nanoprobe Shell Completion
#
# MIT License
#
# Copyright (C) 2025 by Thomas Dreibholz <dreibh@simula.no>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# ###### Bash completion for nanoprobe ##########################################
_nanoprobe()
{
   # 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

   case "${prev}" in
      #  ====== Generic value ===============================================
      --busypoll   | -b | \
      --count      | -n | \
      --delay      | -d | \
      --port       | -p | \
      --timeout    | -t | \
      --dummy-pkt  | -x | \
      --ping-size  | -s | \
      --pong-size  | -o | \
      --pong-every | -y)
         return
         ;;

      # ====== Special case: interface name =================================
      --interface | -i)
         local interfaces
         interfaces=$(ip link show | grep '^[0-9]' | cut -d' ' -f2 | tr -d ':')
         # shellcheck disable=SC2207
         COMPREPLY=( $(compgen -W "${interfaces}" -- "${cur}") )
         return
         ;;

      # ====== Special case: log file =======================================
      --log | -l)
         # Files with extension .log:
         _filedir '@(log)'
         return
         ;;

      # ====== Special case: CSV file =======================================
      --probe-schedule | -S)
         # Files with extension .csv
         _filedir '@(csv)'
         return
         ;;

      # ====== Special case: timer type =====================================
      --timer | -T)
         # shellcheck disable=SC2207
         COMPREPLY=( $(compgen -W "busy sleep" -- "${cur}") )
         return
         ;;
   esac

   # ====== All options =====================================================
   local opts="
-b
--busypoll
--client
--count
-D
-d
--delay
--dummy-pkt
--duplex
-e
--emulation
-i
--interface
-l
--log
-n
-o
-p
--ping-size
--pong-every
--pong-size
--port
--probe-schedule
-R
--reverse
-S
-s
--server
-T
-t
--timeout
--timer
-x
-y
"
   # shellcheck disable=SC2207
   COMPREPLY=( $( compgen -W "${opts}" -- "${cur}" ) )

   return 0
}

complete -F _nanoprobe nanoprobe
