#!/bin/sh
#
# ntPHPselector 4 
# Installation script for Easyapache 4 
# Website : https://www.nixtree.com
#

# Defenitions 
SUPHPCONFIG=/etc/suphp.conf
line(){
echo -e "********************************************************************"
}
downloadCheck(){
if [ ! -f $1 ];then echo "Download Faied exiting installation..." ; exit ; fi
}
clean_files(){
rm -fr /usr/local/src/ntphpselector
rm -fr /usr/local/src/ntphp
rm -fr /usr/local/src/ntphpv4
rm -f /usr/local/src/ntphpv4.tgz
}
createDir(){
	if [ ! -d $1 ]
		then mkdir -p $1
		fi
}
whmInstall(){
	createDir /usr/local/src
	cd /usr/local/src
	wget -N ntphpv4.tgz http://download.nixtree.com/ntphp/ntphpv4.tgz
	tar -xzf ntphpv4.tgz
	chown root.root ntphpv4 -R
	cp -p ntphpv4/addon_ntphp.cgi /usr/local/cpanel/whostmgr/docroot/cgi/
	mkdir -p /usr/local/cpanel/whostmgr/docroot/cgi/ntphp/archive
	cp -rp ntphpv4/ntphp/* /usr/local/cpanel/whostmgr/docroot/cgi/ntphp/
	touch /usr/local/cpanel/whostmgr/docroot/cgi/ntphp/php53.lock
	mkdir -p /var/cpanel/apps/
	cp -p ntphpv4/ntphp.conf  /var/cpanel/apps/
	/usr/local/cpanel/bin/register_appconfig /var/cpanel/apps/ntphp.conf
	mkdir -p /opt/ntphp
	cp -p ntphpv4/version.sh /opt/ntphp/
	cp -p ntphpv4/ntphp.cron /etc/cron.d/
	service crond restart
	wget -O /usr/local/cpanel/whostmgr/docroot/cgi/ntphp/api.txt -o /dev/null http://download.nixtree.com/api/ntphp/
}
install_files() {
	createDir /usr/local/src/ntphpselector
	cd /usr/local/src/ntphpselector
	echo "Downloading Plugin files..."
	wget -N http://download.nixtree.com/ntphp/ntphpselector.tgz  > /dev/null 2>&1
	downloadCheck ntphpselector.tgz
	tar -xzf ntphpselector.tgz
	cd ntphpselector
	createDir /usr/local/cpanel/base/frontend/paper_lantern/ntphpselector
	createDir /usr/local/cpanel/base/frontend/jupiter/ntphpselector
	cp -p index.live.php /usr/local/cpanel/base/frontend/paper_lantern/ntphpselector/
	cp -p index.live.php  /usr/local/cpanel/base/frontend/jupiter/ntphpselector/
	whmInstall
}
plugin_register() {
	createDir /usr/local/src/ntphpselector 
	echo "Registering cPanel plugin...."
	sleep 1
	cd /usr/local/src/ntphpselector
	echo "Downloading theme registering configuration...."
	wget -N http://download.nixtree.com/ntphp/ntphp_paperlantern.tar.gz > /dev/null 2>&1
	downloadCheck ntphp_paperlantern.tar.gz
	if [[ $1 == "register" ]]
		then 	/usr/local/cpanel/scripts/install_plugin ntphp_paperlantern.tar.gz
		elif [[ $1 == "unregister" ]] 	
		then
		/usr/local/cpanel/scripts/uninstall_plugin ntphp_paperlantern.tar.gz
		rm -rf /usr/local/cpanel/base/frontend/paper_lantern/ntphpselector/
		rm -f /usr/local/cpanel/base/frontend/paper_lantern/dynamicui/dynamicui_*ntPHP*
		/usr/local/cpanel/bin/rebuild_sprites -quiet
		fi
}
ea4Check() {
	# Check Easyapache 4 is enabled
	if rpm -qa | grep ea-apache24  > /dev/null; 
	then
		echo "yes"
	else 
		echo "no"
	fi
}

suphpCheck(){
	# Check SuPHP enabled
	if grep mod_suphp.so /etc/apache2/conf.modules.d/*.conf  >> /dev/null; then echo "yes" ;  else echo "no" ; fi
}

phpInstallCheck() {
	# Checking the PHP's are instaled
	versions=`ls -ld  /opt/cpanel/ea-php*  | wc -l`
	if [ $versions -lt 4 ] ; then echo "no"; else echo "yes" ;fi
}

oldInstallationCheck(){
	# Checking old ntPHPselector installation
	if grep ntPHPselector /etc/suphp.conf > /dev/null ; then echo "yes"; else echo "no" ; fi
}

installStatus(){
	echo -e '\nFollowing PHP Versions are installed in the server\n'
	for i in `\ls /opt/cpanel/*-php*/root/usr/bin/php-cgi | sort -V -t'-' -k2` ; do $i -v | grep built ; done
	echo
}
reqCheckStatus(){
	if [[ `ea4Check` == "yes" ]] && [[ `suphpCheck` == "yes" ]] && [[ `phpInstallCheck` == "yes" ]] && [[ `oldInstallationCheck` == "no" ]]
		then echo "yes"
		else echo "no"
		fi
}
reqCheck(){
	if [[ `reqCheckStatus` == "yes" ]]
        	then echo -e "\n\tAll Requirements meet for the ntPHPselector Installation\n\tYou can install it using \"sh $0 install\"\n"
	        else 
		echo -e "Check the requirements and try again\nPossible reasons are: "
		echo ""
		if [[ `ea4Check` == "no" ]] ; then echo -e "\tEasyapache 4 is not detected" ; fi
		if [[ `suphpCheck` == "no" ]] ; then echo -e "\tSuPHP not enabled" ; fi
		if [[ `phpInstallCheck` == "no" ]]; then echo -e "\tPHP versions are not installed" ; fi 
		if [[ `oldInstallationCheck` == "yes" ]] ; then echo -e "\tOld ntPHPselector installation detected";installStatus; fi 
		echo ""
		exit
		fi
}

check_connection(){
#	rm -f $0
	wget http://download.nixtree.com/ntphp/license.lock > /dev/null 2>&1
	if [ $? -ne 0 ] ; then echo -e  "\n\n\tLicensing Failed for your server, Please contact Nixtree support !!\n"; rm -f license.lock ; exit ; fi
	rm -f license.lock
}
checkRequirement(){
	line
	echo -e "\n\tChecking Requirements for ntPHPselector !!\n"
	line
	echo ""
	echo -e "Easyapache 4 is enabled\t\t: `ea4Check` (yes)"
	echo -e "SuPHP	enabled\t\t\t: `suphpCheck` (yes)"
	echo -e "Required PHP's are installed\t: `phpInstallCheck` (yes)"
	echo -e "Old Installation Detected\t: `oldInstallationCheck` (no)"
	echo ""
	reqCheck
}
confModification(){
    if [[ $1 == "add" ]] ; then
	createDir /usr/local/src/ntphpselector 
	cd  /usr/local/src/ntphpselector
	echo "Downlaoding configuration files..."
	wget -N http://download.nixtree.com/ntphp/ntconf.tgz > /dev/null 2>&1	
	downloadCheck ntconf.tgz
	tar -xzf ntconf.tgz
	cp -p ntconf/ntphp.conf /etc/apache2/conf.d/ntphp.conf
	cp -rp  $SUPHPCONFIG $SUPHPCONFIG.ntback
	LINEADD=`grep -n ";Handler for php-scripts" $SUPHPCONFIG -A 5|tail -1|cut -d- -f1`
	if [ -n $LINEADD ]; then
		L=$(expr $LINEADD + 1);
		if ! grep ntPHPselector $SUPHPCONFIG ; then
			file_content=`cat /usr/local/src/ntphpselector/ntconf/ntsuconf.txt` 
			sed -i "${L}i$file_content" $SUPHPCONFIG
			fi
		fi		
	service httpd restart
    elif [[ $1 == "remove" ]] ; 
	then
	rm -f /etc/apache2/conf.d/ntphp.conf
	cp -rp  $SUPHPCONFIG $SUPHPCONFIG.ntbackr
	sed -i -e '/ntPHPselector entry begins/,/ntPHPselector entry ends/d' $SUPHPCONFIG
	service httpd restart
	fi
}
installPlugin0(){
	plugin_register register
	install_files
	confModification add
	service httpd restart
	HTTP=`which httpd`;
	HTTP_CON=`ps auxf | grep $HTTP | grep -v grep  | wc -l ` ; 
	if [ $HTTP_CON -gt 0 ] ; then service httpd stop ; service httpd start ; fi 
	service httpd status
	if [ $? -eq 0 ] ; 
		then  	echo "Restarted Successfully" 
		else	echo -e "\n\tRestart Failed Check configuration or contact support..\n" ; exit;
		fi
	installStatus
	echo -e "\n\tntPHPselector is Successfully Installed !!\n"
}
updatePlugin(){
	cd /usr/local/src/
	wget -N http://download.nixtree.com/ntphp/ntphpselector-update.tar.gz
	tar -xzf ntphpselector-update.tar.gz
	rsync -avz ntphpselector-update/ntphp/ /usr/local/cpanel/whostmgr/docroot/cgi/ntphp/
#	cp -rp ntphpselector-update/addon_ntphp.cgi /usr/local/cpanel/whostmgr/docroot/cgi/addon_ntphp.cgi
#	cp -rp ntphpselector-update/ntphp.cron /etc/cron.d/ntphp.cron
        createDir /usr/local/cpanel/base/frontend/jupiter/ntphpselector
        cp -p ntphpselector-update/index.live.php  /usr/local/cpanel/base/frontend/jupiter/ntphpselector/
	confModification remove
	confModification add
	echo "ntPHPselector plugin files updated.."
	rm -fr ntphpselector-update
}
installPlugin(){
	line
	echo -e "\n\tInstalling ntPHPselector !!\n"
	line
	if [[ `reqCheckStatus` == "no" ]]
		then checkRequirement ; exit
		else installPlugin0
		fi
	clean_files
}
uninstallPlugin(){
	plugin_register unregister
	confModification remove
	echo -e "\n\tntPHPselector is Successfully removed !!\n"
	clean_files
}
recompilePhp(){
	line
	echo -e  "\n\tRecompilation of PHP 5.3 !!\n"
	line
	clean_files
}
unisntallFiles(){
	/usr/local/cpanel/bin/unregister_appconfig  /var/cpanel/apps/ntphp.conf
	rm -f /usr/local/cpanel/whostmgr/docroot/cgi/addon_ntphp.cgi
	rm -fr /usr/local/cpanel/whostmgr/docroot/cgi/ntphp
	rm -f /var/cpanel/apps/ntphp.conf
	rm -f /etc/cron.d/ntphp.cron
	rm -fr /opt/ntphp
	rm -fr /opt/cpanel/nt-php53 
}
installDevelrpm(){
	yum install -y epel-release
	yum install -y sqlite-devel.x86_64 bzip2-devel curl-devel enchant-devel t1lib-devel.x86_64 gmp-devel libc-client-devel libicu-devel openldap-devel libmcrypt-devel.x86_64 unixODBC-devel postgresql-devel aspell-devel libedit-devel.x86_64 recode-devel net-snmp-devel libtidy libtidy-devel pcre-devel
}
versionUpdate(){
>/opt/ntphp/versions.txt
for i in `ls  /opt/cpanel/*/root/usr/bin/php-cgi | sort -V -t'-' -k2` ; 
	do $i -v | grep built | awk '{print $2}'  >>  /opt/ntphp/versions.txt; 
	done
}
#Main
check_connection
echo 
echo "Please update your email from ntPHPselector front end from WHM to get the license related errors."
echo 
sleep 10
ntArg=$1
case $ntArg in
        install)
		installDevelrpm
		installPlugin
		clean_files
		versionUpdate
                break
                ;;
        uninstall)
	        line
	        echo -e "\n\tUninstalling ntPHPselector !!\n"
        	line
		uninstallPlugin
		unisntallFiles
                break
                ;;
	check)
		checkRequirement
		break
                ;;
	recompile)
		recompilePhp
		break
                ;;
	update)
		line		
		echo -e  "\n\tUpdating the ntPHPselector plugin !!\n"
		line
		updatePlugin
	#	uninstallPlugin
	#	installDevelrpm
	#	installPlugin
		clean_files
		versionUpdate
		break
		;;
        *)
                echo -e "Invalid option provided, please use \n\n\t sh $0 argument\n"
                echo -e "Possible arguments are\n\t install\t- Install ntPHPselector Plugin\n\t uninstall\t- Uninstall ntPHPselector Plugin"
#                echo -e "\t check\t\t- Check requirements\n\t recompile\t- Recompile php 5.3\n"
                echo -e "\t check\t\t- Check requirements\n"
                echo -e "\t update\t\t- Update the plugin\n"
                ;;
esac
