#!/bin/sh
# Apache and friends build script
# 2/2/2003

#####
# Apache / suexec
# Mod Gzip
# Mod Perl
# Mod PHP (built in) w/ pgsql, imap, ftp support
#   xor
# PHP-suexec (standalone) w/ pgsql, imap, ftp support
# Mod SSL
# Mod Throttle
#####

### Step 1: Configuration
SUEXEC=1
MODGZIP=1
MODPERL=0
# You can have mod_php or php-suexec, but NOT BOTH!
MODPHP=0
PHPSUEXEC=1
MODSSL=0
MODTHROTTLE=1

SUEXEC_USER=httpd
SUEXEC_GROUP=nogroup
SUEXEC_UIDMIN=999
SUEXEC_GIDMIN=99

APACHE_VERSION="1.3.27"
PHP_VERSION="4.3.0"
MODSSL_VERSION="2.8.11"
MODPERL_VERSION="1.27"
MODTHROTTLE_VERSION_SHORT="312"
MODTHROTTLE_VERSION_LONG="3.1.2"
### PostgreSQL must be compiled beforehand if you want this to work.
PGSQL_DIR="/usr/local/pgsql"

### Step 2: source code retrieval.

wget http://httpd.apache.org/dist/httpd/apache_$APACHE_VERSION.tar.gz
tar zxf apache_$APACHE_VERSION.tar.gz
wget ftp://ftp.vectorstar.net/pub/apache/http_protocol_vectorstar.patch
cd apache_$APACHE_VERSION
patch -p0 < ../httpd_protocol_vectorstar.patch
cd ..

if [ $MODSSL == 1 ]; then
  wget http://www.modssl.org/source/mod_ssl-$MODSSL_VERSION-$APACHE_VERSION.tar.gz
  tar zxf mod_ssl-$MODSSL_VERSION-$APACHE_VERSION.tar.gz
fi

### If you are doing either PHP or PHP-suexec, grab this
if [ $MODPHP == 1 || $PHPSUEXEC == 1 ]; then
  wget http://www.php.net/distributions/php-$PHP_VERSION.tar.gz
  tar zxf php-$PHP_VERSION.tar.gz

  wget --passive-ftp ftp://ftp.cac.washington.edu/imap/imap.tar.Z
  tar zxf imap.tar.Z
fi

if [ $PHPSUEXEC == 1 ]; then
  wget ftp://ftp.vectorstar.net/pub/apache/apache-php-suexec-1.3.27.patch
  cd apache_$APACHE_VERSION
  patch -p0 < ../apache-php-suexec-1.3.27.patch
  cd ..
fi

if [ $MODPERL == 1 ]; then
  wget http://perl.apache.org/dist/mod_perl-$MODPERL_VERSION.tar.gz
  tar zxf mod_perl-$MODPERL_VERSION.tar.gz
fi

if [ $MODTHROTTLE == 1 ]; then
  wget http://www.snert.com/Software/mod_throttle/mod_throttle$MODTHROTTLE_VERSION_SHORT.tgz
  tar zxf mod_throttle$MODTHROTTLE_VERSION_SHORT.tgz
  wget ftp://ftp.vectorstar.net/pub/apache/mod_throttle-3.1.2-fixnis.patch
  patch -p0 < mod_throttle-3.1.2-fixnis.patch
fi

if [ $MODGZIP == 1 ]; then
  wget http://telia.dl.sourceforge.net/sourceforge/mod-gzip/mod_gzip-1.3.26.1a.tgz
  tar xzf mod_gzip-1.3.26.1a.tgz
fi

###
### Step 3: Compilation.
###

# ----- mod_ssl
if [ $MODSSL == 1 ]; then
  cd mod_ssl-$MODSSL_VERSION-$APACHE_VERSION/
  ./configure --with-apache=../apache_$APACHE_VERSION
  cd ..
fi

