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

__hcloud_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.
__hcloud_init_completion()
{
    COMPREPLY=()
    _get_comp_words_by_ref "$@" cur prev words cword
}

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

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

__hcloud_handle_reply()
{
    __hcloud_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%=*}"
                __hcloud_index_of_word "${flag}" "${flags_with_completion[@]}"
                COMPREPLY=()
                if [[ ${index} -ge 0 ]]; then
                    PREFIX=""
                    cur="${cur#*=}"
                    ${flags_completion[${index}]}
                    if [ -n "${ZSH_VERSION}" ]; then
                        # zsh 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
    __hcloud_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

    # available in bash-completion >= 2, not always present on macOS
    if declare -F __ltrim_colon_completions >/dev/null; then
        __ltrim_colon_completions "$cur"
    fi

    # If there is only 1 completion and it is a flag with an = it will be completed
    # but we don't want a space after the =
    if [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ $(type -t compopt) = "builtin" ]] && [[ "${COMPREPLY[0]}" == --*= ]]; then
       compopt -o nospace
    fi
}

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

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

__hcloud_handle_flag()
{
    __hcloud_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
    __hcloud_debug "${FUNCNAME[0]}: looking for ${flagname}"
    if __hcloud_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 __hcloud_contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
      commands=()
    fi

    # keep flag value with flagname as flaghash
    # flaghash variable is an associative array which is only supported in bash > 3.
    if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
        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
    fi

    # skip the argument to a two word flag
    if __hcloud_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))

}

