#!/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"
############change context of this file#################
echo "changing context of this file."
/usr/bin/chcon -t bin_t INSTALL

############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
	if [ -d /usr/share/segatex ]; then
		echo "changing contex of /usr/share/segatex to usr_t."
		/usr/bin/chcon -t usr_t /usr/share/segatex
	else
		mkdir /usr/share/segatex
		echo "changing contex of /usr/share/segatex to usr_t."
		/usr/bin/chcon -t usr_t /usr/share/segatex
	fi
fi

############start installing segatex#################
if [ -r /usr/sbin/segatex ]
then
	rm /usr/sbin/segatex
	cp ./src/segatex /usr/sbin/segatex
	echo Copied ./src/segatex to /usr/sbin/segatex !
else
	cp ./src/segatex /usr/sbin/segatex
	echo Copied ./src/segatex to /usr/sbin/segatex !
fi

if [ -r /usr/bin/segatex ]
then
	rm /usr/bin/segatex
	echo Removed /usr/bin/segatex !
fi

ln -s /usr/bin/consolehelper /usr/bin/segatex

cp ./segatex_for_consolehelper /etc/pam.d/segatex
cp ./segatex-gui /etc/security/console.apps/segatex

if [ -r /usr/share/applications/segatex-gui.desktop ]
then
	rm /usr/share/applications/segatex-gui.desktop
	cp ./segatex-gui.desktop /usr/share/applications
	echo Copied ./segatex-gui.desktop to /usr/share/applications !
else
	cp ./segatex-gui.desktop /usr/share/applications
	echo Copied ./segatex-gui.desktop to /usr/share/applications !
fi

if [ -r /usr/share/pixmaps/segatex-gui.png ]
then
	rm /usr/share/pixmaps/segatex-gui.png
	cp ./src/images/icon.png /usr/share/pixmaps/segatex-gui.png
	echo Copied ./src/images/icon.png to /usr/share/pixmaps as segatex-gui.png!
else
	cp ./src/images/icon.png /usr/share/pixmaps/segatex-gui.png
	echo Copied ./src/images/icon.png to /usr/share/pixmaps as segatex-gui.png!
fi

#if [ -r /etc/xdg/autostart/segatexauto.desktop ]
#then
#	rm /etc/xdg/autostart/segatexauto.desktop
#	cp ./segatexauto.desktop /etc/xdg/autostart/segatexauto.desktop
#	echo Copied ./segatexauto.desktop to /etc/xdg/autostart/segatexauto.desktop!
#else
#	cp ./segatexauto.desktop /etc/xdg/autostart/segatexauto.desktop
#	echo Copied ./segatexauto.desktop to /etc/xdg/autostart/segatexauto.desktop!
#fi

