# Bash completion for omoti-server
# shellcheck shell=bash

_omoti_server_complete_words()
{
	local words
	local cur
	words="${1}"
	cur="${2}"
	mapfile -t COMPREPLY < <(compgen -W "${words}" -- "${cur}")
}

_omoti_server_complete_files()
{
	local cur
	cur="${COMP_WORDS[COMP_CWORD]}"
	compopt -o filenames 2>/dev/null
	mapfile -t COMPREPLY < <(compgen -f -- "$cur")
}

_omoti_server()
{
	local cur prev cmd subcmd certificate_applications
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev=""
	cmd=""
	subcmd=""
	certificate_applications="apache gitlab keycloak nginx omoti-node openbao"

	if [ "$COMP_CWORD" -gt 0 ]
	then
		prev="${COMP_WORDS[$((COMP_CWORD - 1))]}"
	fi

	if [ "${#COMP_WORDS[@]}" -gt 1 ]
	then
		cmd="${COMP_WORDS[1]}"
	fi

	if [ "${#COMP_WORDS[@]}" -gt 2 ]
	then
		subcmd="${COMP_WORDS[2]}"
	fi

	case "$prev" in
		--manager|--path|--file|--token-file)
			_omoti_server_complete_files
			return
			;;
		--url|--lines|--username|--password|--name|--caller|--port|--interface|--comment|--address|--mount|--role-id|--target|--confirm|--repository|--branch|--token-username|--interval-minutes|--database-role|-u|-p|-i|-c)
			COMPREPLY=()
			return
			;;
		--type)
			_omoti_server_complete_words "php static redirect proxy parking" "$cur"
			return
			;;
		--certificate)
			_omoti_server_complete_words "letsencrypt managed" "$cur"
			return
			;;
		--application)
			_omoti_server_complete_words "$certificate_applications" "$cur"
			return
			;;
		--protocol)
			_omoti_server_complete_words "tcp udp" "$cur"
			return
			;;
	esac

	if [ "$COMP_CWORD" -eq 1 ]
	then
		_omoti_server_complete_words "service maintenance license credentials certificate web update download trim log firewall setup help --help --version -h -V" "$cur"
		return
	fi

	case "$cmd" in
		license)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "set_key status help --help -h" "$cur"
				return
			fi
			;;
		credentials)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "set clear help --help -h" "$cur"
				return
			fi

			case "$subcmd" in
				set)
					_omoti_server_complete_words "--username -u --password -p --name --help -h" "$cur"
					return
					;;
				clear)
					_omoti_server_complete_words "--name --help -h" "$cur"
					return
					;;
			esac
			;;
		certificate)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "source add enable disable remove sync status help --help -h" "$cur"
				return
			fi

			case "$subcmd" in
				source)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "configure set-credentials status clear-credentials help --help -h" "$cur"
						return
					fi

					case "${COMP_WORDS[3]}" in
						configure)
							_omoti_server_complete_words "--address --mount --help -h" "$cur"
							return
							;;
						set-credentials)
							_omoti_server_complete_words "--role-id --help -h" "$cur"
							return
							;;
					esac
					;;
				add)
					_omoti_server_complete_words "--application --help -h" "$cur"
					return
					;;
				enable|disable|remove|sync|status)
					_omoti_server_complete_words "--help -h" "$cur"
					return
					;;
			esac
			;;
		web)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "install add remove del status deployment deploy openbao help --help -h" "$cur"
				return
			fi

			case "$subcmd" in
				add)
					_omoti_server_complete_words "--type --target --certificate --allow-private-letsencrypt --help -h" "$cur"
					return
					;;
				remove|del)
					_omoti_server_complete_words "--yes --confirm --help -h" "$cur"
					return
					;;
				install|status|deploy)
					_omoti_server_complete_words "--help -h" "$cur"
					return
					;;
				deployment)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "configure status cleanup help --help -h" "$cur"
						return
					fi

					case "${COMP_WORDS[3]}" in
						cleanup)
							_omoti_server_complete_words "--dry-run --limit --help -h" "$cur"
							return
							;;
						configure)
							_omoti_server_complete_words "--repository --branch --token-username --token-file --interval-minutes --help -h" "$cur"
							return
							;;
						status)
							_omoti_server_complete_words "--help -h" "$cur"
							return
							;;
					esac
					;;
				openbao)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "prepare activate status restart disable help --help -h" "$cur"
						return
					fi

					case "${COMP_WORDS[3]}" in
						prepare)
							_omoti_server_complete_words "--address --database-role --help -h" "$cur"
							return
							;;
						activate)
							_omoti_server_complete_words "--role-id --help -h" "$cur"
							return
							;;
						status|restart|disable)
							_omoti_server_complete_words "--help -h" "$cur"
							return
							;;
					esac
					;;
			esac
			;;
		update)
			_omoti_server_complete_words "--manager --omoti --help -h" "$cur"
			return
			;;
		download)
			_omoti_server_complete_words "--url --path --login --help -h" "$cur"
			return
			;;
		trim)
			_omoti_server_complete_words "--file --lines --help -h" "$cur"
			return
			;;
		log)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "critical error warning info ack --help -h" "$cur"
				return
			fi

			if [ "$subcmd" = "ack" ]
			then
				_omoti_server_complete_words "--caller -c --help -h" "$cur"
				return
			fi

			_omoti_server_complete_words "--caller -c --help -h" "$cur"
			return
			;;
		firewall)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "update show blocklist geoip add delete save docker fail2ban help --help -h" "$cur"
				return
			fi

			case "$subcmd" in
				show|blocklist|geoip)
					_omoti_server_complete_words "--ipv4 -4 --ipv6 -6 --all --help -h" "$cur"
					return
					;;
				add)
					_omoti_server_complete_words "--protocol -p --interface -i --comment -c --help -h" "$cur"
					return
					;;
				delete|del)
					_omoti_server_complete_words "--protocol -p --interface -i --help -h" "$cur"
					return
					;;
				docker)
					_omoti_server_complete_words "--ipv4 -4 --ipv6 -6 --all --help -h" "$cur"
					return
					;;
				fail2ban)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "unban --ipv4 -4 --ipv6 -6 --all --help -h" "$cur"
						return
					fi

					if [ "${COMP_WORDS[3]}" = "unban" ]
					then
						_omoti_server_complete_words "--all --help -h" "$cur"
						return
					fi

					_omoti_server_complete_words "--ipv4 -4 --ipv6 -6 --all --help -h" "$cur"
					return
					;;
			esac
			;;
		setup)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "all network packages config --help -h" "$cur"
				return
			fi
			;;
		help)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				_omoti_server_complete_words "service maintenance license credentials certificate web update download trim log firewall setup help" "$cur"
				return
			fi

			case "$subcmd" in
				license)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "set_key status" "$cur"
						return
					fi
					;;
				credentials)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "set clear" "$cur"
						return
					fi
					;;
				certificate)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "source add enable disable remove sync status" "$cur"
						return
					fi
					;;
				web)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "install add remove del status deployment deploy openbao" "$cur"
						return
					fi
					;;
				firewall)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						_omoti_server_complete_words "update show blocklist geoip add delete save docker fail2ban" "$cur"
						return
					fi
					;;
			esac
			;;
	esac

	COMPREPLY=()
}

complete -F _omoti_server omoti-server
