#!/bin/csh -f
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2021-2022 Xilinx, Inc. All rights reserved.
#

# parse out required variables
set list = "$COMMAND_LINE"
# The command word associated with this xrt-smi invocation
set commandWord = `echo $list | awk 'BEGIN{FS=" "}{print $2}'`
# Get the number of arguments. This script turns spaces into ampersands to count
# the number of commands. This script is NOT space in path name friendly.
set argsplit=(`echo "$list" | sed -e "s/ /@/g"`)
# All non essential characters are removed here (Like hypens) so all definitions below do not have hyphens!
set argsplit=(`echo "$argsplit" | sed "s/[^a-zA-Z0-9@/']//g"`)
# Remove all alphanumeric characters and leave only the ampersands for word count
set argsplit_counter=(`echo "$argsplit" | sed "s/[a-zA-Z0-9/']//g"`)
# The number of characters left (which is only ampersands) determines the number of words in the argument list.
# In case it was missed above this is not a space friendly script.
set commandCount = (`echo $argsplit_counter | awk '{print length($0)}'`)
set previousWord = `echo $argsplit | awk 'BEGIN{FS="@"}{NF=NF-1;print $NF}'`
# This is the word on the righthand side
set currentWord = `echo $argsplit | awk 'BEGIN{FS="@"}{print $NF}'`

# The options for the current command line statement
set programOptions = "--verbose --batch --force --help -h --version"

# Handle the default xrt-smi options first
if($commandCount == "1") then
    set programOptions="dump examine program reset ${programOptions}"
# All other commands enter into this case
else
  set commonSubCommands="--device -d ${programOptions}"

  # First handle the gathering of all options unique to each command
  switch($commandWord)
    case "configure":
      set programOptions="--input --retention ${commonSubCommands}"
      breaksw
    case "dump":
      set programOptions="--flash -f --config -c --output -o ${commonSubCommands}"
      breaksw
    case "examine":
      set programOptions="--report -r --format -f --output -o ${commonSubCommands}"
      breaksw
    case "program":
      set programOptions="--base --image --shell --user -u --revert-to-golden ${commonSubCommands}"
      breaksw
    case "reset":
      set programOptions="${commonSubCommands}"
      breaksw
    # Return an empty reply if an invalid command is entered
    default:
      breaksw
  endsw

  # If the current word requires an argument, clear the option list
  # If an option is "missing" from this list check the wrapper. It is most likely defined there as a 
  # file search completion
  switch($previousWord)
    case "device":
    case "d":
      set programOptions=""
      breaksw
    case "format":
    case "f":
      set programOptions=""
      breaksw
    case "report":
    case "r":
      set programOptions=""
    case "--input":
      set programOptions=""
      breaksw
    case "--retention":
      set programOptions=""
      breaksw
    # do not modify the option list if the argument is not required
    default:
      breaksw
  endsw
endif

# Printout the options for complete to register them
echo $programOptions
