# GENERATED by contrib/gen-completions.py from contrib/completions.json - do not edit by hand.
# bash-completion for slacker (https://forge.slackware.nl/rizitis/slacker)

_slacker_config_dir() {
    local i
    for ((i = 1; i < ${#COMP_WORDS[@]}; i++)); do
        if [[ ${COMP_WORDS[i]} == --config-dir && -n ${COMP_WORDS[i+1]} ]]; then
            printf '%s' "${COMP_WORDS[i+1]}"
            return
        fi
    done
    printf '/etc/slacker'
}

_slacker_installed_names() {
    local db=/var/adm/packages
    [[ -d $db ]] || return
    command ls "$db" 2>/dev/null \
        | grep -Ev -- '-upgraded-[0-9]' \
        | sed -E 's/(-[^-]+){3}$//'
}

_slacker_repo_names() {
    local repos="$1/repos"
    [[ -f $repos ]] || return
    awk '
        /^[[:space:]]*#/ { next }
        /^[[:space:]]*$/ { next }
        {
            url = $3
            if (url ~ /:\/\// || url == "mirror" || url ~ /^mirror\//)
                print $2
        }' "$repos"
}

_slacker_build_tags() {
    local repos="$1/repos" db=/var/adm/packages
    {
        if [[ -f $repos ]]; then
            awk '
                /^[[:space:]]*#/ { next }
                /^[[:space:]]*$/ { next }
                {
                    url = $3
                    if (!(url ~ /:\/\// || url == "mirror" || url ~ /^mirror\//) && NF >= 3)
                        print $3
                }' "$repos"
        fi
        if [[ -d $db ]]; then
            command ls "$db" 2>/dev/null \
                | grep -Ev -- '-upgraded-[0-9]' \
                | sed -E 's/.*-([^-]+)$/\1/; s/^[0-9]+//' \
                | grep -v '^$'
        fi
    } | sort -u
}

_slacker_template_names() {
    local dir="$1/templates"
    [[ -d $dir ]] || return
    command ls "$dir" 2>/dev/null | sed -E 's/\.template$//'
}

_slacker_commands='update search file-search info list-repos status install upgrade reinstall remove revert-pkg download upgrade-all upgrade-dist install-new clean-system clean-cache new-config check-updates show-changelog history find-mirror generate-template install-template remove-template delete-template frozen unfrozen pin unpin add-repo del-repo pri-repo add-tag del-tag vet-repo trust-repo distrust-repo help'
_slacker_global_flags='--config-dir --yes -y --dry-run --no-deps --help -h --version -V'

_slacker_dispatch() {
    local config; config=$(_slacker_config_dir)

    if [[ $prev == --config-dir ]]; then
        compopt -o dirnames 2>/dev/null
        COMPREPLY=()
        return
    fi

    if [[ $cur == -* ]]; then
        local flags="$_slacker_global_flags"
        case $sub in
            download) flags+=' -o --output' ;;
            history) flags+=' --last --since --installed --removed --upgraded' ;;
        esac
        COMPREPLY=($(compgen -W "$flags" -- "$cur"))
        return
    fi

    if [[ $sub == download && ( $prev == -o || $prev == --output ) ]]; then
        compopt -o dirnames 2>/dev/null
        COMPREPLY=()
        return
    fi

    case $sub in
        install|upgrade|reinstall|remove|download)
            if [[ $cur == @* ]]; then
                local names tags list body
                names=$(_slacker_repo_names "$config")
                tags=$(_slacker_build_tags "$config")
                list=""
                for body in $names; do list+=" @$body"; done
                for body in $tags;  do list+=" @$body"; done
                COMPREPLY=($(compgen -W "$list" -- "$cur"))
                return
            fi
            ;;
    esac

    case $sub in
        info|reinstall|remove|revert-pkg|history|unpin)
            COMPREPLY=($(compgen -W "$(_slacker_installed_names)" -- "$cur")) ;;
        install-new|clean-cache|show-changelog|del-repo|pri-repo|vet-repo|trust-repo|distrust-repo)
            COMPREPLY=($(compgen -W "$(_slacker_repo_names "$config")" -- "$cur")) ;;
        install-template|remove-template|delete-template)
            COMPREPLY=($(compgen -W "$(_slacker_template_names "$config")" -- "$cur")) ;;
        pin)
            if [[ $cur != *:* ]]; then
                local r list=""
                for r in $(_slacker_repo_names "$config"); do list+=" $r:"; done
                compopt -o nospace 2>/dev/null
                COMPREPLY=($(compgen -W "$list" -- "$cur"))
            fi ;;
    esac
}

_slacker() {
    local cur prev words cword
    _init_completion 2>/dev/null || {
        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        prev=${COMP_WORDS[COMP_CWORD-1]}
    }

    local sub="" i
    for ((i = 1; i < COMP_CWORD; i++)); do
        case ${COMP_WORDS[i]} in
            --config-dir) ((i++)) ;;
            -*) ;;
            *) sub=${COMP_WORDS[i]}; break ;;
        esac
    done

    if [[ -z $sub ]]; then
        if [[ $cur == -* ]]; then
            COMPREPLY=($(compgen -W "$_slacker_global_flags" -- "$cur"))
        else
            COMPREPLY=($(compgen -W "$_slacker_commands" -- "$cur"))
        fi
        return
    fi

    _slacker_dispatch
}

complete -F _slacker slacker
