# Debian tx(8) completion                             -*- shell-script -*-

_tx()
{
    local cur prev words cword
    _init_completion || return

    local special i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${words[i]} == @(delete|help|init|pull|push|set|status) ]]; then
            special=${words[i]}
        fi
    done

    if [[ -n $special ]]; then
        case $special in
            delete)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-f -h -l -r
                        --force --help --language --resource --skip
                        ' -- "$cur" ) )
                fi
                case $prev in
                    -r|--resource)
                        resources=$(sed -n 's,^\[\(.*\)\]$,\1,p' .tx/config | grep -v main)
                        COMPREPLY=( $( compgen -W "$resources" -- "$cur" ) )
                        ;;
                esac
                return 0
                ;;
            help)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-h --help' -- "$cur" ) )
                fi
                return 0
                ;;
            init)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-h
                        --help --host --pass --user
                        ' -- "$cur" ) )
                fi
                return 0
                ;;
            pull)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-a -f -h -l -r -s
                        --all --disable-overwrite --force --help --language
                        --minimum-perc --mode --pseudo --resource --skip
                        --source
                        ' -- "$cur" ) )
                fi
                case $prev in
                    -r|--resource)
                        resources=$(sed -n 's,^\[\(.*\)\]$,\1,p' .tx/config | grep -v main)
                        COMPREPLY=( $( compgen -W "$resources" -- "$cur" ) )
                        ;;
                esac
                return 0
                ;;
            push)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-f -h -l -r -s -t
                        --force --help --language --no-interactive --resource
                        --skip --source --translations
                        ' -- "$cur" ) )
                fi
                case $prev in
                    -r|--resource)
                        resources=$(sed -n 's,^\[\(.*\)\]$,\1,p' .tx/config | grep -v main)
                        COMPREPLY=( $( compgen -W "$resources" -- "$cur" ) )
                        ;;
                esac
                return 0
                ;;
            set)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-f -h -l -r -s -t
                        --auto-local --auto-remote --execute --force --help
                        --language --minimum-perc --mode --resource --skip
                        --source --type
                        ' -- "$cur" ) )
                fi
                case $prev in
                    -r|--resource)
                        resources=$(sed -n 's,^\[\(.*\)\]$,\1,p' .tx/config | grep -v main)
                        COMPREPLY=( $( compgen -W "$resources" -- "$cur" ) )
                        ;;
                esac
                return 0
                ;;
            status)
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=( $( compgen -W '-h -r
                        --help --resource
                        ' -- "$cur" ) )
                fi
                case $prev in
                    -r|--resource)
                        resources=$(sed -n 's,^\[\(.*\)\]$,\1,p' .tx/config | grep -v main)
                        COMPREPLY=( $( compgen -W "$resources" -- "$cur" ) )
                        ;;
                esac
                return 0
                ;;
        esac
    fi

    case $prev in
        -r|--root)
             _filedir -d
             return 0
             ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-d -h -q -r
            --debug --disable-colors --help --quiet --root --traceback --version
            ' -- "$cur" ) )
    else
        COMPREPLY=( $( compgen -W '
            delete help init pull push set status
            ' -- "$cur" ) )
    fi

    return 0
} &&
complete -F _tx tx

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