#!/bin/bash

type _comp_contains &> /dev/null || return
type _comp_filter &> /dev/null || return
type _comp_filter_shorts &> /dev/null || return

function _wconv() {

    local cur prev words cword cline uac_flags ace_permissions permission_types no_completion group_one group_two opts
    _init_completion || return

    cline="${COMP_LINE}"
    COMPREPLY=()

    uac_flags="SCRIPT ACCOUNTDISABLE HOMEDIR_REQUIRED LOCKOUT PASSWD_NOTREQD PASSWD_CANT_CHANGE ENCRYPTED_TEXT_PWD_ALLOWED"
    uac_flags="$uac_flags TEMP_DUPLICATE_ACCOUNT NORMAL_ACCOUNT INTERDOMAIN_TRUST_ACCOUNT WORKSTATION_TRUST_ACCOUNT SERVER_TRUST_ACCOUNT"
    uac_flags="$uac_flags DONT_EXPIRE_PASSWORD MNS_LOGON_ACCOUNT SMARTCARD_REQUIRED TRUSTED_FOR_DELEGATION NOT_DELEGATED USE_DES_KEY_ONLY"
    uac_flags="$uac_flags DONT_REQ_PREAUTH PASSWORD_EXPIRED TRUSTED_TO_AUTH_FOR_DELEGATION PARTIAL_SECRETS_ACCOUNT"
    ace_permissions="GA GX GW GR SD RC WD WO CC DC LC SW RP WP DT LO CR FA FR FW FX KA KR KW KE"
    permission_types="file directory file_map registry service service_control process thread window_station desktop pipe token"

    if [[ $cword -eq 1 ]]; then

        if [[ $cur == -* ]]; then
            opts="-h --help"
        else
            opts="ace sddl sid uac"
        fi


    elif [[ ${words[1]} == "ace" ]]; then

        group_one='--from-string -t --toggle -v --verbose'
        group_two='--ace-flags --ace-permissions --ace-types --trustees -h --help'

        if [[ $prev == "-t" ]] || [[ $prev == "--toggle" ]]; then
            opts="$ace_permissions"

        elif [[ $prev == "--type" ]]; then
            opts="$permission_types"

        elif _comp_contains "$cline" "$group_two"; then

            opts="-h --help"
            if _comp_contains "$cline" "--ace-permissions"; then
                opts="$opts --type"
            fi

        elif _comp_contains "$cline" "$group_one"; then
            opts="$group_one --type -h --help"

        else
            opts=$($1 ace --help | _parse_help -)
        fi


    elif [[ ${words[1]} == "sddl" ]]; then

        group_one="-t --type -v --verbose"
        group_two="--add-everyone --add-anonymous -h --help"

        if [[ $prev == "-t" ]] || [[ $prev == "--type" ]]; then
            opts="$permission_types"

        elif _comp_contains "$cline" "$group_two"; then
            opts="-h --help"

        elif _comp_contains "$cline" "$group_one"; then
            opts="$group_one -h --help"

        else
            opts=$($1 sddl --help | _parse_help -)
        fi


    elif [[ ${words[1]} == "sid" ]]; then

        no_completion="--to-b64 --raw --well-known -h --help"

        if _comp_contains "$cline" "$no_completion"; then
            opts="-h --help"
        else
            opts=$($1 sid --help | _parse_help -)
        fi


    elif [[ ${words[1]} == "uac" ]]; then

        no_completion="--mapping -h --help"

        if [[ $prev == "-t" ]] || [[ $prev == "--toggle" ]]; then
            opts="$uac_flags"

        elif _comp_contains "$cline" "$no_completion"; then
            opts="-h --help"

        else
            opts=$($1 uac --help | _parse_help -)
        fi


    else
        opts=$(_parse_help "$1")
    fi


    _comp_filter "opts" "-t --toggle"
    _comp_filter_shorts "opts"
    
	mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") 
	return 0
}

complete -F _wconv wconv
