
#!/bin/sh

###########
# need to replace the following
# 	PHP_VERSION = {PHP_VERSION}
# 	PHP_CONF_OPTIONS = {PHP_CONF_OPTIONS}
#	LSWS_HOME = {LSWS_HOME}
############

check_errs()
{
  if [ "${1}" -ne "0" ] ; then
     
    echo "*********************************************"
    echo "**"
    echo "**  BUILD MATCHING PHP FAILED  **************"
    echo "**" 
    echo "**  -- ERROR -- ${2}"
    echo "*********************************************"
     
    exit ${1}
  fi
}

test_phpsrc_ok()
{
	echo "Extracting PHP source archive: tar -zxf ${1}" 
	tar -zxf ${1}
	if [ "$?" -ne "0" ] ; then
		## remove bad copy
		rm -f ${1}
		echo "Could not extract PHP source archive"
		return 1
	fi
	return 0
}

echo ""
echo "=============================================="
echo "Start building PHP {PHP_VERSION} with LSAPI"
echo "=============================================="
echo `date`
echo ""

ulimit -m unlimited
ulimit -v unlimited

# trying to detect src dir
if [ -d "/usr/home/cpeasyapache" ] ; then
	BASE_SRC=/usr/home/cpeasyapache
elif [ -d "/home/cpeasyapache" ] ; then
	BASE_SRC=/home/cpeasyapache
elif [ -d "/var/cpanel/cpeasyapache" ] ; then
	BASE_SRC=/var/cpanel/cpeasyapache
else
	check_errs 1 "Cannot find EasyApache PHP source directory, please try to run EasyApache first."
fi 

PHP_BUILD_DIR=${BASE_SRC}/ls.src
CP_BUILD_DIR=${BASE_SRC}/src

if [ ! -e "${PHP_BUILD_DIR}" ] ; then
	mkdir -p ${PHP_BUILD_DIR}
	check_errs $? "Could not create build directory ${PHP_BUILD_DIR}"
fi

echo "Changing to build directory ${PHP_BUILD_DIR}" 
cd ${PHP_BUILD_DIR}
check_errs $? "Could not get into build directory"

if [ -e php-{PHP_VERSION} ] ; then
	rm -rf php-{PHP_VERSION}
	check_errs $? "Could not delete old php directory"
fi

if [ -e "${CP_BUILD_DIR}/php-{PHP_VERSION}" ] ; then
	echo "Detected php build directory for cPanel ${CP_BUILD_DIR}/php-{PHP_VERSION}"
	echo "Same source code will be copied over"
	cp -rp "${CP_BUILD_DIR}/php-{PHP_VERSION}" php-{PHP_VERSION} 
	check_errs $? "Failed to copy over php source directory"
	
else
	check_errs 1 "Cannot find php build directory for cPanel, please run EasyApache first to build PHP with correct options for Apache."
fi

echo ""


DOWNLOAD_URL="http://update.litespeedtech.com/ws/latest.php"
LSAPI_VERSION=`wget -q --output-document=- $DOWNLOAD_URL`
LSAPI_VERSION=`expr "$LSAPI_VERSION" : '.*PHP-LSAPI=\([0-9\.]*\)'`
echo "Query lastest LSAPI version: $LSAPI_VERSION"

# to do: should use md5 check
if [ -e "php-litespeed-$LSAPI_VERSION.tgz" ] ; then
	rm -f "php-litespeed-$LSAPI_VERSION.tgz"
	check_errs $? "Could not delete old lsapi"
fi

DOWNLOAD_URL="http://www.litespeedtech.com/packages/lsapi/php-litespeed-$LSAPI_VERSION.tgz"
echo "Retrieving LSAPI archive"
wget -nv -O "php-litespeed-$LSAPI_VERSION.tgz" $DOWNLOAD_URL
check_errs $? "Could not retrieve LSAPI archive"

cd php-{PHP_VERSION}/sapi
check_errs $? "Could not get into php/sapi directory"

if [ -e litespeed/Makefile.frag ] ; then
	mv -f litespeed/Makefile.frag litespeed/Makefile.frag.package 
fi

echo "Extracting LSAPI archive: tar -xzf php-litespeed-$LSAPI_VERSION.tgz" 
tar -xzf "../../php-litespeed-$LSAPI_VERSION.tgz"
check_errs $? "Could not extract LSAPI archive"

if [ -e litespeed/Makefile.frag.package ] ; then
	mv -f litespeed/Makefile.frag.package litespeed/Makefile.frag 
fi

echo ""

echo "Finished gathering all the source code"
echo ""

echo "Generating configuration script"
cd ..
check_errs $? "Could not get into php directory"

sleep 1
touch ac*
check_errs $? "Could not touch ac*"

rm -rf autom4te.*

PHP_MAIN_VER=`expr "{PHP_VERSION}" : '\([0-9]*\.[0-9]*\)'`

PHP_MAIN_VER1=`expr "$PHP_MAIN_VER" : '\([0-9]*\)\.'`
PHP_MAIN_VER2=`expr "$PHP_MAIN_VER" : '[0-9]*\.\([0-9]*\)'`

BUILDCONF_FORCE=N
if [ "$PHP_MAIN_VER1" -lt "5" ] ; then
    BUILDCONF_FORCE=Y
elif [ "$PHP_MAIN_VER1" = "5" ] && [ "$PHP_MAIN_VER2" -lt "3" ] ; then
    BUILDCONF_FORCE=Y
fi

if [ "$BUILDCONF_FORCE" = "Y" ] ; then
	./buildconf --force
	check_errs $? "Could not generate configuration script for version prior to 5.3"
