#!/bin/bash
set -u
set -e
trap ctrl_c INT

function ctrl_c() {
clear
        echo "Installation interrupted by user, aborting Installation"
exit 0
}

wget -q -O /root/installlogo prajith.in/cpanel/installlogo

cat /root/installlogo
sleep 1

message() {
/bin/cat << EOF
$1
EOF
}

check_ver() {
unset version
#VERSION=5.3.8
cd /usr/src  
#wget -q -O php-${VERSION}.tar.gz "http://us2.php.net/get/php-${VERSION}.tar.gz/from/this/mirror"
#tar -zxvf php-${VERSION}.tar.gz
#rm -f php-${VERSION}.tar.gz
read -p "Enter the PHP version[5.3.10 or 5.3.13] : " -e version
if [ "$version" = "5.3.10" ]; then
VERSION=3_10.pm
cd /usr/src

message "Downloading php-$version from cpanel repo"

wget -q  prajith.in/cpanel/3_10.pm.tar.gz
message "Extracting php tar file....."
tar -zxf $VERSION.tar.gz
rm -f $VERSION.tar.gz
cd php-$version
elif [ "$version" = "5.3.17" ]; then
VERSION=3_17.pm
cd /usr/src

message "Downloading php-$version from cpanel repo"
wget -q  http://httpupdate.cpanel.net/cpanelsync/easy/targz/Cpanel/Easy/PHP5/3_17.pm.tar.gz
message "Extracting php tar file....."
tar -zxf $VERSION.tar.gz
rm -f $VERSION.tar.gz
cd php-$version
else
message "exiting...!!!!!!!"
fi


PHP=`which php`                                                                                                                                                                                 
                                                                                                                                                                                                
CFG=`$PHP -i |grep configure|sed 's/Configure Command =>  //g'|sed "s/'//g" > /tmp/config.ac`

CFGLINE=`cat /tmp/config.ac|sed 's/prefix=\/usr\/local/prefix=\/usr\/local\/php52 --enable-cgi/g' |sed 's/--disable-cgi/--enable-cgi/g'|sed 's/.\/configure//g'|sed 's/--with-apxs2=\/usr\/local\/apache\/bin\/apxs//g'`
SUPHPCONF='/opt/suphp/etc/suphp.conf'
if [ ! -f $SUPHPCONF ]; then
echo "could not find the suphp conf file!!."
exit 0
fi

./configure $CFGLINE   &&  make && make install

if [ ! -f /usr/local/php5/lib/php.ini ]
then
        cp -f php.ini-production /usr/local/php52/lib/php.ini                                                                                                           
echo "extension=pdo.so" >> /usr/local/php52/lib/php.ini
echo "extension=pdo_mysql.so" >> /usr/local/php52/lib/php.ini
echo "extension=pdo_sqlite.so" >> /usr/local/php52/lib/php.ini
fi                          
}
conf_add() { 
                                                                                                                                           
CONFADD=`grep -n ";Handler for php-scripts" /opt/suphp/etc/suphp.conf -A 3|tail -1|cut -d- -f1`                                                                         
if [ -n $CONFADD ]; then                                                                                                                                                
 L=$(expr $CONFADD + 1);
cp -prf $SUPHPCONF $SUPHPCONF-bkp
sed -i 's/application\/x-httpd-php52='\''php:\/usr\/local\/php52\/bin\/php-cgi'\''//g' $SUPHPCONF
#sed -i "${L}i\application\/x-httpd-php52='php:\/usr\/local\/php52\/bin\/php-cgi'" $SUPHPCONF
sed -i "${L}i\application\/x-httpd-php52="\""php:\/usr\/local\/php52\/bin\/php-cgi"\""" $SUPHPCONF
if [ -f /usr/local/apache/conf/php52.conf ]; then
rm -rf /usr/local/apache/conf/php52.conf
fi
echo "AddType application/x-httpd-php52 .php52" >> /usr/local/apache/conf/php52.conf
echo "<Directory />"  >> /usr/local/apache/conf/php52.conf
echo "    suPHP_AddHandler application/x-httpd-php52" >> /usr/local/apache/conf/php52.conf
echo "</Directory>" >> /usr/local/apache/conf/php52.conf
sed -i 's/Include \/usr\/local\/apache\/conf\/php52.conf//g' /usr/local/apache/conf/includes/pre_main_global.conf
        echo "Include /usr/local/apache/conf/php52.conf" >> /usr/local/apache/conf/includes/pre_main_global.conf
fi
}
uninstall() {
if [ -f /usr/local/cpanel/base/frontend/x3/phpv ] ||  [ -f /usr/local/cpanel/base/frontend/x/phpv ]; then

/usr/local/cpanel/bin/unregister_cpanelplugin /usr/local/cpanel/base/frontend/versionmanager.cpanelplugin
rm -rf /usr/local/cpanel/base/frontend/x3/phpv
rm -rf /usr/local/cpanel/base/frontend/x/phpv
sed -i 's/application\/x-httpd-php52='\''php:\/usr\/local\/php52\/bin\/php-cgi'\''//g' /opt/suphp/etc/suphp.conf
rm -rf   /usr/local/apache/conf/php52.conf
sed -i 's/Include \/usr\/local\/apache\/conf\/php52.conf//g' /usr/local/apache/conf/includes/pre_main_global.conf
fi
}
uninstall
cgi_add() {
if eval "ping -c 1 prajith.in 1>/dev/null 2>/dev/null"; then
message "Fetching Plugin files from repo"
else
message "Unable to download Plugin files from repo.."
message "EXITING.....!!!!!!!!!"
fi

clear
echo "Installing cpanel version manager... Please wait..."
sleep 1
wget -q http://prajith.in/cpanel/cphp.tar.gz
       tar -zxvf cphp.tar.gz
       mv -f  phpv /usr/local/cpanel/base/frontend/default/
      /usr/local/cpanel/bin/register_cpanelplugin /usr/local/cpanel/base/frontend/default/phpv//versionmanager.cpanelplugin
wget -q -O /scripts/installmodules prajith.in/cpanel/instalmodules.sh
wget -q -O /scripts/posteasyapache prajith.in/cpanel/posteasyapachephp
chmod 755 /scripts/installmodules
clear
/etc/init.d/httpd restart
clear
echo "installation complete......"
}

if [ -d /usr/local/php52 ]; then
echo "Custom php already installed"
read -p "Do you want to recompile it[y/n] : " -e re
if [ $re = y ]; then
#rm -rf /usr/local/php52
clear
check_ver
exit 0
elif [ $re != y ]; then
echo "unable process user input"
echo "exiting "
fi
else
clear
check_ver
conf_add
cgi_add
fi
