# CryptoPro certmgr completion

_certmgr_resolve_option()
{
    local value="${1}"
    shift
    local option=''
    local match=''
    local matches=0

    _certmgr_resolved_option=''

    # shift удалили первый аргумент из $@
    # В цикле перебирается только список известных опций
    for option in "$@" ; do
        if [[ "${option}" = "${value}"* ]] ; then
            match="${option}"
            matches=$((matches + 1))
        fi
    done

    if ((matches == 1)) ; then
        _certmgr_resolved_option="${match}"
        return 0
    fi

    return 1
}

_certmgr_completions()
{
    COMPREPLY=()
    modes=('-install' '-list' '-decode' '-export' '-delete' '-enumstores' '-updatestore')

    if [[ "${3}" == *'certmgr' ]] ; then
        COMPREPLY=($(compgen -W "${modes[*]}" -- "${2}"))
    else
        mode='unknown'
        for word in "${COMP_WORDS[@]}" ; do
            if _certmgr_resolve_option "${word}" "${modes[@]}" ; then
                mode="${_certmgr_resolved_option}"
                break
            fi
        done

        completions=()
        case "${mode}" in
            -install)
                completions=('-store' '-file' '-provname' '-provtype' '-container' '-to-container' \
                             '-ask-container' '-certificate' '-crl' '-pfx' '-pin' '-at_signature' '-all' \
                             '-silent' '-keep_exportable' '-protected' '-trace' '-tfmt' '-use-cont-ext')
                ;;
            -list)
                completions=('-store' '-file' '-container' '-at_signature' '-certificate' '-crl' '-request' \
                             '-pfx' '-dn' '-thumbprint' '-keyid' '-verbose' '-chain' '-stdin' '-pin')
                ;;
            -decode)
                completions=('-dest' '-src' '-der' '-base64')
                ;;
            -export)
                completions=('-store' '-dest' '-provname' '-provtype' '-certificate' '-crl' '-pfx' '-pin' '-cpin' \
                             '-container' '-dn' '-thumbprint' '-keyid' '-all' '-base64' '-at_signature' '-silent')
                ;;
            -delete)
                completions=('-store' '-dn' '-thumbprint' '-keyid' '-certificate' '-provname' '-provtype' \
                             '-crl' '-container' '-all' '-silent')
                ;;
            -enumstores)
                completions=('user' 'machine' 'all_locations')
                ;;
            -updatestore)
                completions=('-store' '-file' '-certificate' '-provname' '-provtype' '-crl')
                ;;
        esac

        if [[ "${mode}" = 'unknown' ]] ; then
            COMPREPLY=()
        else
            conflicts=()
            for ((i = 0; i < COMP_CWORD; i++)) ; do
                word="${COMP_WORDS[i]}"
                resolved_word="${word}"

                if _certmgr_resolve_option "${word}" "${completions[@]}" ; then
                    resolved_word="${_certmgr_resolved_option}"
                fi

                conflicts+=("${resolved_word}")
                case "${resolved_word}" in
                    -container) conflicts+=('-ask-container') ;;
                    -ask-container) conflicts+=('-container') ;;
                    -certificate) conflicts+=('-crl' '-pfx' '-request') ;;
                    -crl) conflicts+=('-certificate' '-pfx' '-request') ;;
                    -pfx) conflicts+=('-certificate' '-crl' '-request') ;;
                    -request) conflicts+=('-certificate' '-crl' '-pfx') ;;
                    -der) conflicts+=('-base64') ;;
                    -base64) conflicts+=('-der') ;;
                    -all) conflicts+=('-cpin') ;;
                    -cpin) conflicts+=('-all') ;;
                    user) conflicts+=('machine' 'all_locations') ;;
                    machine) conflicts+=('user' 'all_locations') ;;
                    all_locations) conflicts+=('user' 'machine') ;;
                esac
            done

            previous_option="${3}"
            if _certmgr_resolve_option "${3}" "${completions[@]}" ; then
                previous_option="${_certmgr_resolved_option}"
            fi

            case "${previous_option}" in
                -file|-src|-dest)
                    local IFS=$'\n'
                    COMPREPLY=($(compgen -f -- "${2}")) ;;
                -store|-provname|-provtype|-container|-to-container|-pin|-cpin|-dn|-thumbprint|-keyid)
                    COMPREPLY=() ;;
                *)
                    ans=''
                    for completion in "${completions[@]}" ; do
                        skip=false
                        for conflict in "${conflicts[@]}" ; do
                            if [[ "${completion}" = "${conflict}" ]] ; then
                                skip=true
                                break
                            fi
                        done
                        if ! $skip ; then
                            ans="${ans} ${completion}"
                        fi
                    done
                    COMPREPLY=($(compgen -W "${ans}" -- "${2}"))
                    if [[ -n "${2}" && "${2}" != -* ]] ; then
                        local IFS=$'\n'
                        COMPREPLY+=($(compgen -f -- "${2}"))
                    fi ;;
            esac
        fi
    fi
}

complete -o filenames -F _certmgr_completions certmgr