__hcloud_handle_noun()
{
    __hcloud_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"

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

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

__hcloud_handle_command()
{
    __hcloud_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="_hcloud_root_command"
        else
            next_command="_${words[c]//:/__}"
        fi
    fi
    c=$((c+1))
    __hcloud_debug "${FUNCNAME[0]}: looking for ${next_command}"
    declare -F "$next_command" >/dev/null && $next_command
}

__hcloud_handle_word()
{
    if [[ $c -ge $cword ]]; then
        __hcloud_handle_reply
        return
    fi
    __hcloud_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
    if [[ "${words[c]}" == -* ]]; then
        __hcloud_handle_flag
    elif __hcloud_contains_word "${words[c]}" "${commands[@]}"; then
        __hcloud_handle_command
    elif [[ $c -eq 0 ]]; then
        __hcloud_handle_command
    else
        __hcloud_handle_noun
    fi
    __hcloud_handle_word
}


	__hcloud_sshkey_names() {
		local ctl_output out
		if ctl_output=$(hcloud ssh-key list -o noheader -o columns=name 2>/dev/null); then
			IFS=$'\n'
			COMPREPLY=($(echo "${ctl_output}" | while read -r line; do printf "%q\n" "$line"; done))
		fi
	}

	__hcloud_context_names() {
		local ctl_output out
		if ctl_output=$(hcloud context list -o noheader 2>/dev/null); then
			IFS=$'\n'
			COMPREPLY=($(echo "${ctl_output}" | while read -r line; do printf "%q\n" "$line"; done))
		fi
	}

	__hcloud_floatingip_ids() {
		local ctl_output out
		if ctl_output=$(hcloud floating-ip list -o noheader -o columns=id 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_iso_names() {
		local ctl_output out
		if ctl_output=$(hcloud iso list -o noheader -o columns=name 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_datacenter_names() {
		local ctl_output out
		if ctl_output=$(hcloud datacenter list -o noheader -o columns=name 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_location_names() {
		local ctl_output out
		if ctl_output=$(hcloud location list -o noheader -o columns=name 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_server_names() {
		local ctl_output out
		if ctl_output=$(hcloud server list -o noheader -o columns=name 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_servertype_names() {
		local ctl_output out
		if ctl_output=$(hcloud server-type list -o noheader -o columns=name 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}"))
		fi
	}

	__hcloud_image_ids_no_system() {
		local ctl_output out
		if ctl_output=$(hcloud image list -o noheader 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}" | awk '{if ($2 != "system") {print $1}}'))
		fi
	}

	__hcloud_image_names() {
		local ctl_output out
		if ctl_output=$(hcloud image list -o noheader 2>/dev/null); then
				COMPREPLY=($(echo "${ctl_output}" | awk '{if ($3 == "-") {print $1} else {print $3}}'))
		fi
	}

	__hcloud_floating_ip_ids() {
		local ctl_output out
		if ctl_output=$(hcloud floating-ip list -o noheader 2>/dev/null); then
			COMPREPLY=($(echo "${ctl_output}" | awk '{print $1}'))
		fi
	}

	__hcloud_image_types_no_system() {
		COMPREPLY=($(echo "snapshot backup"))
	}

	__hcloud_protection_levels() {
		COMPREPLY=($(echo "delete"))
	}

	__hcloud_server_protection_levels() {
		COMPREPLY=($(echo "delete rebuild"))
	}

	__hcloud_floatingip_types() {
		COMPREPLY=($(echo "ipv4 ipv6"))
	}

	__hcloud_backup_windows() {
		COMPREPLY=($(echo "22-02 02-06 06-10 10-14 14-18 18-22"))
	}

	__hcloud_rescue_types() {
		COMPREPLY=($(echo "linux64 linux32 freebsd64"))
	}

	__custom_func() {
		case ${last_command} in
			hcloud_server_delete | hcloud_server_describe | \
			hcloud_server_create-image | hcloud_server_poweron | \
			hcloud_server_poweroff | hcloud_server_reboot | \
			hcloud_server_reset | hcloud_server_reset-password | \
			hcloud_server_shutdown | hcloud_server_disable-rescue | \
			hcloud_server_enable-rescue | hcloud_server_detach-iso | \
			hcloud_server_update | hcloud_server_enable-backup | \
			hcloud_server_disable-backup | hcloud_server_rebuild )
				__hcloud_server_names
				return
				;;
			hcloud_server_attach-iso )
				if [[ ${#nouns[@]} -gt 1 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -eq 1 ]]; then
					__hcloud_iso_names
					return
				fi
				__hcloud_server_names
				return
				;;
			hcloud_server_change-type )
				if [[ ${#nouns[@]} -gt 1 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -eq 1 ]]; then
					__hcloud_servertype_names
					return
				fi
				__hcloud_server_names
				return
				;;
			hcloud_server-type_describe )
				__hcloud_servertype_names
				return
				;;
			hcloud_image_describe )
				__hcloud_image_names
				return
				;;
			hcloud_image_delete | hcloud_image_update )
				__hcloud_image_ids_no_system
				return
				;;
			hcloud_floating-ip_assign )
				if [[ ${#nouns[@]} -gt 1 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -eq 1 ]]; then
					__hcloud_server_names
					return
				fi
				__hcloud_floating_ip_ids
				return
				;;
			hcloud_floating-ip_enable-protection | hcloud_floating-ip_disable-protection )
				if [[ ${#nouns[@]} -gt 1 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -eq 1 ]]; then
					__hcloud_protection_levels
					return
				fi
				__hcloud_floating_ip_ids
				return
				;;
			hcloud_image_enable-protection | hcloud_image_disable-protection )
				if [[ ${#nouns[@]} -gt 1 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -eq 1 ]]; then
					__hcloud_protection_levels
					return
				fi
				__hcloud_image_ids_no_system
				return
				;;
			hcloud_server_enable-protection | hcloud_server_disable-protection )
				if [[ ${#nouns[@]} -gt 2 ]]; then
					return 1
				fi
				if [[ ${#nouns[@]} -gt 0 ]]; then
					__hcloud_server_protection_levels
					return
				fi
				__hcloud_server_names
				return
				;;
			hcloud_floating-ip_unassign | hcloud_floating-ip_delete | \
			hcloud_floating-ip_describe | hcloud_floating-ip_update )
				__hcloud_floating_ip_ids
				return
				;;
			hcloud_datacenter_describe )
				__hcloud_datacenter_names
				return
				;;
			hcloud_location_describe )
				__hcloud_location_names
				return
				;;
			hcloud_iso_describe )
				__hcloud_iso_names
				return
				;;
			hcloud_context_use | hcloud_context_delete )
				__hcloud_context_names
				return
				;;
			hcloud_ssh-key_delete | hcloud_ssh-key_describe )
				__hcloud_sshkey_names
				return
				;;
			*)
				;;
		esac
	}
	
_hcloud_completion()
{
    last_command="hcloud_completion"
    commands=()

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

    flags+=("--help")
    flags+=("-h")
    local_nonpersistent_flags+=("--help")
    flags+=("--poll-interval=")

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

_hcloud_context_active()
{
    last_command="hcloud_context_active"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_context_create()
{
    last_command="hcloud_context_create"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_context_delete()
{
    last_command="hcloud_context_delete"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_context_list()
{
    last_command="hcloud_context_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+=("--poll-interval=")

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

_hcloud_context_use()
{
    last_command="hcloud_context_use"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_context()
{
    last_command="hcloud_context"
    commands=()
    commands+=("active")
    commands+=("create")
    commands+=("delete")
    commands+=("list")
    commands+=("use")

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

    flags+=("--poll-interval=")

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

_hcloud_datacenter_describe()
{
    last_command="hcloud_datacenter_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_datacenter_list()
{
    last_command="hcloud_datacenter_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+=("--poll-interval=")

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

_hcloud_datacenter()
{
    last_command="hcloud_datacenter"
    commands=()
    commands+=("describe")
    commands+=("list")

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_assign()
{
    last_command="hcloud_floating-ip_assign"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_create()
{
    last_command="hcloud_floating-ip_create"
    commands=()

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

    flags+=("--description=")
    local_nonpersistent_flags+=("--description=")
    flags+=("--home-location=")
    flags_with_completion+=("--home-location")
    flags_completion+=("__hcloud_location_names")
    local_nonpersistent_flags+=("--home-location=")
    flags+=("--server=")
    flags_with_completion+=("--server")
    flags_completion+=("__hcloud_server_names")
    local_nonpersistent_flags+=("--server=")
    flags+=("--type=")
    flags_with_completion+=("--type")
    flags_completion+=("__hcloud_floatingip_types")
    local_nonpersistent_flags+=("--type=")
    flags+=("--poll-interval=")

    must_have_one_flag=()
    must_have_one_flag+=("--type=")
    must_have_one_noun=()
    noun_aliases=()
}

_hcloud_floating-ip_delete()
{
    last_command="hcloud_floating-ip_delete"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_describe()
{
    last_command="hcloud_floating-ip_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_disable-protection()
{
    last_command="hcloud_floating-ip_disable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_enable-protection()
{
    last_command="hcloud_floating-ip_enable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_list()
{
    last_command="hcloud_floating-ip_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+=("--poll-interval=")

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

_hcloud_floating-ip_unassign()
{
    last_command="hcloud_floating-ip_unassign"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_floating-ip_update()
{
    last_command="hcloud_floating-ip_update"
    commands=()

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

    flags+=("--description=")
    local_nonpersistent_flags+=("--description=")
    flags+=("--poll-interval=")

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

_hcloud_floating-ip()
{
    last_command="hcloud_floating-ip"
    commands=()
    commands+=("assign")
    commands+=("create")
    commands+=("delete")
    commands+=("describe")
    commands+=("disable-protection")
    commands+=("enable-protection")
    commands+=("list")
    commands+=("unassign")
    commands+=("update")

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

    flags+=("--poll-interval=")

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

_hcloud_image_delete()
{
    last_command="hcloud_image_delete"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_image_describe()
{
    last_command="hcloud_image_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_image_disable-protection()
{
    last_command="hcloud_image_disable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_image_enable-protection()
{
    last_command="hcloud_image_enable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_image_list()
{
    last_command="hcloud_image_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+=("--poll-interval=")

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

_hcloud_image_update()
{
    last_command="hcloud_image_update"
    commands=()

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

    flags+=("--description=")
    local_nonpersistent_flags+=("--description=")
    flags+=("--type=")
    flags_with_completion+=("--type")
    flags_completion+=("__hcloud_image_types_no_system")
    local_nonpersistent_flags+=("--type=")
    flags+=("--poll-interval=")

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

_hcloud_image()
{
    last_command="hcloud_image"
    commands=()
    commands+=("delete")
    commands+=("describe")
    commands+=("disable-protection")
    commands+=("enable-protection")
    commands+=("list")
    commands+=("update")

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

    flags+=("--poll-interval=")

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

_hcloud_iso_describe()
{
    last_command="hcloud_iso_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_iso_list()
{
    last_command="hcloud_iso_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+=("--poll-interval=")

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

_hcloud_iso()
{
    last_command="hcloud_iso"
    commands=()
    commands+=("describe")
    commands+=("list")

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

    flags+=("--poll-interval=")

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

_hcloud_location_describe()
{
    last_command="hcloud_location_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_location_list()
{
    last_command="hcloud_location_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+=("--poll-interval=")

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

_hcloud_location()
{
    last_command="hcloud_location"
    commands=()
    commands+=("describe")
    commands+=("list")

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

    flags+=("--poll-interval=")

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

_hcloud_server_attach-iso()
{
    last_command="hcloud_server_attach-iso"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_change-type()
{
    last_command="hcloud_server_change-type"
    commands=()

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

    flags+=("--keep-disk")
    local_nonpersistent_flags+=("--keep-disk")
    flags+=("--poll-interval=")

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

_hcloud_server_create()
{
    last_command="hcloud_server_create"
    commands=()

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

    flags+=("--datacenter=")
    flags_with_completion+=("--datacenter")
    flags_completion+=("__hcloud_datacenter_names")
    local_nonpersistent_flags+=("--datacenter=")
    flags+=("--image=")
    flags_with_completion+=("--image")
    flags_completion+=("__hcloud_image_names")
    local_nonpersistent_flags+=("--image=")
    flags+=("--location=")
    flags_with_completion+=("--location")
    flags_completion+=("__hcloud_location_names")
    local_nonpersistent_flags+=("--location=")
    flags+=("--name=")
    local_nonpersistent_flags+=("--name=")
    flags+=("--ssh-key=")
    flags_with_completion+=("--ssh-key")
    flags_completion+=("__hcloud_sshkey_names")
    local_nonpersistent_flags+=("--ssh-key=")
    flags+=("--type=")
    flags_with_completion+=("--type")
    flags_completion+=("__hcloud_servertype_names")
    local_nonpersistent_flags+=("--type=")
    flags+=("--user-data-from-file=")
    local_nonpersistent_flags+=("--user-data-from-file=")
    flags+=("--poll-interval=")

    must_have_one_flag=()
    must_have_one_flag+=("--image=")
    must_have_one_flag+=("--name=")
    must_have_one_flag+=("--type=")
    must_have_one_noun=()
    noun_aliases=()
}

_hcloud_server_create-image()
{
    last_command="hcloud_server_create-image"
    commands=()

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

    flags+=("--description=")
    local_nonpersistent_flags+=("--description=")
    flags+=("--type=")
    flags_with_completion+=("--type")
    flags_completion+=("__hcloud_image_types_no_system")
    local_nonpersistent_flags+=("--type=")
    flags+=("--poll-interval=")

    must_have_one_flag=()
    must_have_one_flag+=("--type=")
    must_have_one_noun=()
    noun_aliases=()
}

_hcloud_server_delete()
{
    last_command="hcloud_server_delete"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_describe()
{
    last_command="hcloud_server_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_detach-iso()
{
    last_command="hcloud_server_detach-iso"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_disable-backup()
{
    last_command="hcloud_server_disable-backup"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_disable-protection()
{
    last_command="hcloud_server_disable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_disable-rescue()
{
    last_command="hcloud_server_disable-rescue"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_enable-backup()
{
    last_command="hcloud_server_enable-backup"
    commands=()

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

    flags+=("--window=")
    flags_with_completion+=("--window")
    flags_completion+=("__hcloud_backup_windows")
    local_nonpersistent_flags+=("--window=")
    flags+=("--poll-interval=")

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

_hcloud_server_enable-protection()
{
    last_command="hcloud_server_enable-protection"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_enable-rescue()
{
    last_command="hcloud_server_enable-rescue"
    commands=()

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

    flags+=("--ssh-key=")
    flags_with_completion+=("--ssh-key")
    flags_completion+=("__hcloud_sshkey_names")
    local_nonpersistent_flags+=("--ssh-key=")
    flags+=("--type=")
    flags_with_completion+=("--type")
    flags_completion+=("__hcloud_rescue_types")
    local_nonpersistent_flags+=("--type=")
    flags+=("--poll-interval=")

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

_hcloud_server_list()
{
    last_command="hcloud_server_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+=("--poll-interval=")

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

_hcloud_server_poweroff()
{
    last_command="hcloud_server_poweroff"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_poweron()
{
    last_command="hcloud_server_poweron"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_reboot()
{
    last_command="hcloud_server_reboot"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_rebuild()
{
    last_command="hcloud_server_rebuild"
    commands=()

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

    flags+=("--image=")
    flags_with_completion+=("--image")
    flags_completion+=("__hcloud_image_names")
    local_nonpersistent_flags+=("--image=")
    flags+=("--poll-interval=")

    must_have_one_flag=()
    must_have_one_flag+=("--image=")
    must_have_one_noun=()
    noun_aliases=()
}

_hcloud_server_reset()
{
    last_command="hcloud_server_reset"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_reset-password()
{
    last_command="hcloud_server_reset-password"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_shutdown()
{
    last_command="hcloud_server_shutdown"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server_ssh()
{
    last_command="hcloud_server_ssh"
    commands=()

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

    flags+=("--ipv6")
    local_nonpersistent_flags+=("--ipv6")
    flags+=("--user=")
    local_nonpersistent_flags+=("--user=")
    flags+=("--poll-interval=")

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

_hcloud_server_update()
{
    last_command="hcloud_server_update"
    commands=()

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

    flags+=("--name=")
    local_nonpersistent_flags+=("--name=")
    flags+=("--poll-interval=")

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

_hcloud_server()
{
    last_command="hcloud_server"
    commands=()
    commands+=("attach-iso")
    commands+=("change-type")
    commands+=("create")
    commands+=("create-image")
    commands+=("delete")
    commands+=("describe")
    commands+=("detach-iso")
    commands+=("disable-backup")
    commands+=("disable-protection")
    commands+=("disable-rescue")
    commands+=("enable-backup")
    commands+=("enable-protection")
    commands+=("enable-rescue")
    commands+=("list")
    commands+=("poweroff")
    commands+=("poweron")
    commands+=("reboot")
    commands+=("rebuild")
    commands+=("reset")
    commands+=("reset-password")
    commands+=("shutdown")
    commands+=("ssh")
    commands+=("update")

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

    flags+=("--poll-interval=")

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

_hcloud_server-type_describe()
{
    last_command="hcloud_server-type_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_server-type_list()
{
    last_command="hcloud_server-type_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+=("--poll-interval=")

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

_hcloud_server-type()
{
    last_command="hcloud_server-type"
    commands=()
    commands+=("describe")
    commands+=("list")

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

    flags+=("--poll-interval=")

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

_hcloud_ssh-key_create()
{
    last_command="hcloud_ssh-key_create"
    commands=()

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

    flags+=("--name=")
    local_nonpersistent_flags+=("--name=")
    flags+=("--public-key=")
    local_nonpersistent_flags+=("--public-key=")
    flags+=("--public-key-from-file=")
    local_nonpersistent_flags+=("--public-key-from-file=")
    flags+=("--poll-interval=")

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

_hcloud_ssh-key_delete()
{
    last_command="hcloud_ssh-key_delete"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_ssh-key_describe()
{
    last_command="hcloud_ssh-key_describe"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_ssh-key_list()
{
    last_command="hcloud_ssh-key_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+=("--poll-interval=")

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

_hcloud_ssh-key()
{
    last_command="hcloud_ssh-key"
    commands=()
    commands+=("create")
    commands+=("delete")
    commands+=("describe")
    commands+=("list")

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

    flags+=("--poll-interval=")

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

_hcloud_version()
{
    last_command="hcloud_version"
    commands=()

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

    flags+=("--poll-interval=")

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

_hcloud_root_command()
{
    last_command="hcloud"
    commands=()
    commands+=("completion")
    commands+=("context")
    commands+=("datacenter")
    commands+=("floating-ip")
    commands+=("image")
    commands+=("iso")
    commands+=("location")
    commands+=("server")
    commands+=("server-type")
    commands+=("ssh-key")
    commands+=("version")

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

    flags+=("--poll-interval=")

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

__start_hcloud()
{
    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
        __hcloud_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=("hcloud")
    local must_have_one_flag=()
    local must_have_one_noun=()
    local last_command
    local nouns=()

    __hcloud_handle_word
}

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

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