# CryptoPro certmgr completion

_certmgr_completions()
{
    modes=('-install' '-list' '-decode' '-export' '-delete' '-enumstores' '-updatestore')
    if [[ "${3}" == *'certmgr' ]] ; then
        COMPREPLY=($(compgen -W '-install -list -decode -export -delete -enumstores -updatestore' -- "${2}"))
    else
        mode='unknown'
        for word in "${COMP_WORDS[@]}" ; do
            for mode in "${modes[@]}" ; do
                if [[ "${word}" = "${mode}" ]] ; then
                    break
                fi
            done
            if [[ "${word}" = "${mode}" ]] ; then
                break
            fi
        done

        conflicts=()
        for word in "${COMP_WORDS[@]}" ; do
            conflicts+=("${word}")
            if [[ "${word}" = '-container' ]] ; then
                conflicts+=('-ask-container')
            fi
            if [[ "${word}" = '-certificate' ]] ; then
                conflicts+=('-crl' '-pfx' '-pkcs10')
            fi
            if [[ "${word}" = '-crl' ]] ; then
                conflicts+=('-certificate' '-pfx' '-pkcs10')
            fi
            if [[ "${word}" = '-pfx' ]] ; then
                conflicts+=('-certificate' '-crl' '-pkcs10')
            fi
            if [[ "${word}" = '-pkcs10' ]] ; then
                conflicts+=('-certificate' '-crl' '-pfx')
            fi
            if [[ "${word}" = '-der' ]] ; then
                conflicts+=('-base64')
            fi
            if [[ "${word}" = '-base64' ]] ; then
                conflicts+=('-der')
            fi
            if [[ "${word}" = '-ask-container' ]] ; then
                conflicts+=('-container')
            fi
            if [[ "${word}" = 'user' ]] ; then
                conflicts+=('machine' 'all_locations')
            fi
            if [[ "${word}" = 'machine' ]] ; then
                conflicts+=('user' 'all_locations')
            fi
            if [[ "${word}" = 'all_locations' ]] ; then
                conflicts+=('user' 'machine')
            fi
        done

        completions=()
        if [[ "${mode}" = 'unknown' ]] ; then
            :
        elif [[ "${mode}" = '-install' ]] ; then
            completions=('-store' '-file' '-provname' '-provtype' '-container' '-ask-container' '-certificate' '-crl' '-pfx' '-pin' '-at_signature' '-all' '-silent' '-keep_exportable' '-protected' '-trace' '-tfmt')
        elif [[ "${mode}" = '-list' ]] ; then
            completions=('-store' '-file' '-container' '-at_signature' '-certificate' '-crl' '-pkcs10' '-dn' '-thumbprint' '-keyid' '-verbose' '-chain' '-stdin')
        elif [[ "${mode}" = '-decode' ]] ; then
            completions=('-dest' '-src' '-der' '-base64')
        elif [[ "${mode}" = '-export' ]] ; then
            completions=('-store' '-dest' '-provname' '-provtype' '-certificate' '-crl' '-pfx' '-pin' '-container' '-dn' '-thumbprint' '-keyid' '-all' '-base64' '-at_signature' '-silent')
        elif [[ "${mode}" = '-delete' ]] ; then
            completions=('-store' '-dn' '-thumbprint' '-keyid' '-certificate' '-provname' '-provtype' '-crl' '-container' '-all' '-silent')
        elif [[ "${mode}" = '-enumstores' ]] ; then
            completions=('user' 'machine' 'all_locations')
        elif [[ "${mode}" = '-updatestore' ]] ; then
            completions=('-store' '-file' '-certificate' '-provname' '-provtype' '-crl')
        else
            :
        fi

        if [[ "${3}" = 'unknown' ]] ; then
            :
        elif [[ "${3}" = '-store' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-file' ]] ; then
            COMPREPLY=($(compgen -f -- "${2}"))
        elif [[ "${3}" = '-provname' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-provtype' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-container' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-pin' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-dn' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-thumbprint' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-keyid' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-src' ]] ; then
            COMPREPLY=($(compgen -f -- "${2}"))
        elif [[ "${3}" = '-dest' ]] ; then
            COMPREPLY=($(compgen -f -- "${2}"))
        elif [[ "${3}" = '-trace' ]] ; then
            COMPREPLY=($())
        elif [[ "${3}" = '-tfmt' ]] ; then
            COMPREPLY=($())
        else
            ans=''
            for completion in "${completions[@]}" ; do
                for conflict in "${conflicts[@]}" ; do
                    if [[ "${completion}" = "${conflict}" ]] ; then
                        break
                    fi
                done
                if [[ "${completion}" != "${conflict}" ]] ; then
                    ans="${ans} ${completion}"
                fi
            done
            COMPREPLY=($(compgen -W "${ans}" -- "${2}"))
        fi
    fi
}

complete -o filenames -F _certmgr_completions certmgr
