# bash completion for xbmgmt                              -*- shell-script -*-
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2021-2022 Xilinx, Inc. All rights reserved.
#

# Generates the command word options dependant on the previous word
# using COMPREPLY
# If an option requires an argument, it will not provide a list through
# COMPREPLY
# 
# Parameters:
# 1: The complete list of options for the previous word
_command_word_xbmgmt_completion()
{
  # Get the righthand most word on the command line
  local cur=${COMP_WORDS[COMP_CWORD]}
  # The previous word 
  local prev=${COMP_WORDS[COMP_CWORD-1]}

  # The BDFs (for devices) contain colons which breaks the autocomplete
  # to get around this a helper function removes colons and helps parse the current and previous
  # words
  _get_comp_words_by_ref -n : cur prev

  # Each defined case requires an argument so no reply is given
  # All other cases default to using `compgen` output to format COMPREPLY
  case ${prev} in
    -d|--device)
      options=""
      ;;
    -u|--user)
      _filedir
      options=""
      ;;
    -f|--format)
      options=""
      ;;
    -o|--output)
      _filedir
      options=""
      ;;
    -r|--report)
      options=""
      ;;
    --shell)
      _filedir
      options=""
      ;;
    --image)
      _filedir
      options=""
      ;;
    --input)
      ;;
    --retention)
      ;;
    *)
      # Default to using the passed in options if no particular option is used
      options=$1
      ;;
  esac
  # The format of the compgen commands options is seperated from the current word using a --.
  # The -- character signifies the end of command options. All following arguments are positional.
  COMPREPLY+=($(compgen -W "$options" -- $cur))
  # 2nd part of getting around colons in the suggested keywords
  __ltrim_colon_completions "$cur"
}

# The main function populating the COMPREPLY
_xbmgmt_completion()
{
  commonSubCommands="--verbose --batch --force --help -h --version"
  # COMP_CWORD is the current index of the cursor in the command line
  # 0 is the first argument (xbmgmt), 1 is the desired command for xbmgmt,
  # 2 is an option for the command, etc.
  case ${COMP_CWORD} in
    # Case for command after xbmgmt
    1)
      options="configure dump examine program reset ${commonSubCommands}"
      ;;
    # Case for options after the above command is entered
    *)
      # Command word is used to specify further options as the command expands
      commandWord=${COMP_WORDS[1]}
      # Options that appear for all commands
      commonSubCommands="--device -d ${commonSubCommands}"
      # Once a command is identified the options will always be the same
      case ${commandWord} in
        "configure")
          options="--input --retention ${commonSubCommands}"
          ;;
        "dump")
          options="--flash -f --config -c --output -o ${commonSubCommands}"
          ;;
        "examine")
          options="--report -r --format -f --output -o ${commonSubCommands}"
          ;;
        "program")
          options="--base --image --shell --user -u --revert-to-golden ${commonSubCommands}"
          ;;
        "reset")
          options="${commonSubCommands}"
          ;;
        # Return an empty reply if an invalid command is entered
        *)
          ;;
      esac
      ;;
  esac
  _command_word_xbmgmt_completion "${options}"
}

complete -F _xbmgmt_completion xbmgmt
echo Autocomplete enabled for the xbmgmt command

# ex: filetype=sh