if [ -d /usr/share/segatex/refpolicy ]
then
	cp -R ./refpolicy/* /usr/share/segatex/refpolicy
	chmod 700 /usr/share/segatex/refpolicy
	echo Copied ./refpolicy to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/refpolicy
else
	mkdir -p /usr/share/segatex/refpolicy
	cp -R ./refpolicy/* /usr/share/segatex/refpolicy
	chmod 700 /usr/share/segatex/refpolicy
	echo Copied ./refpolicy to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/refpolicy
fi

if [ -d /usr/share/segatex/policy ]
then
	cp -R ./policy/* /usr/share/segatex/policy
	chmod 700 /usr/share/segatex/policy
	echo Copied ./policy to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/policy
else
	mkdir -p /usr/share/segatex/policy
	cp -R ./policy/* /usr/share/segatex/policy
	chmod 700 /usr/share/segatex/policy
	echo Copied ./policy to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/policy
fi

cp ./policy/* /usr/share/segatex/ 
echo Copied every policy files in /usr/share/segatex !

if [ -d /usr/share/segatex/images ]
then
	cp -R ./src/images/* /usr/share/segatex/images
	chmod 700 /usr/share/segatex/images
	echo Copied ./src/images to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/images
else
	mkdir -p /usr/share/segatex/images
	cp -R ./src/images/* /usr/share/segatex/images
	chmod 700 /usr/share/segatex/images
	echo Copied ./src/images to /usr/share/segatex !
	echo chmod 700 /usr/share/segatex/images
fi

if [ -d /usr/share/segatex/semanage ]
then
	chmod 700 /usr/share/segatex/semanage
	echo chmod 700 /usr/share/segatex/semanage
else
	mkdir -p /usr/share/segatex/semanage
	chmod 700 /usr/share/segatex/semanage
	echo Made directory /usr/share/segatex/semanage !
	echo chmod 700 /usr/share/segatex/semanage
fi

if [ -d /usr/share/segatex/aureport ]
then
	chmod 700 /usr/share/segatex/aureport
	echo chmod 700 /usr/share/segatex/aureport
else
	mkdir -p /usr/share/segatex/aureport
	chmod 700 /usr/share/segatex/aureport
	echo Made directory /usr/share/segatex/aureport !
	echo chmod 700 /usr/share/segatex/aureport
fi

if [ -d /usr/share/segatex/ausearch ]
then
	chmod 700 /usr/share/segatex/ausearch
	echo chmod 700 /usr/share/segatex/ausearch
else
	mkdir -p /usr/share/segatex/ausearch
	chmod 700 /usr/share/segatex/ausearch
	echo Made directory /usr/share/segatex/ausearch !
	echo chmod 700 /usr/share/segatex/ausearch
fi

if [ -d /usr/share/segatex/seinfo ]
then
	chmod 700 /usr/share/segatex/seinfo
	echo chmod 700 /usr/share/segatex/seinfo
else
	mkdir -p /usr/share/segatex/seinfo
	chmod 700 /usr/share/segatex/seinfo
	echo Made directory /usr/share/segatex/seinfo !
	echo chmod 700 /usr/share/segatex/seinfo
fi

cp ./selpaw_interface.csv /usr/share/segatex/
echo Copied ./selpaw_interface.csv to /usr/share/segatex !
cp ./splash.png /usr/share/segatex/
echo Copied ./splash.png to /usr/share/segatex !
cp ./policygeneration_script /usr/share/segatex/
echo Copied ./policygeneration_script to /usr/share/segatex !
cp src/segatex_ja_JP.qm /usr/share/segatex/
echo Copied src/segatex_ja_JP.qm to /usr/share/segatex !
cp ./sqlrefpolicy.db /usr/share/segatex/
echo Copied ./sqlrefpolicy.db to /usr/share/segatex !
cp ./SILENCE_SETROUBLESHOOTD_FOR_SEEPROCESS* /usr/share/segatex/
echo Copied ./SILENCE_SETROUBLESHOOTD_FOR_SEEPROCESS* to /usr/share/segatex !
cp ./DONTAUDIT_STATE /usr/share/segatex/
echo Copied ./DONTAUDIT_STATE to /usr/share/segatex !


if [ -d /usr/share/segatex/raw_te_files ]
then
	rm -rf /usr/share/segatex/raw_te_files
	mkdir -p /usr/share/segatex/raw_te_files
	mkdir -p /usr/share/segatex/raw_te_files/{admin,apps,kernel,roles,services,system}
	echo Made directory /usr/share/segatex/raw_te_files !
	chmod 700 /usr/share/segatex/raw_te_files/{admin,apps,kernel,roles,services,system}
	echo chmod 700 /usr/share/segatex/raw_te_files
	cp ./refpolicy/policy/modules/admin/*te /usr/share/segatex/raw_te_files/admin/ 
	cp ./refpolicy/policy/modules/apps/*te /usr/share/segatex/raw_te_files/apps/ 
	cp ./refpolicy/policy/modules/kernel/*te /usr/share/segatex/raw_te_files/kernel/ 
	cp ./refpolicy/policy/modules/roles/*te /usr/share/segatex/raw_te_files/roles/ 
	cp ./refpolicy/policy/modules/services/*te /usr/share/segatex/raw_te_files/services/ 
	cp ./refpolicy/policy/modules/system/*te /usr/share/segatex/raw_te_files/system/ 
	echo Copied every policy files in /usr/share/segatex/raw_te_files/layer !
	cp all.if all.spt allif.txt allspt.txt breakte.sh /usr/share/segatex/raw_te_files
	echo Copied all.if all.spt allif.txt allspt.txt breakte.sh to /usr/share/segatex/raw_te_files !
else
	mkdir -p /usr/share/segatex/raw_te_files
	mkdir -p /usr/share/segatex/raw_te_files/{admin,apps,kernel,roles,services,system}
	echo Made directory /usr/share/segatex/raw_te_files !
	chmod 700 /usr/share/segatex/raw_te_files/{admin,apps,kernel,roles,services,system}
	echo chmod 700 /usr/share/segatex/raw_te_files
	cp ./refpolicy/policy/modules/admin/*te /usr/share/segatex/raw_te_files/admin/ 
	cp ./refpolicy/policy/modules/apps/*te /usr/share/segatex/raw_te_files/apps/ 
	cp ./refpolicy/policy/modules/kernel/*te /usr/share/segatex/raw_te_files/kernel/ 
	cp ./refpolicy/policy/modules/roles/*te /usr/share/segatex/raw_te_files/roles/ 
	cp ./refpolicy/policy/modules/services/*te /usr/share/segatex/raw_te_files/services/ 
	cp ./refpolicy/policy/modules/system/*te /usr/share/segatex/raw_te_files/system/ 
	echo Copied every policy files in /usr/share/segatex/raw_te_files/layer !
	cp -f all.if all.spt allif.txt allspt.txt breakte.sh /usr/share/segatex/raw_te_files
	echo Copied all.if all.spt allif.txt allspt.txt breakte.sh to /usr/share/segatex/raw_te_files !
fi
if [ -d /usr/share/segatex/raw_if_files ]
then
	rm -rf /usr/share/segatex/raw_if_files
	mkdir -p /usr/share/segatex/raw_if_files
	mkdir -p /usr/share/segatex/raw_if_files/{admin,apps,kernel,roles,services,system}
	echo Made directory /usr/share/segatex/raw_if_files !
	chmod 700 /usr/share/segatex/raw_if_files/{admin,apps,kernel,roles,services,system}
	echo chmod 700 /usr/share/segatex/raw_if_files
	cp ./refpolicy/policy/modules/admin/*if /usr/share/segatex/raw_if_files/admin/ 
	cp ./refpolicy/policy/modules/apps/*if /usr/share/segatex/raw_if_files/apps/ 
	cp ./refpolicy/policy/modules/kernel/*if /usr/share/segatex/raw_if_files/kernel/ 
	cp ./refpolicy/policy/modules/roles/*if /usr/share/segatex/raw_if_files/roles/ 
	cp ./refpolicy/policy/modules/services/*if /usr/share/segatex/raw_if_files/services/ 
	cp ./refpolicy/policy/modules/system/*if /usr/share/segatex/raw_if_files/system/ 
	echo Copied every policy files in /usr/share/segatex/raw_if_files/layer !
	cp allif_exclude_myself.txt start_file all.spt allif.txt allspt.txt breakif.sh /usr/share/segatex/raw_if_files
	echo Copied allif_exclude_myself.txt start_file all.spt allif.txt allspt.txt breakif.sh to /usr/share/segatex/raw_if_files !
else
	mkdir -p /usr/share/segatex/raw_if_files
	mkdir -p /usr/share/segatex/raw_if_files/{admin,apps,kernel,roles,services,system}
	echo Made directory /usr/share/segatex/raw_if_files !
	chmod 700 /usr/share/segatex/raw_if_files/{admin,apps,kernel,roles,services,system}
	echo chmod 700 /usr/share/segatex/raw_if_files
	cp ./refpolicy/policy/modules/admin/*if /usr/share/segatex/raw_if_files/admin/ 
	cp ./refpolicy/policy/modules/apps/*if /usr/share/segatex/raw_if_files/apps/ 
	cp ./refpolicy/policy/modules/kernel/*if /usr/share/segatex/raw_if_files/kernel/ 
	cp ./refpolicy/policy/modules/roles/*if /usr/share/segatex/raw_if_files/roles/ 
	cp ./refpolicy/policy/modules/services/*if /usr/share/segatex/raw_if_files/services/ 
	cp ./refpolicy/policy/modules/system/*if /usr/share/segatex/raw_if_files/system/ 
	echo Copied every policy files in /usr/share/segatex/raw_if_files/layer !
	cp -f allif_exclude_myself.txt start_file all.spt allif.txt allspt.txt breakif.sh /usr/share/segatex/raw_if_files
	echo Copied allif_exclude_myself.txt start_file all.spt allif.txt allspt.txt breakif.sh to /usr/share/segatex/raw_if_files !
fi


chmod 700 /usr/share/segatex
echo chmod 700 /usr/share/segatex
chmod 600 /usr/share/segatex/*.fc /usr/share/segatex/*.te /usr/share/segatex/*.pp /usr/share/segatex/*.xml 
chmod 700 /usr/share/segatex/policygeneration_script
echo chmod 700 /usr/share/segatex/policygeneration_script
chmod 700 /usr/share/segatex/segatex_ja_JP.qm
echo chmod 700 /usr/share/segatex/segatex_jp_JP.qm
unlink /usr/share/segatex/downloader_program
cp ./downloader/downloader /usr/share/segatex/downloader_program
echo Copied ./downloader/downloader to /usr/share/segatex/downloader_program !
chmod 755 /usr/share/segatex/downloader_program
echo chmod 755 /usr/share/segatex/downloader_program
chmod 600 /usr/share/segatex/sqlrefpolicy.db
echo chmod 600 /usr/share/segatex/sqlrefpolicy.db
chmod 700 /usr/share/segatex/SILENCE_SETROUBLESHOOTD_FOR_SEEPROCESS*
echo chmod 700 /usr/share/segatex/SILENCE_SETROUBLESHOOTD_FOR_SEEPROCESS*
chmod 700 /usr/share/segatex/DONTAUDIT_STATE
echo chmod 700 /usr/share/segatex/DONTAUDIT_STATE
echo 

############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######################
############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 
