# bash completion for autospec

_autospec_colors() {
    local themes_sys="/etc/autospec.d"
    local themes_usr="$HOME/.autospec.d"
    local themes="none"
    local f
    for f in "$themes_sys"/color-theme.* "$themes_usr"/color-theme.*; do
        [[ -f "$f" ]] && themes+=" ${f##*/color-theme.}"
    done
    echo "$themes"
}

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

    local mode=""
    local w
    for w in "${COMP_WORDS[@]}"; do
        case "$w" in
            -u|--update)  mode=update  ;;
            -s|--source)  mode=source  ;;
            -x|--extract) mode=extract ;;
            --eval)       mode=eval    ;;
        esac
    done

    local common_opts="--colors -C --config -D --debug -q --quiet -h --help -V --version"

    case "$prev" in
        --colors)
            COMPREPLY=( $(compgen -W "$(_autospec_colors)" -- "$cur") )
            return ;;
        -C|--config)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return ;;
    esac

    if [[ "$mode" == "update" ]]; then
        case "$prev" in
            -a|--action)
                COMPREPLY=( $(compgen -W "0 1 2 3 4 5 6 7 8 9 10 11" -- "$cur") )
                return ;;
            -S|--specfile)
                COMPREPLY=( $(compgen -f -X '!*.spec' -- "$cur") )
                return ;;
            -A|--arch)
                COMPREPLY=( $(compgen -W "x86_64 i686 aarch64 noarch" -- "$cur") )
                return ;;
            --root)
                COMPREPLY=( $(compgen -d -- "$cur") )
                return ;;
            --ignore-test)
                COMPREPLY=( $(compgen -W "0 1 2 3 4 5 6 7 8 9" -- "$cur") )
                return ;;
            -d|--define|-l|--login|--changelog|--packager-fullname|--packager-email|\
            --distro-rpm|--server-download|--server-upload|--server)
                return ;;
        esac
        local update_opts="-u --update
            -a --action -d --define -l --login
            -S --specfile -A --arch
            --server-download --server-upload --server
            --changelog --packager-fullname --packager-email --distro-rpm
            --nosrpm --norpm --list-check
            -b --update-autobuildreq
            --force-update --force-build
            --force-install --force-install-nodeps
            --force-download --force
            --ignore-test
            -p --noprep -c --clear -f --format
            -L --log -R --rebuild --root
            $common_opts"
        COMPREPLY=( $(compgen -W "$update_opts" -- "$cur") )
        return

    elif [[ "$mode" == "source" ]]; then
        case "$prev" in
            -s|--source|-o|--output)
                COMPREPLY=( $(compgen -f -- "$cur") )
                return ;;
            -t|--type)
                COMPREPLY=( $(compgen -W "standard library python perl perl-build ghc ocaml-libs gnome kde5 kf6 web standard-daemon" -- "$cur") )
                return ;;
            --changelog|--packager-fullname|--packager-email|\
            -n|--pck-name|-v|--pck-version|--git-branch)
                return ;;
        esac
        local source_opts="-s --source
            -n --pck-name -v --pck-version
            -t --type -o --output
            --changelog --packager-fullname --packager-email
            --git-branch --preserve-dot-git
            $common_opts"
        COMPREPLY=( $(compgen -W "$source_opts" -- "$cur") )
        return

    elif [[ "$mode" == "extract" ]]; then
        case "$prev" in
            -F|--files|--destdir)
                COMPREPLY=( $(compgen -f -- "$cur") )
                return ;;
        esac
        COMPREPLY=( $(compgen -W "-x --extract -F --files --destdir $common_opts" -- "$cur") )
        return

    elif [[ "$mode" == "eval" ]]; then
        COMPREPLY=( $(compgen -W "$common_opts" -- "$cur") )
        return
    fi

    # no mode selected yet: offer top-level options
    COMPREPLY=( $(compgen -W "-u --update -s --source -x --extract --eval $common_opts" -- "$cur") )
}

complete -F _autospec autospec
