#compdef slacker
# GENERATED by contrib/gen-completions.py from contrib/completions.json - do not edit by hand.
_slacker_config_dir() {
    local i
    for (( i = 2; i <= $#words; i++ )); do
        if [[ ${words[i]} == --config-dir && -n ${words[i+1]} ]]; then
            print -r -- ${words[i+1]}
            return
        fi
    done
    print -r -- /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
    {
        [[ -f $repos ]] && awk '
            /^[[:space:]]*#/ { next }
            /^[[:space:]]*$/ { next }
            { url = $3
              if (!(url ~ /:\/\// || url == "mirror" || url ~ /^mirror\//) && NF >= 3) print $3 }' $repos
        [[ -d $db ]] && command ls $db 2>/dev/null \
            | grep -Ev -- '-upgraded-[0-9]' \
            | sed -E 's/.*-([^-]+)$/\1/; s/^[0-9]+//' | grep -v '^$'
    } | sort -u
}

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


_slacker() {
    local cfg; cfg=$(_slacker_config_dir)
    local -a cmds; cmds=( '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' )
    local -a gflags; gflags=( '--config-dir' '--yes' '-y' '--dry-run' '--no-deps' '--help' '-h' '--version' '-V' )

    if (( CURRENT == 2 )); then
        compadd -a cmds
        compadd -a gflags
        return
    fi

    local sub; sub=${words[2]}

    # The value after --config-dir is a directory.
    if [[ ${words[CURRENT-1]} == --config-dir ]]; then
        _files -/
        return
    fi

    # A flag is being typed: global flags plus any this subcommand adds.
    if [[ $PREFIX == -* ]]; then
        local -a flags; flags=( $gflags )
        case $sub in
        (download) flags+=( '-o' '--output' ) ;;
        (history) flags+=( '--last' '--since' '--installed' '--removed' '--upgraded' ) ;;
        esac
        compadd -a flags
        return
    fi

    case $sub in
        (info)
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (install)
            if [[ $PREFIX == @* ]]; then
                local -a sels
                sels=( ${(f)"$(_slacker_repo_names $cfg)"} ${(f)"$(_slacker_build_tags $cfg)"} )
                sels=( ${sels/#/@} )
                compadd -a sels
                return
            fi
            ;;
        (upgrade)
            if [[ $PREFIX == @* ]]; then
                local -a sels
                sels=( ${(f)"$(_slacker_repo_names $cfg)"} ${(f)"$(_slacker_build_tags $cfg)"} )
                sels=( ${sels/#/@} )
                compadd -a sels
                return
            fi
            ;;
        (reinstall)
            if [[ $PREFIX == @* ]]; then
                local -a sels
                sels=( ${(f)"$(_slacker_repo_names $cfg)"} ${(f)"$(_slacker_build_tags $cfg)"} )
                sels=( ${sels/#/@} )
                compadd -a sels
                return
            fi
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (remove)
            if [[ $PREFIX == @* ]]; then
                local -a sels
                sels=( ${(f)"$(_slacker_repo_names $cfg)"} ${(f)"$(_slacker_build_tags $cfg)"} )
                sels=( ${sels/#/@} )
                compadd -a sels
                return
            fi
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (revert-pkg)
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (download)
            if [[ $PREFIX == @* ]]; then
                local -a sels
                sels=( ${(f)"$(_slacker_repo_names $cfg)"} ${(f)"$(_slacker_build_tags $cfg)"} )
                sels=( ${sels/#/@} )
                compadd -a sels
                return
            fi
            ;;
        (install-new)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (clean-cache)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (show-changelog)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (history)
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (install-template)
            compadd ${(f)"$(_slacker_template_names $cfg)"}
            ;;
        (remove-template)
            compadd ${(f)"$(_slacker_template_names $cfg)"}
            ;;
        (delete-template)
            compadd ${(f)"$(_slacker_template_names $cfg)"}
            ;;
        (pin)
            if [[ $PREFIX != *:* ]]; then
                local -a repos
                repos=( ${(f)"$(_slacker_repo_names $cfg)"} )
                compadd -S ':' -- $repos
            fi
            ;;
        (unpin)
            compadd ${(f)"$(_slacker_installed_names)"}
            ;;
        (del-repo)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (pri-repo)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (vet-repo)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (trust-repo)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
        (distrust-repo)
            compadd ${(f)"$(_slacker_repo_names $cfg)"}
            ;;
    esac
}

_slacker "$@"
