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

    if [[ ${words[1]} == --help || ${words[1]} == --version ]]; then
        return
    fi

    case $prev in
    -n | --name)
        _filedir
        return
        ;;
    -l | --level)
        COMPREPLY=($(compgen -W '00 01 02 03 04 05 06 07 08 09' -- "$cur"))
        return
        ;;
    esac

    if [[ ${words[1]} == install ]]; then
        if [[ $cur == --* ]]; then
            COMPREPLY=($(compgen -W '--yes --allow-downgrades --install-recommends --install-suggests --no-install-recommends --no-install-suggests --target-release --bext --comp --name --level --help --version' -- "$cur"))
        elif [[ $cur == -* ]]; then
            COMPREPLY=($(compgen -W '-b -c -y -t -n -l --help --version' -- "$cur"))
        elif [[ ${#words[@]} -ge 3 && $cur != */* ]]; then
            COMPREPLY+=($(apt-cache --no-generate pkgnames "$cur" 2>/dev/null))
        else
            _filedir
        fi
    elif [[ ${words[1]} == upgrade ]]; then
        if [[ $cur == --* ]]; then
            COMPREPLY=($(compgen -W '--yes --allow-downgrades --install-recommends --install-suggests --no-install-recommends --no-install-suggests --target-release --bext --comp --name --level --help --version' -- "$cur"))
        elif [[ $cur == -* ]]; then
            COMPREPLY=($(compgen -W '-b -c -y -t -n -l --help --version' -- "$cur"))
        fi
    else
        if [[ $cur == -* ]]; then
            COMPREPLY=($(compgen -W '--help --version' -- "$cur"))
        else
            COMPREPLY=($(compgen -W 'install upgrade' -- "$cur"))
        fi
    fi
}

complete -F _apt2sb apt2sb
