# bash completion for dmount

_dmount_cli_share_names() {
    local config="${1:-/etc/security/pam_mount.conf.xml}"
    dmount list -c "$config" 2>/dev/null | tail -n +3 | awk '{print $1}'
}

_dmount_cli_disabled_names() {
    local config="${1:-/etc/security/pam_mount.conf.xml}"
    dmount list --disabled -c "$config" 2>/dev/null | tail -n +3 | awk '{print $1}'
}

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

    local commands="list show add edit delete disable enable test info"
    local security_values="krb5 krb5i krb5p"
    local type_values="cifs smb"

    # Find the subcommand
    local cmd=""
    local i
    for ((i=1; i < cword; i++)); do
        case "${words[i]}" in
            -c|--config) ((i++)); continue ;;
            -*) continue ;;
            list|show|add|edit|delete|disable|enable|test|info) cmd="${words[i]}"; break ;;
        esac
    done

    # Find config path if specified
    local config="/etc/security/pam_mount.conf.xml"
    for ((i=1; i < cword; i++)); do
        if [[ "${words[i]}" == "-c" || "${words[i]}" == "--config" ]]; then
            config="${words[i+1]}"
            break
        fi
    done

    # Complete subcommand
    if [[ -z "$cmd" ]]; then
        case "$prev" in
            -c|--config) _filedir; return ;;
        esac
        if [[ "$cur" == -* ]]; then
            COMPREPLY=($(compgen -W "-c --config -V --version -h --help" -- "$cur"))
        else
            COMPREPLY=($(compgen -W "$commands" -- "$cur"))
        fi
        return
    fi

    # Complete option values
    case "$prev" in
        -c|--config) _filedir; return ;;
        -S|--security) COMPREPLY=($(compgen -W "$security_values" -- "$cur")); return ;;
        -t|--type) COMPREPLY=($(compgen -W "$type_values" -- "$cur")); return ;;
        -s|--server|-m|--mount-point|-o|--extra-options|-u|--users|-g|--groups|--path)
            return ;;
    esac

    # Complete per subcommand
    case "$cmd" in
        list)
            COMPREPLY=($(compgen -W "--disabled -c --config -h --help" -- "$cur"))
            ;;
        show)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "-c --config -h --help" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(_dmount_cli_share_names "$config")" -- "$cur"))
            fi
            ;;
        add)
            COMPREPLY=($(compgen -W "-m --mount-point -t --type -S --security -u --users -g --groups --icase --no-icase -o --extra-options -c --config -h --help" -- "$cur"))
            ;;
        edit)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "-s --server --path -m --mount-point -t --type -S --security -u --users -g --groups --icase --no-icase -o --extra-options -c --config -h --help" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(_dmount_cli_share_names "$config")" -- "$cur"))
            fi
            ;;
        delete)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--all -c --config -h --help" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(_dmount_cli_share_names "$config")" -- "$cur"))
            fi
            ;;
        disable)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--all -c --config -h --help" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(_dmount_cli_share_names "$config")" -- "$cur"))
            fi
            ;;
        enable)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--all -c --config -h --help" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "$(_dmount_cli_disabled_names "$config")" -- "$cur"))
            fi
            ;;
        test)
            COMPREPLY=($(compgen -W "-t --type -S --security -o --extra-options -c --config -h --help" -- "$cur"))
            ;;
        info)
            COMPREPLY=($(compgen -W "-c --config -h --help" -- "$cur"))
            ;;
    esac
}

complete -F _dmount_cli dmount
