# bash completion for cilium                               -*- shell-script -*-

__debug()
{
    if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
        echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
    fi
}

# Homebrew on Macs have version 1.3 of bash-completion which doesn't include
# _init_completion. This is a very minimal version of that function.
__my_init_completion()
{
    COMPREPLY=()
    _get_comp_words_by_ref "$@" cur prev words cword
}

__index_of_word()
{
    local w word=$1
    shift
    index=0
    for w in "$@"; do
        [[ $w = "$word" ]] && return
        index=$((index+1))
    done
    index=-1
}

__contains_word()
{
    local w word=$1; shift
    for w in "$@"; do
        [[ $w = "$word" ]] && return
    done
    return 1
}

__handle_reply()
{
    __debug "${FUNCNAME[0]}"
    case $cur in
        -*)
            if [[ $(type -t compopt) = "builtin" ]]; then
                compopt -o nospace
            fi
            local allflags
            if [ ${#must_have_one_flag[@]} -ne 0 ]; then
                allflags=("${must_have_one_flag[@]}")
            else
                allflags=("${flags[*]} ${two_word_flags[*]}")
            fi
            COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )
            if [[ $(type -t compopt) = "builtin" ]]; then
                [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace
            fi

            # complete after --flag=abc
            if [[ $cur == *=* ]]; then
                if [[ $(type -t compopt) = "builtin" ]]; then
                    compopt +o nospace
                fi

                local index flag
                flag="${cur%%=*}"
                __index_of_word "${flag}" "${flags_with_completion[@]}"
                if [[ ${index} -ge 0 ]]; then
                    COMPREPLY=()
                    PREFIX=""
                    cur="${cur#*=}"
                    ${flags_completion[${index}]}
                    if [ -n "${ZSH_VERSION}" ]; then
                        # zfs completion needs --flag= prefix
                        eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"
                    fi
                fi
            fi
            return 0;
            ;;
    esac

    # check if we are handling a flag with special work handling
    local index
    __index_of_word "${prev}" "${flags_with_completion[@]}"
    if [[ ${index} -ge 0 ]]; then
        ${flags_completion[${index}]}
        return
    fi

    # we are parsing a flag and don't have a special handler, no completion
    if [[ ${cur} != "${words[cword]}" ]]; then
        return
    fi

    local completions
    completions=("${commands[@]}")
    if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
        completions=("${must_have_one_noun[@]}")
    fi
    if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
        completions+=("${must_have_one_flag[@]}")
    fi
    COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )

    if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
        COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
    fi

    if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
        declare -F __custom_func >/dev/null && __custom_func
    fi

    __ltrim_colon_completions "$cur"
}

# The arguments should be in the form "ext1|ext2|extn"
__handle_filename_extension_flag()
{
    local ext="$1"
    _filedir "@(${ext})"
}

__handle_subdirs_in_dir_flag()
{
    local dir="$1"
    pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
}

