#!/bin/sh

# Modify these if building using newer versions
SRCDIR=/usr/src
APACHE=apache_1.3.12
MOD_PERL=mod_perl-1.22
RSAREF=rsaref20
OPENSSL=openssl-0.9.5
MM=mm-1.0.12
MOD_SSL=mod_ssl-2.6.2-1.3.12
INSTALLDIR=/Suite/apache

# Shouldn't have to change these unless packaging changes
APACHE_TGZ=${APACHE}.tar.gz
MOD_PERL_TGZ=${MOD_PERL}.tar.gz
RSAREF_TZ=${RSAREF}.tar.Z
OPENSSL_TGZ=${OPENSSL}.tar.gz
MM_TGZ=${MM}.tar.gz
MOD_SSL_TGZ=${MOD_SSL}.tar.gz

# Check for required package files
cd $SRCDIR
if [ ! -f $APACHE_TGZ ]
then
	echo "$0: ERROR: $APACHE_TGZ missing!"
	exit
fi

if [ ! -f $MOD_PERL_TGZ ]
then
	echo "$0: ERROR: $MOD_PERL_TGZ missing!"
	exit
fi

if [ ! -f $RSAREF_TZ ]
then
	echo "$0: ERROR: $RSAREF_TZ missing!"
	exit
fi

if [ ! -f $OPENSSL_TGZ ]
then
	echo "$0: ERROR: $OPENSSL_TGZ missing!"
	exit
fi

if [ ! -f $MM_TGZ ]
then
	echo "$0: ERROR: $MM_TGZ missing!"
	exit
fi

if [ ! -f $MOD_SSL_TGZ ]
then
	echo "$0: ERROR: $MOD_SSL_TGZ missing!"
	exit
fi

# Remove old Build Directories
if [ -d $APACHE ]
then
	echo "$0: NOTICE: Removing old build directory: $APACHE"
	rm -vfr $APACHE
fi

if [ -d $MOD_PERL ]
then
	echo "$0: NOTICE: Removing old build directory: $MOD_PERL"
	rm -vfr $MOD_PERL
fi

if [ -d $RSAREF ]
then
	echo "$0: NOTICE: Removing old build directory: $RSAREF"
	rm -vfr $RSAREF
fi

if [ -d $OPENSSL ]
then
	echo "$0: NOTICE: Removing old build directory: $OPENSSL"
	rm -vfr $OPENSSL
fi

if [ -d $MM ]
then
	echo "$0: NOTICE: Removing old build directory: $MM"
	rm -vfr $MM
fi

if [ -f $MOD_SSL ]
then
	echo "$0: NOTICE: Removing old build directory: $MOD_SSL"
	rm -vfr $MOD_SSL
fi

if [ -d $INSTALLDIR ]
then
	echo "$0: NOTICE: Removing current apache installation: $INSTALLDIR"
	rm -vfr $INSTALLDIR
fi

# Unpack the tarballs
tar xvzf $APACHE_TGZ
tar xvzf $MOD_PERL_TGZ
mkdir $RSAREF
cd $RSAREF
tar xvzf ../$RSAREF_TZ
cd ..
tar xvzf $OPENSSL_TGZ
tar xvzf $MM_TGZ
tar xvzf $MOD_SSL_TGZ

# Build RSAref
cd $SRCDIR/$RSAREF || exit
cp -rp install/unix local || exit
cd local || exit
make || exit
mv rsaref.a librsaref.a || exit

# Build OpenSSL
cd $SRCDIR/$OPENSSL || exit
./config -L$SRCDIR/$RSAREF/local/ rsaref -fPIC || exit
make || exit
make test

# Build mm
cd $SRCDIR/$MM || exit
./configure --disable-shared || exit
make

# Build mod_perl and add to apache source tree
cd $SRCDIR/$MOD_PERL || exit
perl Makefile.PL \
	APACHE_SRC=../$APACHE/src \
	DO_HTTPD=1 \
	USE_APACI=1 \
	PREP_HTTPD=1 \
	EVERYTHING=1 || exit
make || exit
make install || exit

# Add mod_ssl to apache source tree
cd $SRCDIR/$MOD_SSL || exit
./configure --with-apache=../$APACHE

# Build/Install apache
export SSL_BASE=../$OPENSSL
export RSA_BASE=../$RSAREF/local
export EAPI_MM=../$MM
cd $SRCDIR/$APACHE || exit
./configure --prefix=$INSTALLDIR \
	--activate-module=src/modules/perl/libperl.a \
	--enable-module=ssl \
	--disable-rule=SSL_COMPAT || exit
make || exit
make certificate || exit
make install || exit

rm -f $INSTALLDIR/conf/*.conf*
cp /Suite/httpd.conf $INSTALLDIR/conf/
cp /Suite/startup.pl $INSTALLDIR/conf/
