# fapolicyd-cli (8) completion                             -*- shell-script -*-
# shellcheck shell=bash

_fapolicydcli()
{
	local cur prev opts
	local word i
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	opts="--check-config --check-path --check-rules --check-status --check-metrics \
		--check-trustdb --compact-trustdb --check-watch_fs --check-ignore_mounts \
		--delete-db --dump-db \
		--file --filter --ftype --ftype-ext --help --list --update --reload-rules \
		--reset-metrics --timing-start --timing-stop --timer-start \
		--timer-stop --test-filter \
		--trust-file --lint --verbose --yes -h -d -D -f -t -T -l -u -r -y"

	# Handle file management subcommands specially so we can complete file paths
	local file_mode=false subcmd="" have_path=0 filter_used=0 trust_used=0
	for word in "${COMP_WORDS[@]}"; do
		if [[ ${word} == --file || ${word} == -f ]]; then
			file_mode=true
		fi
	done

	if ${file_mode}; then
		for ((i = 1; i < ${#COMP_WORDS[@]}; i++)); do
			word=${COMP_WORDS[$i]}
			if [[ ${word} == --filter ]]; then
				filter_used=1
				continue
			fi
			if [[ ${word} == --trust-file ]]; then
				trust_used=1
				i=$((i+1))
				continue
			fi
			if [[ -z ${subcmd} && ( ${word} == add || ${word} == delete || ${word} == update ) ]]; then
				subcmd=${word}
				continue
			fi
			if [[ -n ${subcmd} && ${word} != -* && ${word} != add && ${word} != delete && ${word} != update ]]; then
				have_path=1
			fi
		done

		if [[ ${prev} == --trust-file ]]; then
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
			fi
			return 0
		fi

		if [[ -z ${subcmd} ]]; then
			mapfile -t COMPREPLY < <(compgen -W 'add delete update' -- "${cur}")
			return 0
		fi

		if [[ ${prev} == "${subcmd}" || ( ${have_path} -eq 0 && ${cur} != -* && ${prev} != --filter ) ]]; then
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
			fi
			return 0
		fi

		local file_opts=""
		[[ ${filter_used} -eq 0 ]] && file_opts="${file_opts} --filter"
		[[ ${trust_used} -eq 0 ]] && file_opts="${file_opts} --trust-file"
		if [[ -n ${file_opts} && ( -z ${cur} || ${cur} == -* ) ]]; then
			mapfile -t COMPREPLY < <(compgen -W "${file_opts}" -- "${cur}")
			[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
			return 0
		fi
	fi

	case $prev in
		--ftype|-t|--ftype-ext|-T|--test-filter)
			# If bash completions is installed, use it
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
				return 0
			else	# this is almost as good
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
				return 0
			fi
		;;
		--check-ignore_mounts)
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir -d
				return 0
			else
				compopt -o dirnames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -d -- "${cur}")
				return 0
			fi
		;;
		--file|-f)
			mapfile -t COMPREPLY < <(compgen -W 'add delete update' -- "$cur")
			return 0
		;;
		add|update|delete)
			mapfile -t COMPREPLY < <(compgen -W '--filter --trust-file' -- "$cur")
			[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
			return 0
		;;
		--filter)
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
			fi
			return 0
		;;
		--check-rules)
			if [[ ${cur} == -* ]]; then
				mapfile -t COMPREPLY < <(compgen -W '--lint' -- "${cur}")
				return 0
			fi
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
			fi
			return 0
		;;
		--trust-file)
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				mapfile -t COMPREPLY < <(compgen -f -- "${cur}")
			fi
			return 0
		;;
	esac

	if [[ $cur == -* ]]; then
		mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "$cur")
		[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
		return 0
	fi
}

_fapolicyd()
{
	local cur opts
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	opts="--debug --debug-deny --help --mounts= --no-details --permissive --version"

	if [[ ${cur} == --mounts=* ]]; then
		local mount_cur=${cur#--mounts=}
		if [ -e /usr/share/bash-completion/bash_completion ] ; then
			_filedir
			COMPREPLY=( "${COMPREPLY[@]/#/--mounts=}" )
		else
			compopt -o filenames 2>/dev/null
			mapfile -t COMPREPLY < <(compgen -f -- "${mount_cur}")
			COMPREPLY=( "${COMPREPLY[@]/#/--mounts=}" )
		fi
		return 0
	fi

	if [[ ${cur} == -* ]] ; then
		mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}")
		[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
		return 0
	fi
}

_fagenrules()
{
	local cur opts
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	opts="--check --load"

	if [[ ${cur} == -* ]] ; then
		mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}")
		return 0
	fi
}

complete -F _fapolicydcli fapolicyd-cli
complete -F _fapolicyd fapolicyd
complete -F _fagenrules fagenrules

# ex: filetype=sh
