# 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
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev=""
	cmd=""
	subcmd=""

	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)
			_omoti_server_complete_files
			return
			;;
		--url|--lines|--username|--password|--name|--caller|--port|--interface|--comment|-u|-p|-i|-c)
			COMPREPLY=()
			return
			;;
		--protocol)
			_omoti_server_complete_words "tcp udp" "$cur"
			return
			;;
	esac

	if [ "$COMP_CWORD" -eq 1 ]
	then
		_omoti_server_complete_words "service license credentials 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
			;;
		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|fail2ban)
					_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 license credentials 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
					;;
				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
