# Debian apt-get(8) completion

# @since 2.12
_comp_xfunc_apt_get_compgen_installed_packages()
{
    if [[ -f /etc/debian_version ]]; then
        # Debian system
        _comp_compgen -x dpkg installed_packages
    else
        # assume RPM based
        _comp_compgen -x rpm installed_packages
    fi
}

_comp_cmd_apt_get()
{
    local cur prev words cword comp_args package
    _comp_initialize -n ':=' -- "$@" || return

    local special="" i
    for ((i = 1; i < cword; i++)); do
        if [[ ${words[i]} == @(install|remove|auto?(-)remove|source|build-dep|changelog) ]]; then
            special=${words[i]}
            break
        fi
    done

    if [[ $special ]]; then
        case $special in
            remove | auto?(-)remove)
                _comp_xfunc_apt_get_compgen_installed_packages
                ;;
            source)
                # Prefer `apt-cache` in the same dir as command
                local pathcmd
                pathcmd=$(type -P -- "$1") && local PATH=${pathcmd%/*}:$PATH
                _comp_compgen -x apt-cache packages
                _comp_compgen -a split -- "$(apt-cache dumpavail |
                    _comp_awk '$1 == "Source:" { print $2 }' | sort -u)"
                ;;
            install | reinstall)
                if _comp_looks_like_path "$cur"; then
                    _comp_compgen_filedir '@(rpm|RPM)'
                    return
                elif [[ $cur == *=* ]]; then
                    package="${cur%%=*}"
                    cur="${cur#*=}"
                    _comp_compgen_split -l -- "$(
                        apt-cache --no-generate madison "$package" 2>/dev/null |
                            while IFS=' |' read -r _ version _; do
                                echo "$version"
                            done
                    )"
                    _comp_ltrim_colon_completions "$cur"
                    return
                fi
                ;;&
            build-dep)
                _comp_compgen_filedir -d
                _comp_looks_like_path "$cur" && return
                ;;&
            *)
                _comp_compgen -ax apt-cache packages
                ;;
        esac
        return
    fi

    local noargopts='!(-*|*[eoct]*)'
    # shellcheck disable=SC2254
    case $prev in
        --help | --version | --option | -${noargopts}[hvo])
            return
            ;;
        --config-file | -${noargopts}c)
            _comp_compgen_filedir
            return
            ;;
        --target-release | --default-release | -${noargopts}t)
            # Prefer `apt-cache` in the same dir as command
            local pathcmd
            pathcmd=$(type -P -- "$1") && local PATH=${pathcmd%/*}:$PATH
            _comp_compgen_split -- "$(apt-cache policy | command sed -ne '
                s/^ *release.*[ ,]o=Debian,a=\([[:alnum:]]*\).*/\1/p
                s/^ *release.*[ ,]o=Ubuntu,a=\([[:alnum:]]*\).*/\1/p
            ')"
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        _comp_compgen -- -W '--download-only --fix-broken --ignore-missing --fix-missing
            --no-download --quiet --simulate --just-print --dry-run --recon
            --no-act --yes
            --verbose-versions --build-profiles --compile
            --build --ignore-hold  --no-upgrade --only-upgrade
            --force-yes --print-uris --purge
            --reinstall --list-cleanup --target-release --default-release
            --trivial-only --no-remove --auto-remove --autoremove --only-source
            --diff-only --arch-only
            --help --version --config-file --option'
    else
        _comp_compgen -- -W 'update upgrade dist-upgrade
            install reinstall remove source build-dep check
            clean autoclean autoremove changelog dedup'
    fi

} &&
    complete -F _comp_cmd_apt_get apt-get
