#!/bin/bash
# apache httpd installer
###SHELLPACK preamble apachebuild-install 2.4.39

APR_VERSION=1.7.0
PCRE_VERSION=8.43
PHP_VERSION=7.3.5

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

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

###SHELLPACK sources_fetch httpd-${VERSION}.tar.gz apachebuild-${VERSION}
if [ ! -e $SHELLPACK_SOURCES/aprbuild-${APR_VERSION}-installed ]; then
	$SHELLPACK_TOPLEVEL/shellpack_src/src/refresh.sh aprbuild
	$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_TOPLEVEL/shellpack_src/src/refresh.sh pcrebuild
	$SHELLPACK_INCLUDE/shellpack-install-pcrebuild -v ${PCRE_VERSION} || die Failed to install pcre
fi

# Build Apache
###SHELLPACK build_start apachebuild-${VERSION}
###SHELLPACK build_configure apachebuild-${VERSION} --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
export MAKE_RETRY=yes
###SHELLPACK make_make_install

# 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_TOPLEVEL/shellpack_src/src/refresh.sh phpbuild
	$SHELLPACK_INCLUDE/shellpack-install-phpbuild -v ${PHP_VERSION} || die Failed to install php
fi

echo httpd installed successfully
