#!/bin/sh

SEGATEX_POLICY_CURRENT="segatex	1.4.11"
SEGATEX_POLICY_PRE23="segatex	1.4.10"
SEGATEX_POLICY_PRE22="segatex	1.4.9"
SEGATEX_POLICY_PRE21="segatex	1.4.8"
SEGATEX_POLICY_PRE20="segatex	1.4.7"
SEGATEX_POLICY_PRE19="segatex	1.4.6"
SEGATEX_POLICY_PRE18="segatex	1.4.5"
SEGATEX_POLICY_PRE17="segatex	1.4.4"
SEGATEX_POLICY_PRE16="segatex	1.4.3"
SEGATEX_POLICY_PRE15="segatex	1.4.2"
SEGATEX_POLICY_PRE14="segatex	1.4.1"
SEGATEX_POLICY_PRE13="segatex	1.4.0"
SEGATEX_POLICY_PRE12="segatex	1.3.64"
SEGATEX_POLICY_PRE11="segatex	1.3.63"
SEGATEX_POLICY_PRE10="segatex	1.3.62"
SEGATEX_POLICY_PRE9="segatex	1.3.61"
SEGATEX_POLICY_PRE8="segatex	1.3.60"
SEGATEX_POLICY_PRE7="segatex	1.3.59"
SEGATEX_POLICY_PRE6="segatex	1.3.58"
SEGATEX_POLICY_PRE5="segatex	1.3.56"
SEGATEX_POLICY_PRE4="segatex	1.3.55"
SEGATEX_POLICY_PRE3="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 -R 
else
		/usr/bin/chcon -t usr_t /usr/share/segatex -R

fi

############delete segatex permissive policy#################
echo "Setting segatex policy alive !"	
#####################edited in segatex-7.765######################
SEGATEX_PERMISSIVE_IS_ALIVE=`semodule -l|grep segatex|grep permissive`
if [ "$SEGATEX_IS_ALIVE" != "" ]; then
	semanage permissive -d segatex_t
fi
#####################end edited in segatex-7.765######################
#####################added in segatex-7.769######################
cd /usr/share/segatex
#####################end added in segatex-7.769######################
############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_PRE23" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE22" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE21" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE20" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE19" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE18" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE17" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE16" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE15" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE14" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE13" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE12" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE11" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE10" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE9" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE8" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE7" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE6" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE5" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE4" ]; then
		echo "update segatex to ""$SEGATEX_POLICY_CURRENT"" !!"
		semodule -u segatex.pp
	elif [ "$SEGATEX_IS_ALIVE" = "$SEGATEX_POLICY_PRE3" ]; 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
	echo "Trying installing auditcheck2.pp anew!"
	semodule -i auditcheck2.pp
	echo "Trying installing segatex.pp anew!"
	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 