__handle_flag()
{
    __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"

    # if a command required a flag, and we found it, unset must_have_one_flag()
    local flagname=${words[c]}
    local flagvalue
    # if the word contained an =
    if [[ ${words[c]} == *"="* ]]; then
        flagvalue=${flagname#*=} # take in as flagvalue after the =
        flagname=${flagname%%=*} # strip everything after the =
        flagname="${flagname}=" # but put the = back
    fi
    __debug "${FUNCNAME[0]}: looking for ${flagname}"
    if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then
        must_have_one_flag=()
    fi

    # if you set a flag which only applies to this command, don't show subcommands
    if __contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
      commands=()
    fi

    # keep flag value with flagname as flaghash
    if [ -n "${flagvalue}" ] ; then
        flaghash[${flagname}]=${flagvalue}
    elif [ -n "${words[ $((c+1)) ]}" ] ; then
        flaghash[${flagname}]=${words[ $((c+1)) ]}
    else
        flaghash[${flagname}]="true" # pad "true" for bool flag
    fi

    # skip the argument to a two word flag
    if __contains_word "${words[c]}" "${two_word_flags[@]}"; then
        c=$((c+1))
        # if we are looking for a flags value, don't show commands
        if [[ $c -eq $cword ]]; then
            commands=()
        fi
    fi

    c=$((c+1))

}

__handle_noun()
{
    __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"

    if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then
        must_have_one_noun=()
    elif __contains_word "${words[c]}" "${noun_aliases[@]}"; then
        must_have_one_noun=()
    fi

    nouns+=("${words[c]}")
    c=$((c+1))
}

__handle_command()
{
    __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"

    local next_command
    if [[ -n ${last_command} ]]; then
        next_command="_${last_command}_${words[c]//:/__}"
    else
        if [[ $c -eq 0 ]]; then
            next_command="_$(basename "${words[c]//:/__}")"
        else
            next_command="_${words[c]//:/__}"
        fi
    fi
    c=$((c+1))
    __debug "${FUNCNAME[0]}: looking for ${next_command}"
    declare -F $next_command >/dev/null && $next_command
}

__handle_word()
{
    if [[ $c -ge $cword ]]; then
        __handle_reply
        return
    fi
    __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
    if [[ "${words[c]}" == -* ]]; then
        __handle_flag
    elif __contains_word "${words[c]}" "${commands[@]}"; then
        __handle_command
    elif [[ $c -eq 0 ]] && __contains_word "$(basename "${words[c]}")" "${commands[@]}"; then
        __handle_command
    else
        __handle_noun
    fi
    __handle_word
}

_cilium_bpf_ct_flush()
{
    last_command="cilium_bpf_ct_flush"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_ct_list()
{
    last_command="cilium_bpf_ct_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_ct()
{
    last_command="cilium_bpf_ct"
    commands=()
    commands+=("flush")
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_endpoint_delete()
{
    last_command="cilium_bpf_endpoint_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_endpoint_list()
{
    last_command="cilium_bpf_endpoint_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_endpoint()
{
    last_command="cilium_bpf_endpoint"
    commands=()
    commands+=("delete")
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_ipcache_list()
{
    last_command="cilium_bpf_ipcache_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_ipcache()
{
    last_command="cilium_bpf_ipcache"
    commands=()
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_lb_list()
{
    last_command="cilium_bpf_lb_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--revnat")
    local_nonpersistent_flags+=("--revnat")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_lb()
{
    last_command="cilium_bpf_lb"
    commands=()
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_metrics_list()
{
    last_command="cilium_bpf_metrics_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_metrics()
{
    last_command="cilium_bpf_metrics"
    commands=()
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_policy_add()
{
    last_command="cilium_bpf_policy_add"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_policy_delete()
{
    last_command="cilium_bpf_policy_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_policy_get()
{
    last_command="cilium_bpf_policy_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--numeric")
    flags+=("-n")
    local_nonpersistent_flags+=("--numeric")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_policy()
{
    last_command="cilium_bpf_policy"
    commands=()
    commands+=("add")
    commands+=("delete")
    commands+=("get")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_proxy_flush()
{
    last_command="cilium_bpf_proxy_flush"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_proxy_list()
{
    last_command="cilium_bpf_proxy_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_proxy()
{
    last_command="cilium_bpf_proxy"
    commands=()
    commands+=("flush")
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_tunnel_list()
{
    last_command="cilium_bpf_tunnel_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf_tunnel()
{
    last_command="cilium_bpf_tunnel"
    commands=()
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_bpf()
{
    last_command="cilium_bpf"
    commands=()
    commands+=("ct")
    commands+=("endpoint")
    commands+=("ipcache")
    commands+=("lb")
    commands+=("metrics")
    commands+=("policy")
    commands+=("proxy")
    commands+=("tunnel")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_cleanup()
{
    last_command="cilium_cleanup"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--force")
    flags+=("-f")
    local_nonpersistent_flags+=("--force")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_completion()
{
    last_command="cilium_completion"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    must_have_one_noun+=("bash")
    noun_aliases=()
}

_cilium_config()
{
    last_command="cilium_config"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--list-options")
    local_nonpersistent_flags+=("--list-options")
    flags+=("--num-pages=")
    two_word_flags+=("-n")
    local_nonpersistent_flags+=("--num-pages=")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_debuginfo()
{
    last_command="cilium_debuginfo"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--file=")
    two_word_flags+=("-f")
    local_nonpersistent_flags+=("--file=")
    flags+=("--file-per-command")
    local_nonpersistent_flags+=("--file-per-command")
    flags+=("--html-file=")
    local_nonpersistent_flags+=("--html-file=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_config()
{
    last_command="cilium_endpoint_config"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--list-options")
    local_nonpersistent_flags+=("--list-options")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_disconnect()
{
    last_command="cilium_endpoint_disconnect"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_get()
{
    last_command="cilium_endpoint_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--labels=")
    two_word_flags+=("-l")
    local_nonpersistent_flags+=("--labels=")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_health()
{
    last_command="cilium_endpoint_health"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_labels()
{
    last_command="cilium_endpoint_labels"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--add=")
    two_word_flags+=("-a")
    local_nonpersistent_flags+=("--add=")
    flags+=("--delete=")
    two_word_flags+=("-d")
    local_nonpersistent_flags+=("--delete=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_list()
{
    last_command="cilium_endpoint_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--no-headers")
    local_nonpersistent_flags+=("--no-headers")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_log()
{
    last_command="cilium_endpoint_log"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint_regenerate()
{
    last_command="cilium_endpoint_regenerate"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_endpoint()
{
    last_command="cilium_endpoint"
    commands=()
    commands+=("config")
    commands+=("disconnect")
    commands+=("get")
    commands+=("health")
    commands+=("labels")
    commands+=("list")
    commands+=("log")
    commands+=("regenerate")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_identity_get()
{
    last_command="cilium_identity_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--label=")
    local_nonpersistent_flags+=("--label=")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_identity_list()
{
    last_command="cilium_identity_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_identity()
{
    last_command="cilium_identity"
    commands=()
    commands+=("get")
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_kvstore_delete()
{
    last_command="cilium_kvstore_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--recursive")
    local_nonpersistent_flags+=("--recursive")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")
    flags+=("--kvstore=")
    flags+=("--kvstore-opt=")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_kvstore_get()
{
    last_command="cilium_kvstore_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--recursive")
    local_nonpersistent_flags+=("--recursive")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")
    flags+=("--kvstore=")
    flags+=("--kvstore-opt=")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_kvstore_set()
{
    last_command="cilium_kvstore_set"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--key=")
    local_nonpersistent_flags+=("--key=")
    flags+=("--value=")
    local_nonpersistent_flags+=("--value=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")
    flags+=("--kvstore=")
    flags+=("--kvstore-opt=")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_kvstore()
{
    last_command="cilium_kvstore"
    commands=()
    commands+=("delete")
    commands+=("get")
    commands+=("set")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--kvstore=")
    flags+=("--kvstore-opt=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_map_get()
{
    last_command="cilium_map_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_map_list()
{
    last_command="cilium_map_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--verbose")
    local_nonpersistent_flags+=("--verbose")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_map()
{
    last_command="cilium_map"
    commands=()
    commands+=("get")
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_monitor()
{
    last_command="cilium_monitor"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--from=")
    local_nonpersistent_flags+=("--from=")
    flags+=("--hex")
    local_nonpersistent_flags+=("--hex")
    flags+=("--json")
    flags+=("-j")
    local_nonpersistent_flags+=("--json")
    flags+=("--related-to=")
    local_nonpersistent_flags+=("--related-to=")
    flags+=("--to=")
    local_nonpersistent_flags+=("--to=")
    flags+=("--type=")
    two_word_flags+=("-t")
    local_nonpersistent_flags+=("--type=")
    flags+=("--verbose")
    flags+=("-v")
    local_nonpersistent_flags+=("--verbose")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_node_list()
{
    last_command="cilium_node_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_node()
{
    last_command="cilium_node"
    commands=()
    commands+=("list")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_delete()
{
    last_command="cilium_policy_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--all")
    local_nonpersistent_flags+=("--all")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_get()
{
    last_command="cilium_policy_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_import()
{
    last_command="cilium_policy_import"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--print")
    local_nonpersistent_flags+=("--print")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_trace()
{
    last_command="cilium_policy_trace"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--dport=")
    local_nonpersistent_flags+=("--dport=")
    flags+=("--dst=")
    two_word_flags+=("-d")
    local_nonpersistent_flags+=("--dst=")
    flags+=("--dst-endpoint=")
    local_nonpersistent_flags+=("--dst-endpoint=")
    flags+=("--dst-identity=")
    local_nonpersistent_flags+=("--dst-identity=")
    flags+=("--dst-k8s-pod=")
    local_nonpersistent_flags+=("--dst-k8s-pod=")
    flags+=("--dst-k8s-yaml=")
    local_nonpersistent_flags+=("--dst-k8s-yaml=")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--src=")
    two_word_flags+=("-s")
    local_nonpersistent_flags+=("--src=")
    flags+=("--src-endpoint=")
    local_nonpersistent_flags+=("--src-endpoint=")
    flags+=("--src-identity=")
    local_nonpersistent_flags+=("--src-identity=")
    flags+=("--src-k8s-pod=")
    local_nonpersistent_flags+=("--src-k8s-pod=")
    flags+=("--src-k8s-yaml=")
    local_nonpersistent_flags+=("--src-k8s-yaml=")
    flags+=("--verbose")
    flags+=("-v")
    local_nonpersistent_flags+=("--verbose")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_validate()
{
    last_command="cilium_policy_validate"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--print")
    local_nonpersistent_flags+=("--print")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy_wait()
{
    last_command="cilium_policy_wait"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--fail-wait-time=")
    local_nonpersistent_flags+=("--fail-wait-time=")
    flags+=("--max-wait-time=")
    local_nonpersistent_flags+=("--max-wait-time=")
    flags+=("--sleep-time=")
    local_nonpersistent_flags+=("--sleep-time=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_policy()
{
    last_command="cilium_policy"
    commands=()
    commands+=("delete")
    commands+=("get")
    commands+=("import")
    commands+=("trace")
    commands+=("validate")
    commands+=("wait")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_prefilter_delete()
{
    last_command="cilium_prefilter_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--cidr=")
    local_nonpersistent_flags+=("--cidr=")
    flags+=("--revision=")
    local_nonpersistent_flags+=("--revision=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_prefilter_list()
{
    last_command="cilium_prefilter_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_prefilter_update()
{
    last_command="cilium_prefilter_update"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--cidr=")
    local_nonpersistent_flags+=("--cidr=")
    flags+=("--revision=")
    local_nonpersistent_flags+=("--revision=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_prefilter()
{
    last_command="cilium_prefilter"
    commands=()
    commands+=("delete")
    commands+=("list")
    commands+=("update")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_service_delete()
{
    last_command="cilium_service_delete"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--all")
    local_nonpersistent_flags+=("--all")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_service_get()
{
    last_command="cilium_service_get"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_service_list()
{
    last_command="cilium_service_list"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_service_update()
{
    last_command="cilium_service_update"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--backends=")
    local_nonpersistent_flags+=("--backends=")
    flags+=("--frontend=")
    local_nonpersistent_flags+=("--frontend=")
    flags+=("--id=")
    local_nonpersistent_flags+=("--id=")
    flags+=("--rev")
    local_nonpersistent_flags+=("--rev")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_service()
{
    last_command="cilium_service"
    commands=()
    commands+=("delete")
    commands+=("get")
    commands+=("list")
    commands+=("update")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_status()
{
    last_command="cilium_status"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--all-addresses")
    local_nonpersistent_flags+=("--all-addresses")
    flags+=("--all-controllers")
    local_nonpersistent_flags+=("--all-controllers")
    flags+=("--all-health")
    local_nonpersistent_flags+=("--all-health")
    flags+=("--all-nodes")
    local_nonpersistent_flags+=("--all-nodes")
    flags+=("--all-redirects")
    local_nonpersistent_flags+=("--all-redirects")
    flags+=("--brief")
    local_nonpersistent_flags+=("--brief")
    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--verbose")
    local_nonpersistent_flags+=("--verbose")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium_version()
{
    last_command="cilium_version"
    commands=()

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--output=")
    two_word_flags+=("-o")
    local_nonpersistent_flags+=("--output=")
    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

_cilium()
{
    last_command="cilium"
    commands=()
    commands+=("bpf")
    commands+=("cleanup")
    commands+=("completion")
    commands+=("config")
    commands+=("debuginfo")
    commands+=("endpoint")
    commands+=("identity")
    commands+=("kvstore")
    commands+=("map")
    commands+=("monitor")
    commands+=("node")
    commands+=("policy")
    commands+=("prefilter")
    commands+=("service")
    commands+=("status")
    commands+=("version")

    flags=()
    two_word_flags=()
    local_nonpersistent_flags=()
    flags_with_completion=()
    flags_completion=()

    flags+=("--config=")
    flags+=("--debug")
    flags+=("-D")
    flags+=("--host=")
    two_word_flags+=("-H")

    must_have_one_flag=()
    must_have_one_noun=()
    noun_aliases=()
}

__start_cilium()
{
    local cur prev words cword
    declare -A flaghash 2>/dev/null || :
    if declare -F _init_completion >/dev/null 2>&1; then
        _init_completion -s || return
    else
        __my_init_completion -n "=" || return
    fi

    local c=0
    local flags=()
    local two_word_flags=()
    local local_nonpersistent_flags=()
    local flags_with_completion=()
    local flags_completion=()
    local commands=("cilium")
    local must_have_one_flag=()
    local must_have_one_noun=()
    local last_command
    local nouns=()

    __handle_word
}

if [[ $(type -t compopt) = "builtin" ]]; then
    complete -o default -F __start_cilium cilium
else
    complete -o default -o nospace -F __start_cilium cilium
fi

# ex: ts=4 sw=4 et filetype=sh