fi

rm config.cache

echo "Configuring PHP build (2-3 minutes)" 
echo "{PHP_CONF_OPTIONS}"

OS=`uname -s`

if [ "x$OS" = "xFreeBSD" ]; then
     CFLAGS='-O3' LDFLAGS='-L/usr/local/lib' {PHP_CONF_OPTIONS}
else
     CFLAGS='-O3' {PHP_CONF_OPTIONS}
fi

check_errs $? "Could not configure PHP build"

cp -fp "${CP_BUILD_DIR}/php-{PHP_VERSION}/libtool" . 
PLF=`uname -p`
if [ "x$PLF" = "xx86_64" ] ; then
	# work around for libtool problem for linux
	DLSCH=`grep 'sys_lib_dlsearch_path_spec="/lib /usr/lib ' libtool`
	if [ "x$DLSCH" != "x" ] ; then
		echo "  .. work around for libtool problem: sys_lib_dlsearch_path_spec should use lib64"
		cp libtool libtool.orig
		sed -e 's/sys_lib_dlsearch_path_spec=\"\/lib \/usr\/lib /sys_lib_dlsearch_path_spec=\"\/lib64 \/usr\/lib64 /' libtool.orig > libtool
	    if [ "$?" -ne "0" ] ; then
			echo "   sed command error, please try to modify libtool manually using lib64 for line: sys_lib_dlsearch_path_spec=\"/lib /usr/lib\" "
	    fi	
	fi
fi

find . -name '*.1' > /tmp/php-1.lst.$$
tar -cf /tmp/php-1.tar.$$ -T /tmp/php-1.lst.$$
make clean
tar -xf /tmp/php-1.tar.$$
rm -f /tmp/php-1.tar.$$ /tmp/php-1.lst.$$

echo "Compiling PHP (5-10 minutes)" 
echo `date`
make
check_errs $? "Could not compile PHP"

make install

PHP_INI=/usr/local/lib/php.ini

ext=`grep '^zend_extension=".*\/ioncube' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/ioncube*
    if [ $? -eq 0 ]; then
		echo "Build IonCube ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="eaccelerator' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/eaccelerator*
    if [ $? -eq 0 ]; then
		echo "Build eaccelerator ..."
		echo "  .. Special handling for eAccelerator: do not use spin lock, buggy"
		echo "     set mm_sem_spinlock=no and mm_sem_ipc=no in config.m4"
		
		found=`find . -name "config.m4"`

		for f in $found
		do
		    cp $f $f.orig
		    sed -e 's/mm_sem_spinlock=yes/mm_sem_spinlock=no/' -e 's/mm_sem_ipc=yes/mm_sem_ipc=no/' $f.orig > $f
		    if [ "$?" -ne "0" ] ; then
				echo "   sed command error, please try to modify $f manually to avoid using spinlock"
		    fi
		done
		
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="suhosin' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/suhosin*
    if [ $? -eq 0 ]; then
		echo "Build suhosin ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension="ixed\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/sourceguardian*
    if [ $? -eq 0 ]; then
		echo "Build sourceguardian ..."
		./cpanel-install
		cd ..
    fi
fi

ext=`grep '^extension=".*homeloader\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/userphp
    if [ $? -eq 0 ]; then
		echo "Build homeloader ..."
		./configure
		make install
		cd ..
    fi
fi

ext=`grep '^zend_extension=".*ZendOptimizer\.' $PHP_INI`
if [ $? -eq 0 ]; then
    cd ${CP_BUILD_DIR}/zendopt
    if [ $? -eq 0 ]; then
		echo "Build Zend Optimizer ..."
		./cpanel-install
		cd ..
    fi
fi

cp -p /usr/local/lib/php.ini /usr/local/lib/php.ini-lsbak
sed -e "s/^extension_dir/;extension_dir/" /usr/local/lib/php.ini-lsbak > /usr/local/lib/php.ini

chmod a+r /usr/local/lib/php.ini

echo "copy compiled php binary to litespeed directory"

echo "cd {LSWS_HOME}/fcgi-bin"
cd {LSWS_HOME}/fcgi-bin
check_errs $? "cannot cd to {LSWS_HOME}/fcgi-bin"

if [ -e "lsphp-{PHP_VERSION}" ] ; then
	mv lsphp-{PHP_VERSION} lsphp-{PHP_VERSION}.bak
fi

if [ -e "/usr/local/bin/lsphp" ] ; then
	mv /usr/local/bin/lsphp /usr/local/bin/lsphp.bak
fi

cp ${PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php lsphp-{PHP_VERSION}
check_errs $? "fail to copy lsphp from ${PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php"

chmod a+rx lsphp-{PHP_VERSION}

cp ${PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php /usr/local/bin/lsphp
check_errs $? "fail to copy ${PHP_BUILD_DIR}/php-{PHP_VERSION}/sapi/litespeed/php to /usr/local/bin/lsphp"

chmod a+rx /usr/local/bin/lsphp


PHP_MAIN_VERSION=`expr "{PHP_VERSION}" : '\([0-9]*\)\.'`

echo "ln -sf lsphp-{PHP_VERSION} lsphp$PHP_MAIN_VERSION"
ln -sf lsphp-{PHP_VERSION} "lsphp$PHP_MAIN_VERSION" 
check_errs $? "fail to creat symbolic link"
 
echo ""
echo "=============================================="
echo "Finished building PHP {PHP_VERSION} with LSAPI"
echo "=============================================="
echo `date`
echo "**COMPLETE**