#!/bin/bash
# apache httpd installer
P=apachebuild-install
DEFAULT_VERSION=2.4.17
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

APR_VERSION=1.5.2
PCRE_VERSION=8.37
PHP_VERSION=5.6.15

WEB_LOCATION=http://ftp.heanet.ie/mirrors/www.apache.org/dist/httpd
WEB_LOCATION_ALT=https://archive.apache.org/dist/httpd/
MIRROR_LOCATION="$WEBROOT/apache/"

install-depends gcc gcc-c++ libxml2-devel bison flex

# 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=httpd-${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 httpd-${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 apachebuild-${VERSION}
pushd apachebuild-${VERSION} > /dev/null || die Failed to rename tar
if [ ! -e $SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed ]; then
	$SHELLPACK_INCLUDE/shellpack-install-aprbuild -v ${APR_VERSION} || die Failed to install apr and apr-utils
fi
if [ ! -e $SHELLPACK_SOURCES/pcre-${PCRE_VERSION}-installed ]; then
	$SHELLPACK_INCLUDE/shellpack-install-pcrebuild -v ${PCRE_VERSION} || die Failed to install pcre
fi

# Build Apache
pushd $SHELLPACK_SOURCES/apachebuild-${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/apachebuild-${VERSION}-installed --with-apr=$SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed --with-apr-util=$SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed --with-pcre=$SHELLPACK_SOURCES/pcrebuild-${PCRE_VERSION}-installed --enable-so --disable-ssl
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/apachebuild-${VERSION}-installed --with-apr=$SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed --with-apr-util=$SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed --with-pcre=$SHELLPACK_SOURCES/pcrebuild-${PCRE_VERSION}-installed --enable-so --disable-ssl
	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

# remove User and Group directives from httpd config file, so that it's executed as the current user
sed -i 's/^User .*$//' $SHELLPACK_SOURCES/apachebuild-${VERSION}-installed/conf/httpd.conf || die Failed to remove User directive from httpd.conf
sed -i 's/^Group .*$//' $SHELLPACK_SOURCES/apachebuild-${VERSION}-installed/conf/httpd.conf || die Failed to remove Group directive from httpd.conf

# Install PHP (module) -- requires httpd to previously be built
if [ ! -e $SHELLPACK_SOURCES/phpbuild-${PHP_VERSION}-installed ]; then
	$SHELLPACK_INCLUDE/shellpack-install-phpbuild -v ${PHP_VERSION} || die Failed to install php
fi

echo httpd installed successfully
#### Description apachebuild
#### Details apachebuild 20
