# This file is part of permission-manager-helper
#
# zhangya@uniontech.com 2024-06-08

__get_all_booleans() {
	usecmanage getsebool -a | cut -f1 -d' '
}

_setsebool () {
	local command=${COMP_WORDS[1]}
	local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
	local verb comps

	if   [ "$verb" = "" -a "$prev" = "setsebool" -o "$prev" = "-P" ]; then
		COMPREPLY=( $(compgen -W "-P $( __get_all_booleans ) " -- "$cur") )
		return 0
	fi

	COMPREPLY=( $(compgen -W "0 1 -P" -- "$cur") )
	return 0
}

_getsebool () {
	local command=${COMP_WORDS[1]}
	local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
	local verb comps

	if   [ "$verb" = "" -a "$prev" == "getsebool" ]; then
		COMPREPLY=( $(compgen -W "-a $( __get_all_booleans ) " -- "$cur") )
		return 0
	fi
	if   [ "$verb" = "" -a "$prev" != "-a" ]; then
	        COMPREPLY=( $(compgen -W "$( __get_all_booleans ) " -- "$cur") )
		return 0
	fi
	return 0
}

_usecmanage () {
	local command=${COMP_WORDS[1]}
	local cur=${COMP_WORDS[COMP_CWORD]}
	local prev=${COMP_WORDS[COMP_CWORD-1]}
	local verb comps

	if   [ "$command" == "getsebool" ]; then
		if   [ "$verb" = "" -a "$prev" == "getsebool" ]; then
			COMPREPLY=( $(compgen -W "-a $( __get_all_booleans ) " -- "$cur") )
			return 0
		fi
		if   [ "$verb" = "" -a "$prev" != "-a" ]; then
				COMPREPLY=( $(compgen -W "$( __get_all_booleans ) " -- "$cur") )
			return 0
		fi
        return 0
	fi
	if   [ "$command" == "setsebool" ]; then
		if   [ "$verb" = "" -a "$prev" = "setsebool" -o "$prev" = "-P" ]; then
			COMPREPLY=( $(compgen -W "-P $( __get_all_booleans ) " -- "$cur") )
			return 0
		fi

		COMPREPLY=( $(compgen -W "0 1 -P" -- "$cur") )
		return 0
	fi

	if   [ "$command" == "setenforce" ]; then
		COMPREPLY=( $(compgen -W "Enforcing Permissive 0 1" -- "$cur") )
		return 0
	fi

	if   [ "$command" == "getenforce" ]; then
		return 0
	fi

	COMPREPLY=( $(compgen -W "setenforce getenforce setsebool getsebool"  -- "$cur"))
	return 0
}

complete -F _usecmanage usecmanage