#!/bin/sh
SEGATEX_POLICY_CURRENT="segatex	1.2.1"
SEGATEX_POLICY_PRE2="segatex	1.1.13"
SEGATEX_POLICY_PRE="segatex	1.0.23"

############check if segatex policy is alive#################
SEGATEX_IS_ALIVE=`semodule -l|grep segatex`

if [ "$SEGATEX_IS_ALIVE" != "" ]; then
	echo "Policy version- ""$SEGATEX_IS_ALIVE"" is alive !!"
	echo "setting segatex policy permissive."
	semanage permissive -a segatex_t
	echo "changing contex of /usr/share/segatex to usr_t."
	chcon -t usr_t /usr/share/segatex
else
		/usr/bin/chcon -t usr_t /usr/share/segatex
fi

############delete segatex permissive policy#################
echo "Setting segatex policy alive !"	
semanage permissive -d segatex_t
############starting segatex policy#################
if [ "$SEGATEX_IS_ALIVE" != "" ]; then
	echo "OK! ""$SEGATEX_IS_ALIVE"" is alive !!"	
	if [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE2" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_CURRENT" ]; then 
		echo "segatex policy version is up to date !!"
	else
		echo "segatex policy version is wrong."		
	fi
		echo "Now segatex policy version ""$SEGATEX_IS_ALIVE"" is alive !!"
else
	semodule -i segatex.pp
	echo "Installed segatex.pp!"
fi

############restorcon directory and files of segatex#################
restorecon -R -v /usr/share/segatex
echo restoreconed /usr/share/segatex
restorecon -R -v /usr/sbin
echo restoreconed /usr/sbin
restorecon -R -v /usr/bin
echo restoreconed /usr/bin
echo 
############echo messages#################
echo Installation Complete!
echo 
echo Exit and type "segatex" as a non-root user.
echo You can call "segatex" from anywhere.
echo You will find "segatex" in System-Tools.
echo 
