#compdef keyringer

# Completion for git subcommand
_keyringer_git_complete() {
  # TODO: how to call _git() properly?
  return
}

_keyringer() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    # Initial options
    local config="$HOME/.keyringer"

    # Check if we have initial configuration
    if [ ! -d "$config" ]; then
      return
    fi

    # Process config
    local keyrings="`ls --color=never $config | sed -e '/^config$/d' | xargs`"
    local keyring_path="`grep -e "^$words[2]=" "$config/config" | tail -n 1 | cut -d = -f 2 | sed -e 's/"//g' -e "s/'//g" | sed -e 's/ *#.*$//'`"
    local instances="`echo $keyrings | sed -e 's/ /$\\\|^/g' -e 's/^/^/' -e 's/$/$/'`"

    _arguments        \
      '1: :->keyring' \
      '2: :->action'  \
      '3: :->options' \
      '4: :->misc'    \
      '*: :->final'

    case $state in
      keyring)
        _arguments "1:Keyrings:($keyrings)"
        ;;
      action)
        if echo $words[2] | grep -qe "$instances"; then
          compadd "$@" `KEYRINGER_CHECK_RECIPIENTS=false KEYRINGER_CHECK_VERSION=false keyringer $words[2] commands`
        else
          compadd "$@" "init"
        fi
        ;;
      options)
        case $words[3] in
          options|preferences)
            compadd "$@" ls edit add
            ;;
          recipients)
            compadd "$@" ls edit
            ;;
          ls|tree|mkdir|encrypt|encrypt-batch|pwgen|decrypt|edit|append|append-batch|del|rm|recrypt|open|clip|xclip|sclip|search|ssearch|find|mv|cp)
            words[4]="`echo $words[4] | sed -e "s|^/*||"`" # avoid leading slash
            compadd "$@" $(KEYRINGER_CHECK_RECIPIENTS=false KEYRINGER_CHECK_VERSION=false keyringer $words[2] ls -p -d $words[4]'*' 2> /dev/null)
            ;;
          genkeys|genpair)
            compadd "$@" gpg ssh x509 x509-self ssl ssl-self
            ;;
          git)
            compadd "$@" $(_keyringer_git_complete $words[4])
            ;;
          init)
            _files
            ;;
          *)
            ;;
        esac
        ;;
      misc)
        case "$words[3]" in
          mv|cp)
            words[5]="`echo $words[5] | sed -e "s|^/*||"`" # avoid leading slash
            compadd "$@" $(KEYRINGER_CHECK_RECIPIENTS=false KEYRINGER_CHECK_VERSION=false keyringer $words[2] ls -p -d $words[5]'*' 2> /dev/null)
            ;;
          recipients)
            words[5]="$(echo $words[5] | sed -e "s|^/||")" # TODO: avoid leading slash
            compadd "$@" $(cd $keyring_path/config/recipients && ls --color=never -p $words[5]* 2> /dev/null)
            ;;
          genkeys|genpair)
            words[5]="$(echo $words[5] | sed -e "s|^/||")" # TODO: avoid leading slash
            compadd "$@" $(KEYRINGER_CHECK_RECIPIENTS=false KEYRINGER_CHECK_VERSION=false keyringer $words[2] ls -p -d $words[5]'*' 2> /dev/null)
            ;;
          git)
            compadd "$@" $(_keyringer_git_complete $words[4] $words[5])
            ;;
          encrypt|encrypt-batch|pwgen)
            _files
            ;;
          *)
            ;;
        esac
        ;;
      *)
        if [[ $words[3] == "git" ]]; then
          # TODO
          true
        fi
        ;;
    esac
}

_keyringer "$@"
