# Debian GNU/Linux debtags(1) completion
# (C) 2004 2005 Emanuele Rocca <ema@debian.org>
# (C) 2017 Enrico Rossi <e.rossi@tecnobrain.com>
# License: GNU GPL v2 or later

# @note: depracated command will not be completed.

_debtags()
{
    local cur prev options

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

    # Helper function to keep up with ":"
    _get_comp_words_by_ref -n : cur prev

    options='cat check diff dumpavail grep help \
        mkpatch search show submit tagcat tagsearch \
        tagshow update vocfilter'

    local i

    for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
        case ${COMP_WORDS[i]} in
            cat|grep)
                if [[ "$cur" == -* ]]; then
                    options='--facets --names --quiet --invert --help'
                    COMPREPLY=( $( compgen -W "$options" -- $cur ) )
                fi

                return 0
                ;;

            check)
                if [[ "$cur" == -* ]]; then
                    options='--facets --names --quiet --help'
                    COMPREPLY=( $( compgen -W "$options" -- $cur ) )
                    return 0
                fi

                _filedir
                return 0
                ;;

            diff|mkpatch)
                if [[ "$cur" == -* ]]; then
                    options='--help'
                    COMPREPLY=( $( compgen -W "$options" -- $cur ) )
                    return 0
                fi

                _filedir
                return 0
                ;;

            tagshow)
                # No more that 1 args
                if [[ "$COMP_CWORD"  -gt 4 ]]; then
                    return 0
                fi

                if [[ "$cur" == -* ]]; then
                    options='--help'
                    COMPREPLY=( $( compgen -W "$options" -- $cur ) )
                    return 0
                fi

                COMPREPLY=( $( grep "^Tag: $cur" /var/lib/debtags/vocabulary |cut -b 6- ) )

                # Helper function to keep up with ":"
                __ltrim_colon_completions "$cur"
                return 0
                ;;

            search|dumpavail)
                if [[ "$cur" == -* ]]; then
                    options='--names --quiet --full --short --invert --help'
                    COMPREPLY=( $( compgen -W "$options" -- $cur ) )
                    return 0
                fi

                COMPREPLY=( $( grep "^Tag: $cur" /var/lib/debtags/vocabulary |cut -b 6- ) )

                # Helper function to keep up with ":"
                __ltrim_colon_completions "$cur"
                return 0
                ;;
            *)
                ;;
        esac
    done

    # short or long option
    if [[ "$cur" == -* ]]; then
        options='--verbose --debug -V --version -? --help'
        COMPREPLY=( $( compgen -W "$options" -- $cur ) )
        return 0
    fi

    if [[ "$COMP_CWORD" == 1 ]]; then
        COMPREPLY=( $( compgen -W "$options" -- $cur ) )
        return 0
    fi
} && complete -F _debtags debtags

# ex: ts=4 sw=4 et filetype=sh
