# bash completion for rtadmin                              -*- shell-script -*-

_rtadmin_cache_dir() {
    local d="${XDG_CACHE_HOME:-$HOME/.cache}/rtadmin-completion"
    [[ -d $d ]] || mkdir -p "$d" 2>/dev/null
    printf '%s' "$d"
}

# Возвращает путь к бинарнику rtadmin (для проверки mtime)
_rtadmin_bin() {
    command -v rtadmin 2>/dev/null
}

_rtadmin_commands() {
    local cache bin
    cache="$(_rtadmin_cache_dir)/commands"
    bin="$(_rtadmin_bin)"

    if [[ ! -s $cache ]] || { [[ -n $bin ]] && [[ $bin -nt $cache ]]; }; then
        rtadmin --help 2>/dev/null \
            | awk '
                /^[A-Za-z].*commands:/ { in_cmds=1; next }
                in_cmds && /^[[:space:]]*$/ { in_cmds=0; next }
                in_cmds {
                    if ($0 ~ /^[[:space:]]+[a-z][a-z0-9-]+[[:space:]]/) {
                        print $1
                    }
                }
            ' > "$cache" 2>/dev/null
    fi
    [[ -s $cache ]] && cat "$cache"
}

_rtadmin_cmd_opts() {
    local cmd="$1"
    local cache bin
    cache="$(_rtadmin_cache_dir)/opts.$cmd"
    bin="$(_rtadmin_bin)"

    if [[ ! -s $cache ]] || { [[ -n $bin ]] && [[ $bin -nt $cache ]]; }; then
        rtadmin "$cmd" --help 2>&1 \
            | grep -oE -- '(^|[[:space:]])-{1,2}[A-Za-z][A-Za-z0-9_-]*' \
            | sed -E 's/^[[:space:]]+//' \
            | sort -u > "$cache" 2>/dev/null
    fi
    [[ -s $cache ]] && cat "$cache"
}

_rtadmin_enum_values() {
    local cmd="$1" opt="$2"
    rtadmin "$cmd" --help 2>&1 | awk -v opt="$opt" '
        BEGIN { found=0; invals=0 }
        index($0, opt) && !invals { found=1; next }
        found && /Possible values:|Allowed values:/ { invals=1; next }
        invals {
            if ($0 ~ /^[[:space:]]+[A-Za-z0-9_-]+[[:space:]]/) {
                val=$1
                sub(/[;,.].*$/, "", val)
                if ($0 ~ /^[[:space:]]*-/) { invals=0; found=0; next }
                print val
            } else if ($0 ~ /^[[:space:]]*$/) {
                invals=0; found=0
            }
        }
    ' | sort -u
}

_rtadmin()
{
    local cur prev words cword
    _init_completion -n =: || return

    local global_opts='--help -h --parallel --serial -s --exclude -e
                       --log-file --no-log --pin-file --no-pin-file'

    local cmd="" i
    for (( i=1; i < cword; i++ )); do
        case "${words[i]}" in
            -*) ;;
            *)  cmd="${words[i]}"; break ;;
        esac
    done

    if [[ -z $cmd ]]; then
        if [[ $cur == -* ]]; then
            COMPREPLY=( $(compgen -W "$global_opts" -- "$cur") )
        else
            COMPREPLY=( $(compgen -W "$(_rtadmin_commands)" -- "$cur") )
        fi
        return
    fi

    case "$prev" in
        -P|--auth-pin-input|--new-user-pin-input|--new-so-pin-input)
            COMPREPLY=( $(compgen -W "default console options" -- "$cur") )
            return
            ;;
        --pin-change-policy)
            COMPREPLY=( $(compgen -W "so user both" -- "$cur") )
            return
            ;;
        -p|--auth-pin|--new-user-pin|--new-so-pin|-l|--label|\
        --min-so-pin|--min-user-pin|--max-so-pin-retry-count|\
        --max-user-pin-retry-count|--serial|-s|--exclude|-e|\
        --log-file|--pin-file)
            return
            ;;
    esac

    if [[ $cur == -* ]]; then
        local cmd_opts
        cmd_opts="$(_rtadmin_cmd_opts "$cmd")"
        COMPREPLY=( $(compgen -W "$cmd_opts $global_opts" -- "$cur") )
        return
    fi

    COMPREPLY=( $(compgen -W "$(_rtadmin_cmd_opts "$cmd") $global_opts" -- "$cur") )
}

complete -F _rtadmin rtadmin
