# p11cat(1) completion                                     -*- shell-script -*-
#
# Bash autocompletion for `p11cat`.
# Provides completions for options and PKCS#11 object labels.
#
# Depends on:
#   - ./p11-common

source "$(dirname "${BASH_SOURCE[0]}")/p11-common"

_p11cat_opts="-l -m -s -t -p -S -x -h -V"

_p11cat() {
  local cur="${COMP_WORDS[COMP_CWORD]}"
  local prev="${COMP_WORDS[COMP_CWORD-1]}"

  if [[ "$prev" == "-l" ]]; then
    __p11_complete_filepath "$cur"
    return
  fi

  if [[ "$prev" == "-m" ]]; then
    __p11_complete_dirpath "$cur"
    return
  fi

  if [[ "$prev" == "-s" ]]; then
    __p11_complete_slots "$cur"
    return
  fi

  if [[ "$prev" == "-t" ]]; then
    __p11_complete_token_labels "$cur"
    return
  fi

  if [[ "$cur" == -* ]]; then
    __p11_complete_options "$cur" "${_p11cat_opts}"
    return
  fi

  __p11_complete_objects "$cur"
}

complete -F _p11cat p11cat
