#!/usr/bin/env bash
#
# completion for yunohost
# automatically generated from the actionsmap
#

_yunohost()
{
	local cur prev opts narg
	COMPREPLY=()

	# the number of words already typed
	narg=${#COMP_WORDS[@]}

	# the current word being typed
	cur="${COMP_WORDS[COMP_CWORD]}"

	# If one is currently typing a category,
	# match with categorys
	if [[ $narg == 2 ]]; then
		opts="user domain app backup settings service firewall dyndns tools hook log diagnosis storage"
	fi

	# If one already typed a category,
	# match the actions or the subcategories of that category
	if [[ $narg == 3 ]]; then
		# the category typed
		category="${COMP_WORDS[1]}"
		if [[ $category == "user" ]]; then
			opts="list create delete update info export import group permission ssh"
		fi
		if [[ $category == "domain" ]]; then
			opts="list info add remove main-domain url-available action-run dyndns config dns cert"
		fi
		if [[ $category == "app" ]]; then
			opts="catalog search manifest list info map install remove upgrade change-url setting shell register-url makedefault dismiss-notification ssowatconf change-label action config"
		fi
		if [[ $category == "backup" ]]; then
			opts="create restore list info download delete"
		fi
		if [[ $category == "settings" ]]; then
			opts="list get set reset-all reset"
		fi
		if [[ $category == "service" ]]; then
			opts="add remove start stop reload restart reload_or_restart enable disable status log"
		fi
		if [[ $category == "firewall" ]]; then
			opts="list is-open open close delete allow disallow upnp reload stop"
		fi
		if [[ $category == "dyndns" ]]; then
			opts="subscribe update"
		fi
		if [[ $category == "tools" ]]; then
			opts="rootpw maindomain postinstall update_norefresh update upgrade shell basic-space-cleanup shutdown reboot regen-conf versions migrations"
		fi
		if [[ $category == "hook" ]]; then
			opts="add remove info list callback exec"
		fi
		if [[ $category == "log" ]]; then
			opts="list show share"
		fi
		if [[ $category == "diagnosis" ]]; then
			opts="list show get run ignore unignore"
		fi
		if [[ $category == "storage" ]]; then
			opts="disk"
		fi
	fi

	# If one already typed an action or a subcategory,
	# match the actions of that subcategory
	if [[ $narg == 4 ]]; then
		# the category typed
		category="${COMP_WORDS[1]}"

		# the action or the subcategory typed
		action_or_subcategory="${COMP_WORDS[2]}"
		if [[ $category == "user" ]]; then
			if [[ $action_or_subcategory == "group" ]]; then
				opts="list create delete info add remove add-mailalias remove-mailalias"
			fi
			if [[ $action_or_subcategory == "permission" ]]; then
				opts="list info update add remove ldapsync"
			fi
			if [[ $action_or_subcategory == "ssh" ]]; then
				opts="list-keys add-key remove-key"
			fi
		fi
		if [[ $category == "domain" ]]; then
			if [[ $action_or_subcategory == "dyndns" ]]; then
				opts="subscribe unsubscribe set-recovery-password"
			fi
			if [[ $action_or_subcategory == "config" ]]; then
				opts="get set"
			fi
			if [[ $action_or_subcategory == "dns" ]]; then
				opts="suggest push"
			fi
			if [[ $action_or_subcategory == "cert" ]]; then
				opts="status install renew"
			fi
		fi
		if [[ $category == "app" ]]; then
			if [[ $action_or_subcategory == "action" ]]; then
				opts="list run"
			fi
			if [[ $action_or_subcategory == "config" ]]; then
				opts="get set"
			fi
		fi
		if [[ $category == "tools" ]]; then
			if [[ $action_or_subcategory == "migrations" ]]; then
				opts="list run state"
			fi
		fi
		if [[ $category == "storage" ]]; then
			if [[ $action_or_subcategory == "disk" ]]; then
				opts="list info"
			fi
		fi
	fi

	# If no options were found propose --help
	if [ -z "$opts" ]; then
		prev="${COMP_WORDS[COMP_CWORD-1]}"

		if [[ $prev != "--help" ]]; then
			opts=( --help )
		fi
	fi
	COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
	return 0
}

complete -F _yunohost yunohost