#compdef clr-boot-manager
# -----------------------------------------------------------------------
#   Kernel & Boot Loader Management - autocompletion script
#
#   Author: Lucius Hu - http://github.com/lebensterben
#
#   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, version 2 or later of the License.
#
#   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/>.
# -----------------------------------------------------------------------

local context curcontext="$curcontext" state state_descr line ret=1
local -A opt_args val_args
local -a subcmds; subcmds=(
  "version:Print the version and quit"
  "report-booted:Report the current kernel as successfully booted"
  "update:Perform post-update configuration of the system"
  "set-timeout:Set the timeout to be used by the bootloader"
  "get-timeout:Get the timeout to be used by the bootloader"
  "set-kernel:Configure kernel to be used at next boot"
  "remove-kernel:Remove kernel from system"
  "list-kernels:Display currently selectable kernels to boot"
  "help:Display help information on available commands"
)

_arguments -C \
           '1:clr-boot-manager subcommand:->subcmd' \
           '*::clr-boot-mannager argument:->args' \
  && ret=0

if [[ -n "$state" ]]; then
  local -a args=(
    '(-p --path)'{-p,--path=}'[Set the base path for boot management operations]:path: _files -/'
    '(-i --image)'{-i,--image}'[Force clr-boot-manager to run in image mode]'
    '(-n --no-efi-update)'{-n,--no-efi-update}'[Don`t update efi vars when using shim-systemd backend]'
  )
  case "$state" in
    subcmd)
      _describe -t subcmds 'clr-boot-manager subcommand' subcmds && ret=0
      ;;
    args)
      case $line[1] in
        get-timeout|list-kernels|update)
          _arguments $args && ret=0
        ;;
        set-kernel|remove-kernel)
          local -a kernelpath

          kernelpath=(${opt_args[--path=]:-${opt_args[-p]}})
          if (( $#kernelpath )); then
            # Filename expansion, path start with `~` (zsh completion system doesn't expand ~
            eval kernelpath=$kernelpath
            # Convert to absolute path (_path_files function cannot understand relative path)
            kernelpath=$(realpath -e "$kernelpath")
          fi
          # Prepend it to /usr/lib/kernel
          kernelpath="$kernelpath/usr/lib/kernel"
          if [ -r "$kernelpath" ]; then
            args+=(':kernel: _path_files -W "$kernelpath" -g com.solus-project.\*')
          else
            args+=(': : _message -r "Directory \"$kernelpath\" does not exist."')
          fi
          _arguments $args && ret=0
          ;;
        set-timeout)
          local -a args=($args)
          args+=(':timeout: _message -r "Please enter an integer value"')
          _arguments $args && ret=0
          ;;
        help)
          _describe -t subcmds 'clr-boot-manager subcommand' subcmds && ret=0
        ;;
      esac
      ;;
    *)
      ret=1
      ;;
  esac
fi

return ret
