# Copyright (C) 2012 - 2014 Jason A. Donenfeld <Jason@zx2c4.com> and
# Brian Mattern <rephorm@rephorm.com>. All Rights Reserved.
# This file is licensed under the GPLv2+.

# completion file for bash

_gopass_complete_entries () {
    prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
    suffix=".gpg"
    autoexpand=${1:-0}

    local IFS=$'\n'
    local items=($(compgen -f $prefix$cur))
    for item in ${items[@]}; do
        [[ $item =~ /\.[^/]*$ ]] && continue

        # if there is a unique match, and it is a directory with one entry
        # autocomplete the subentry as well (recursively)
        if [[ ${#items[@]} -eq 1 && $autoexpand -eq 1 ]]; then
            while [[ -d $item ]]; do
                local subitems=($(compgen -f "$item/"))
                local filtereditems=( )
                for item2 in "${subitems[@]}"; do
                    [[ $item2 =~ /\.[^/]*$ ]] && continue
                    filtereditems+=( "$item2" )
                done
                if [[ ${#filtereditems[@]} -eq 1 ]]; then
                    item="${filtereditems[0]}"
                else
                    break
                fi
            done
        fi

        # append / to directories
        [[ -d $item ]] && item="$item/"

        item="${item%$suffix}"
        COMPREPLY+=("${item#$prefix}")
    done
}

_gopass_complete_folders () {
    prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"

    local IFS=$'\n'
    local items=($(compgen -d $prefix$cur))
    for item in ${items[@]}; do
        [[ $item == $prefix.* ]] && continue
        COMPREPLY+=("${item#$prefix}/")
    done
}

_gopass_complete_keys () {
    local IFS=$'\n'
    # Extract names and email addresses from gpg --list-keys
    local keys="$(gpg2 --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d')"
    COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
}

_gopass()
{
    COMPREPLY=()
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local commands="init ls find grep show insert generate edit rm mv cp git help version"
    if [[ $COMP_CWORD -gt 1 ]]; then
        local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
        COMPREPLY+=($(compgen -W "-h --help" -- ${cur}))
        case "${COMP_WORDS[1]}" in
            init)
                if [[ $lastarg == "-p" || $lastarg == "--path" ]]; then
                    _gopass_complete_folders
                else
                    COMPREPLY+=($(compgen -W "-p --path" -- ${cur}))
                    _gopass_complete_keys
                fi
                ;;
            ls|list|edit)
                _gopass_complete_entries
                ;;
            show|-*)
                _gopass_complete_entries 1
                ;;
            insert)
                COMPREPLY+=($(compgen -W "-m --multiline -f --force" -- ${cur}))
                _gopass_complete_entries
                ;;
            generate)
                COMPREPLY+=($(compgen -W "-n --no-symbols -f --force" -- ${cur}))
                _gopass_complete_entries
                ;;
            cp|mv)
                COMPREPLY+=($(compgen -W "-f --force" -- ${cur}))
                _gopass_complete_entries
                ;;
            rm)
                COMPREPLY+=($(compgen -W "-r --recursive -f --force" -- ${cur}))
                _gopass_complete_entries
                ;;
            git)
                COMPREPLY+=($(compgen -W "init push pull config log reflog rebase status" -- ${cur}))
                ;;
        esac
    else
        _gopass_complete_entries 1
    fi
}

complete -o filenames -o nospace -F _gopass gopass
