#!/bin/bash
# PHP installer for Apache httpd.
P=phpbuild-install
DEFAULT_VERSION=5.6.15
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
WEB_LOCATION=http://linux-scalability.org/benchmarks/
MIRROR_LOCATION="$WEBROOT/php/"

PHP_VERSION=5.6.15
APACHE_VERSION=2.4.17

# PHP XCache version to use for PHP 5.6
XCACHE_VERSION=3.2.0

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

# Unconditionally fetch the tar to find out the real version number
TARFILE=php-${PHP_VERSION}.tar.gz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf php-${PHP_VERSION}.tar.gz failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR phpbuild-${PHP_VERSION}
pushd phpbuild-${PHP_VERSION} > /dev/null || die Failed to rename tar
pushd $SHELLPACK_SOURCES/phpbuild-${PHP_VERSION} || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
export CFLAGS="-O2 $CFLAGS_MMTESTS_EXTRA"
eval ./configure --prefix=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed --with-apxs2=$SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/bin/apxs --with-mysql --with-config-file-path=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed --with-apxs2=$SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/bin/apxs --with-mysql --with-config-file-path=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make install
if [ $? -ne 0 ]; then
	error "$P: make install failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# post build crap
cp $SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}/php.ini-development $SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/php.ini

printf '%s\n\t%s\n%s\n\n' '<FilesMatch "\.ph(p[2-6]?|tml)$">' 'SetHandler application/x-httpd-php' '</FilesMatch>' >> $SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/conf/httpd.conf

printf '%s\n\t%s\n%s\n' '<FilesMatch "\.phps$">' 'SetHandler application/x-httpd-php-source' '</FilesMatch>' >> $SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/conf/httpd.conf

echo Generating phpinfo.php file
echo "<?php phpinfo(); ?>" >> $SHELLPACK_SOURCES/apachebuild-${APACHE_VERSION}-installed/htdocs/phpinfo.php

WEB_LOCATION=https://xcache.lighttpd.net/pub/Releases/$XCACHE_VERSION
# Unconditionally fetch the tar to find out the real version number
TARFILE=xcache-$XCACHE_VERSION.tar.gz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf xcache-$XCACHE_VERSION.tar.gz failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR xcache-$XCACHE_VERSION
pushd xcache-$XCACHE_VERSION > /dev/null || die Failed to rename tar
$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/bin/phpize
export CFLAGS="-O2 $CFLAGS_MMTESTS_EXTRA"
eval ./configure --prefix=$SHELLPACK_SOURCES/xcode-${XCACHE_VERSION}-installed --enable-xcache --with-php-config=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/bin/php-config
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/xcode-${XCACHE_VERSION}-installed --enable-xcache --with-php-config=$SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/bin/php-config
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make install
if [ $? -ne 0 ]; then
	error "$P: make install failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

cat xcache.ini >> $SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed/php.ini

echo php installed successfully
#### Description phpbuild
#### Details phpbuild 9
