#!/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="${programOptions} program validate examine configure reset"
else
  set commonSubCommands="--device -d ${programOptions}"

  # First handle the gathering of all options unique to each command
  switch($commandWord)
    case "program":
      set programOptions="${programOptions} --user -u"
      breaksw
    case "validate":
      set programOptions="${programOptions} --run -r --format -f --output -o --path -p"
      breaksw
    case "examine":
      set programOptions="${programOptions} --report -r --format -f --output -o"
      breaksw
    case "configure":
      set programOptions="${programOptions} --host-mem --p2p --size"
      breaksw
    case "reset":
      set programOptions="${programOptions} --type -t"
      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 "type":
    case "t":
      set programOptions=""
      breaksw
    case "p2p":
      set programOptions=""
      breaksw
    case "hostmem":
      set programOptions=""
      breaksw
    # -r shorthand applies to multiple commands under xrt-smi and requires additional processing
    # Assuming we want to add something here one day
    case "report":
    case "run":
    case "r":
      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
