# Bash completion for omoti-server

_omoti_server_complete_files()
{
	local cur
	cur="${COMP_WORDS[COMP_CWORD]}"
	compopt -o filenames 2>/dev/null
	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|-u|-p|-c)
			COMPREPLY=()
			return
			;;
	esac

	if [ "$COMP_CWORD" -eq 1 ]
	then
		COMPREPLY=( $(compgen -W "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
				COMPREPLY=( $(compgen -W "set_key status help --help -h" -- "$cur") )
				return
			fi
			;;
		credentials)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				COMPREPLY=( $(compgen -W "set clear help --help -h" -- "$cur") )
				return
			fi

			case "$subcmd" in
				set)
					COMPREPLY=( $(compgen -W "--username -u --password -p --name --help -h" -- "$cur") )
					return
					;;
				clear)
					COMPREPLY=( $(compgen -W "--name --help -h" -- "$cur") )
					return
					;;
			esac
			;;
		update)
			COMPREPLY=( $(compgen -W "--manager --omoti --help -h" -- "$cur") )
			return
			;;
		download)
			COMPREPLY=( $(compgen -W "--url --path --login --help -h" -- "$cur") )
			return
			;;
		trim)
			COMPREPLY=( $(compgen -W "--file --lines --help -h" -- "$cur") )
			return
			;;
		log)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				COMPREPLY=( $(compgen -W "critical error warning info --help -h" -- "$cur") )
				return
			fi

			COMPREPLY=( $(compgen -W "--caller -c --help -h" -- "$cur") )
			return
			;;
		firewall)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				COMPREPLY=( $(compgen -W "update show blocklist geoip help --help -h" -- "$cur") )
				return
			fi

			case "$subcmd" in
				show|blocklist|geoip)
					COMPREPLY=( $(compgen -W "--ipv4 -4 --ipv6 -6 --help -h" -- "$cur") )
					return
					;;
			esac
			;;
		setup)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				COMPREPLY=( $(compgen -W "all network packages config --help -h" -- "$cur") )
				return
			fi
			;;
		help)
			if [ "$COMP_CWORD" -eq 2 ]
			then
				COMPREPLY=( $(compgen -W "service license credentials update download trim log firewall setup help" -- "$cur") )
				return
			fi

			case "$subcmd" in
				license)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						COMPREPLY=( $(compgen -W "set_key status" -- "$cur") )
						return
					fi
					;;
				credentials)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						COMPREPLY=( $(compgen -W "set clear" -- "$cur") )
						return
					fi
					;;
				firewall)
					if [ "$COMP_CWORD" -eq 3 ]
					then
						COMPREPLY=( $(compgen -W "update show blocklist geoip" -- "$cur") )
						return
					fi
					;;
			esac
			;;
	esac

	COMPREPLY=()
}

complete -F _omoti_server omoti-server
