# fapolicyd-cli (8) completion                             -*- shell-script -*-

_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-status --check-trustdb \
		--check-watch_fs --check-ignore_mounts --delete-db --dump-db \
		--file --filter --ftype --help --list --update --reload-rules \
		--test-filter --trust-file --verbose -h -d -D -f -t -l -u -r"

	# 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
				COMPREPLY=( $(compgen -f -- ${cur}) )
			fi
			return 0
		fi

		if [[ -z ${subcmd} ]]; then
			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
				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
			COMPREPLY=($(compgen -W "${file_opts}" -- "${cur}"))
			[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
			return 0
		fi
	fi

	case $prev in
		--ftype|-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
				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
				COMPREPLY=( $(compgen -d -- ${cur}) )
				return 0
			fi
		;;
		--file|-f)
			COMPREPLY=($(compgen -W 'add delete update' -- "$cur"))
			return 0
		;;
		add|update|delete)
			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
				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
				COMPREPLY=( $(compgen -f -- ${cur}) )
			fi
			return 0
		;;
	esac

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

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

	case ${prev} in
		--mounts)
			if [ -e /usr/share/bash-completion/bash_completion ] ; then
				_filedir
			else
				compopt -o filenames 2>/dev/null
				COMPREPLY=( $(compgen -f -- ${cur}) )
			fi
			return 0
		;;
	esac

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

complete -F _fapolicydcli fapolicyd-cli
complete -F _fapolicyd fapolicyd

# ex: filetype=sh
