# Debian apt-offline(8) completion                             -*- shell-script -*-

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

    local special i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${words[i]} == @(set|get|install|--install-packages|--install-src-packages) ]]; then
            special=${words[i]}
        fi
    done

    if [[ -n $special ]]; then
        case $special in
            --install-src-packages)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) $( apt-cache dumpavail | \
                    command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) )
                return 0
                ;;
            --install-packages)
                COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
                    2> /dev/null ) )
                return 0
                ;;
            get)
                case $prev in
                    -d|--download-dir|-s|--cache-dir)
                        _filedir -d
                        return 0
                        ;;
                    --bundle)
                        _filedir
                        return 0
                        ;;
                    --https-cert-file)
                        _filedir
                        return 0
                        ;;
                    --https-key-file)
                        _filedir
                        return 0
                        ;;
                    --http-basicauth)
                        _http_auth_url
                        return 0
                        ;;
                    --proxy-host)
                        _ip_addresses
                        return 0
                        ;;
                    --proxy-port)
                        _port
                        return 0
                        ;;
                    --socket-timeout)
                        COMPREPLY=( $( compgen -W '10 30 100 300 1000' -- "$cur" ) )
                        return 0
                        ;;
                    -t|--threads)
                        COMPREPLY=( $( compgen -W '2 3 4 5 6 7 8 9 10' -- "$cur" ) )
                        return 0
                        ;;
                esac
                if [[ "$cur" == -* || -e $prev ]]; then
                    COMPREPLY=( $( compgen -W '-h --help -v --verbose --version
                        --simulate --socket-timeout -d --download-dir -s
                        --cache-dir --no-checksum -t --threads --bundle
                        --bug-reports --proxy-host --proxy-port
                        --https-key-file --https-cert-file --disable-cert-check
                        --http-basicauth' -- "$cur" ) )
                else
                    _filedir
                fi
                return 0
                ;;
            install)
                case $prev in
                    --install-src-path)
                        _filedir -d
                        return 0
                        ;;
                esac
                if [[ "$cur" == -* || -e $prev ]]; then
                    COMPREPLY=( $( compgen -W '-h --help -v --verbose --version
                        --simulate --install-src-path --skip-bug-reports
                        --allow-unauthenticated --skip-changelog
                        --strict-deb-check' -- "$cur" ) )
                else
                    _filedir
                fi
                return 0
                ;;
            set)
                case $prev in
                    --release)
                        COMPREPLY=( $( apt-cache policy | \
                            command grep "release.o=Debian,a=$cur" | \
                            sed -e "s/.*a=\(\w*\).*/\1/" | uniq 2> /dev/null) )
                        return 0
                        ;;
                    --upgrade-type)
                        COMPREPLY=( $( compgen -W 'upgrade dist-upgrade
                            dselect-ugprade' -- "$cur" ) )
                        return 0
                        ;;
                    --apt-backend)
                        COMPREPLY=( $( compgen -W 'apt apt-get
                            python-apt' -- "$cur" ) )
                        return 0
                        ;;
                esac
                if [[ "$cur" == -* || -e $prev ]]; then
                    COMPREPLY=( $( compgen -W '-h --help -v --verbose --version
                        --simulate --install-packages --install-src-packages
                        --src-build-dep --release --update --upgrade
                        --upgrade-type --generate-changelog' -- "$cur" ) )
                else
                    _filedir
                fi
                return 0
                ;;
        esac
    fi

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-h --help -v --verbose --version --simulate' -- "$cur" ) )
    else
        COMPREPLY=( $( compgen -W 'get install set' -- "$cur" ) )
    fi

    return 0
} &&
complete -F _apt_offline apt-offline

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