# ----- Apache 1.3
cd apache_$APACHE_VERSION/
if [ $SUEXEC == 1 ]; then
	CONF_SUEXEC=" --enable-suexec \
	--suexec-caller=$SUEXEC_USER \
	--suexec-docroot=/vsn \
	--suexec-logfile=/usr/local/apache/logs/suexec_log \
	--suexec-uidmin=$SUEXEC_UIDMIN \
	--suexec-gidmin=$SUEXEC_GIDMIN"
fi
if [ $MODSSL == 1 ]; then
	CONF_MODSSL=" --enable-module=ssl"
fi
### Actually config and compile now
./configure \
	--prefix=/usr/local/apache \
	--enable-module=so \
	$CONF_SUEXEC \
	$CONF_MODSSL
make
make install
# (installs to /usr/local/apache)
cd ..
# ----- end of core apache installation

if [ $MODPHP == 1 || $PHPSUEXEC == 1 ]; then
  # ----- IMAP Support
  cd imap-*/
	# The EXTRACFLAGS fixes imap's retarded include options.
	make slx EXTRACFLAGS=-I/usr/include/openssl
	#copy the imap c-client directory to /usr/local/lib/c-client
	cp -RL c-client /usr/local/lib/
  cd ../
fi

if [ $MODPHP == 1 ]; then
  # ----- PHP
  cd php-$PHP_VERSION
  ./configure --with-apxs=/usr/local/apache/bin/apxs \
	--enable-force-cgi-redirect \
	--enable-discard-path \
	--enable-ftp \
	--enable-memory-limit \
	--enable-ucd-snmp-hack \
	--with-imap=/usr/local/lib \
 	--with-pgsql=$PGSQL_DIR
  make
  make install
  cd ..
fi
# End if mod_php

if [ $PHPSUEXEC == 1 ]; then
  cd php-$PHP_VERSION
  ./configure --enable-force-cgi-redirect \
	--enable-discard-path \
	--enable-ftp \
	--enable-memory-limit \
	--enable-ucd-snmp-hack \
	--with-imap=/usr/local/lib \
	--with-imap-ssl=/usr/local/lib \
 	--with-pgsql=$PGSQL_DIR
  make
  make install
  cd ..
fi

#if [ $MODPHP == 1 || $PHPSUEXEC == 1 ]; then
#  if [ $SUEXEC == 1 ]; then
#    # This is actually for our webmail. Should no longer be needed since webmail
#    # is no longer on the main web server. it has its own custom install.
#    # Of course, webmail code will have to be modified to not put cookies there.
#    mkdir -p /usr/local/lib/php/sessions
#    chown $SUEXEC_USER /usr/local/lib/php/sessions
#    chgrp $SUEXEC_GROUP /usr/local/lib/php/sessions
#    chmod 700 /usr/local/lib/php/sessions
#    echo "You may need a php.ini file to make PHP write session files to /usr/local/lib/php/sessions. this might have been fixed since 4.0.4 tho."
#  fi
#fi

# ----- mod_perl, compiled with much blood, sweat and tears
if [ $MODPERL == 1 ]; then
  cd mod_perl-$MODPERL_VERSION
  perl Makefile.PL \
    USE_APXS=1 \
    WITH_APXS=/usr/local/apache/bin/apxs \
    PERL_USELARGEFILES=0 \
    EVERYTHING=1
  make && \
  make install
  cd ..
fi

if [ $MODTHROTTLE == 1 ]; then
  # ----- mod_throttle. It has already been patched for NIS at this point.
  cd mod_throttle-$MODTHROTTLE_VERSION_LONG/
  make APXS=/usr/local/apache/bin/apxs
  make APXS=/usr/local/apache/bin/apxs install
  cd ..
fi

# ----- mod_gzip.
if [ $MODGZIP == 1 ]; then
  cd mod_gzip-1.3.26.1a
  make APXS=/usr/local/apache/bin/apxs
  make APXS=/usr/local/apache/bin/apxs install
  cd ..
fi

echo
echo "All set. Do your configuration files now!"
echo

