# rpi-connect completion                                   -*- shell-script -*-

# This bash completions script was generated by
# completely (https://github.com/bashly-framework/completely)
# Modifying it manually is not recommended

_rpi-connect_completions_filter() {
  local words="$1"
  local cur=${COMP_WORDS[COMP_CWORD]}
  local result=()

  # words the user already typed (excluding the command itself)
  local used=()
  if ((COMP_CWORD > 1)); then
    used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
  fi

  if [[ "${cur:0:1}" == "-" ]]; then
    # Completing an option: offer everything (including options)
    echo "$words"

  else
    # Completing a non-option: offer only non-options,
    # and don't re-offer ones already used earlier in the line.
    for word in $words; do
      [[ "${word:0:1}" == "-" ]] && continue

      local seen=0
      for u in "${used[@]}"; do
        if [[ "$u" == "$word" ]]; then
          seen=1
          break
        fi
      done
      ((!seen)) && result+=("$word")
    done

    echo "${result[*]}"
  fi
}

_rpi-connect_completions() {
  local cur=${COMP_WORDS[COMP_CWORD]}
  local compwords=()
  if ((COMP_CWORD > 0)); then
    compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
  fi
  local compline="${compwords[*]}"

  COMPREPLY=()

  case "$compline" in
    'signin'*)
      while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_rpi-connect_completions_filter "--auth-key --identity --non-interactive")" -- "$cur")
      ;;

    'shell'*)
      while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_rpi-connect_completions_filter "on off")" -- "$cur")
      ;;

    'vnc'*)
      while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_rpi-connect_completions_filter "on off")" -- "$cur")
      ;;

    'ota'*)
      while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_rpi-connect_completions_filter "on off")" -- "$cur")
      ;;

    *)
      while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_rpi-connect_completions_filter "on off restart signin signout vnc shell status doctor version ota reload")" -- "$cur")
      ;;

  esac
} &&
  complete -F _rpi-connect_completions rpi-connect

# ex: filetype=sh
