#!/bin/sh
 
SEGATEX_POLICY_CURRENT="segatex	1.11.0"
############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 [ -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

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

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

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

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 !
cp ./utils/getorderedcontextlist /usr/share/segatex/utils
echo Copied ./utils/getorderedcontextlist to /usr/share/segatex/utils !
cp ./utils/classaccessvector /usr/share/segatex/utils
echo Copied ./utils/classaccessvector to /usr/share/segatex/utils !
cp ./utils/booleannames /usr/share/segatex/utils
echo Copied ./utils/booleannames to /usr/share/segatex/utils !
cp ./utils/getpidcon /usr/share/segatex/utils
echo Copied ./utils/getpidcon to /usr/share/segatex/utils !
cp ./utils/selinuxpath /usr/share/segatex/utils
echo Copied ./utils/selinuxpath to /usr/share/segatex/utils !


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/{admin,apps,contrib,kernel,roles,services,system}
	echo Made directory /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/contrib/*.te /usr/share/segatex/raw_te_files/contrib/ 
	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 splitfile.txt breakte.sh /usr/share/segatex/raw_te_files
	echo Copied all.if all.spt allif.txt allspt.txt splitfile.txt breakte.sh to /usr/share/segatex/raw_te_files !
else
	mkdir -p /usr/share/segatex/raw_te_files/{admin,apps,contrib,kernel,roles,services,system}
	echo Made directory /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/contrib/*.te /usr/share/segatex/raw_te_files/contrib/ 
	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 splitfile.txt breakte.sh /usr/share/segatex/raw_te_files
	echo Copied all.if all.spt allif.txt allspt.txt splitfile.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/{admin,apps,contrib,kernel,roles,services,system}
	echo Made directory /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/contrib/*.if /usr/share/segatex/raw_if_files/contrib/ 
	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 define_file all.if all.spt allif.txt allspt.txt breakif.sh /usr/share/segatex/raw_if_files
	echo Copied allif_exclude_myself.txt start_file define_file all.if all.spt allif.txt allspt.txt breakif.sh to /usr/share/segatex/raw_if_files !
else
	mkdir -p /usr/share/segatex/raw_if_files/{admin,apps,contrib,kernel,roles,services,system}
	echo Made directory /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/contrib/*.if /usr/share/segatex/raw_if_files/contrib/ 
	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 define_file all.if all.spt allif.txt allspt.txt breakif.sh /usr/share/segatex/raw_if_files
	echo Copied allif_exclude_myself.txt start_file define_file all.if 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
chmod 700 /usr/share/segatex/utils/getorderedcontextlist
echo chmod 700 /usr/share/segatex/utils/getorderedcontextlist
chmod 700 /usr/share/segatex/utils/classaccessvector
echo chmod 700 /usr/share/segatex/utils/classaccessvector
chmod 700 /usr/share/segatex/utils/booleannames
echo chmod 700 /usr/share/segatex/utils/booleannames
chmod 700 /usr/share/segatex/utils/getpidcon
echo chmod 700 /usr/share/segatex/utils/getpidcon
chmod 700 /usr/share/segatex/utils/selinuxpath
echo chmod 700 /usr/share/segatex/utils/selinuxpath
chmod 700 /usr/share/segatex/raw_te_files/{admin,apps,contrib,kernel,roles,services,system}
echo chmod 700 /usr/share/segatex/raw_te_files
chmod 700 /usr/share/segatex/raw_if_files/{admin,apps,contrib,kernel,roles,services,system}
echo chmod 700 /usr/share/segatex/raw_if_files
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######################
######################### auditcheck2 needs to be installed ##################
AUDITCHECK2_IS_ALIVE=`semodule -l|grep auditcheck2`
if [ "$AUDITCHECK2_IS_ALIVE" = "" ]; then
	echo "Trying installing auditcheck2 module anew!"
	semodule -i auditcheck2.pp
else
	echo "auditcheck2 module is installed. OK let's move on."
fi
############starting segatex policy#################
echo "Trying installing new segatex.pp!"
if [ "$SEGATEX_IS_ALIVE" != "" ]; then
echo "Removing present segatex module!"
	semodule -r segatex
fi
echo "Installing new segatex!"
	semodule -i segatex.pp
echo "Installed segatex module!"
############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
restorecon -R -v /root
echo restoreconed /root
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 
