#compdef dmount

_dmount_cli_share_names() {
    local config="${opt_args[-c]:-${opt_args[--config]:-/etc/security/pam_mount.conf.xml}}"
    local -a names
    names=(${(f)"$(dmount list -c "$config" 2>/dev/null | tail -n +3 | awk '{print $1}')"})
    _describe 'share name' names
}

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

_dmount_cli() {
    local -a commands=(
        'list:list configured shares'
        'show:show details of a share'
        'add:add a new share'
        'edit:edit an existing share'
        'delete:delete shares'
        'disable:disable (comment out) shares'
        'enable:enable (uncomment) shares'
        'test:test mount connectivity'
        'info:show domain and NSS backend info'
    )

    local -a global_opts=(
        '(-c --config)'{-c,--config}'[path to pam_mount config]:config file:_files'
        '(-V --version)'{-V,--version}'[show version]'
        '(-h --help)'{-h,--help}'[show help]'
    )

    local -a security_values=(krb5 krb5i krb5p)
    local -a type_values=(cifs smb)

    _arguments -C \
        $global_opts \
        '1:command:->command' \
        '*::arg:->args' && return

    case $state in
        command)
            _describe 'command' commands
            ;;
        args)
            case $words[1] in
                list)
                    _arguments \
                        $global_opts \
                        '(--disabled)'--disabled'[list disabled (commented-out) shares]'
                    ;;
                show)
                    _arguments \
                        $global_opts \
                        '1:share name:_dmount_cli_share_names'
                    ;;
                add)
                    _arguments \
                        $global_opts \
                        '1:share name:' \
                        '2:server:_hosts' \
                        '3:share path:' \
                        '(-m --mount-point)'{-m,--mount-point}'[mount point]:mount point:_files -/' \
                        '(-t --type)'{-t,--type}'[share type]:type:('"${type_values[*]}"')' \
                        '(-S --security)'{-S,--security}'[auth method]:security:('"${security_values[*]}"')' \
                        '(-u --users)'{-u,--users}'[target users]:users:' \
                        '(-g --groups)'{-g,--groups}'[target groups]:groups:' \
                        '(--icase --no-icase)'--icase'[case-insensitive user/group matching]' \
                        '(--icase --no-icase)'--no-icase'[case-sensitive user/group matching]' \
                        '(-o --extra-options)'{-o,--extra-options}'[extra mount options]:options:'
                    ;;
                edit)
                    _arguments \
                        $global_opts \
                        '1:share name:_dmount_cli_share_names' \
                        '(-s --server)'{-s,--server}'[server address]:server:_hosts' \
                        '(--path)'--path'[share path]:path:' \
                        '(-m --mount-point)'{-m,--mount-point}'[mount point]:mount point:_files -/' \
                        '(-t --type)'{-t,--type}'[share type]:type:('"${type_values[*]}"')' \
                        '(-S --security)'{-S,--security}'[auth method]:security:('"${security_values[*]}"')' \
                        '(-u --users)'{-u,--users}'[target users]:users:' \
                        '(-g --groups)'{-g,--groups}'[target groups]:groups:' \
                        '(--icase --no-icase)'--icase'[case-insensitive user/group matching]' \
                        '(--icase --no-icase)'--no-icase'[case-sensitive user/group matching]' \
                        '(-o --extra-options)'{-o,--extra-options}'[extra mount options]:options:'
                    ;;
                delete)
                    _arguments \
                        $global_opts \
                        '(--all)'--all'[delete all managed shares]' \
                        '1:share names:_dmount_cli_share_names'
                    ;;
                disable)
                    _arguments \
                        $global_opts \
                        '(--all)'--all'[disable all managed shares]' \
                        '1:share names:_dmount_cli_share_names'
                    ;;
                enable)
                    _arguments \
                        $global_opts \
                        '(--all)'--all'[enable all disabled shares]' \
                        '1:share names:_dmount_cli_disabled_names'
                    ;;
                test)
                    _arguments \
                        $global_opts \
                        '1:server:_hosts' \
                        '2:share path:' \
                        '(-t --type)'{-t,--type}'[share type]:type:('"${type_values[*]}"')' \
                        '(-S --security)'{-S,--security}'[auth method]:security:('"${security_values[*]}"')' \
                        '(-o --extra-options)'{-o,--extra-options}'[extra mount options]:options:'
                    ;;
                info)
                    _arguments $global_opts
                    ;;
            esac
            ;;
    esac
}

_dmount_cli "$@"
