#!/bin/sh
# Apache build script, for version 2.x
#  In this build script, PHP requires suexec.
# 3/16/07

## Prerequisites - Postgres libs installed
## libxml2 MUST be installed for PHP5 to work.
if [ ! -f /usr/lib/libxml2.a ]; then
  echo "libxml2 is missing! Install it first."
  echo "On slackware, use the command 'swaret --install -a libxml2' to install."
  exit
fi

#####
# Apache 2.x
#   suexec
#   SSL
#   Compression (now mod_deflate and mod_headers)
#   see http://wiki.vhcs.net/en/index.php/HowTo:_Apache2_Gzip_Content-Encoding
#     http://httpd.apache.org/docs-2.0/mod/mod_deflate.html
#
# PHP (suexec) w/ pgsql, mysql, imap, ftp support
#
# mod_cband for throttling
#####

### Step 1: Configuration
SUEXEC=1
COMPRESS=1
CBAND=1
PHP=1
SSL=1
IMAP=0

SUEXEC_USER=httpd
SUEXEC_GROUP=nobody
SUEXEC_UIDMIN=999
SUEXEC_GIDMIN=100

APACHE_VERSION="2.0.59"
PHP_VERSION="5.2.1"
CBAND_VERSION="0.9.7.5"
PGSQL_DIR="/usr/local/pgsql"
MYSQL_DIR="/usr/local/mysql"

###
### Step 2: source code retrieval.
###

wget http://mirror.candidhosting.com/pub/apache/httpd/httpd-$APACHE_VERSION.tar.bz2
tar xjf httpd-$APACHE_VERSION.tar.bz2

wget --passive-ftp ftp://ftp.vectorstar.net/pub/apache-2.x/http-service-unavailable.patch
cd httpd-$APACHE_VERSION
patch -p0 < ../http-service-unavailable.patch
cd ..
if [ $CBAND == 1 ]; then
  wget http://cband.linux.pl/download/mod-cband-$CBAND_VERSION.tgz
  tar xzf mod-cband-$CBAND_VERSION.tgz
fi

if [ $PHP == 1 ]; then
  if [ $SUEXEC == 1 ]; then
    wget --passive-ftp ftp://ftp.vectorstar.net/pub/apache-2.x/httpd-2.x-suexec.patch
    cd httpd-$APACHE_VERSION
    patch -p0 < ../httpd-2.x-suexec.patch
    cd ..
  fi
  if [ $IMAP == 1 ]; then
    wget --passive-ftp ftp://ftp.cac.washington.edu/imap/imap.tar.Z
    tar zxf imap.tar.Z
  fi
  wget http://us2.php.net/distributions/php-$PHP_VERSION.tar.bz2
  tar xjf php-$PHP_VERSION.tar.bz2
fi

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


# ----- Apache 2.x
cd httpd-$APACHE_VERSION/
if [ $SUEXEC == 1 ]; then
	CONF_SUEXEC=" --enable-suexec \
	--with-suexec-caller=$SUEXEC_USER \
	--with-suexec-docroot=/vsn \
	--with-suexec-logfile=/usr/local/apache/logs/suexec_log \
	--with-suexec-uidmin=$SUEXEC_UIDMIN \
	--with-suexec-gidmin=$SUEXEC_GIDMIN"
fi
if [ $SSL == 1 ]; then
	CONF_SSL=" --enable-ssl"
fi
if [ $COMPRESS == 1 ]; then
	CONF_COMPRESS=" --enable-headers --enable-deflate";
fi
### Actually config and compile now
./configure \
	--prefix=/usr/local/apache \
	--enable-so --enable-cgi --enable-rewrite\
	$CONF_COMPRESS \
	$CONF_SUEXEC \
	$CONF_SSL

make
make install
cp support/split-logfile /usr/local/apache/bin/
chmod 755 /usr/local/apache/bin/split-logfile
# (installs to /usr/local/apache)
cd ..
# ----- end of core apache installation

# Mod_cband installation (using apxs)
if [ $CBAND == 1 ]; then
  cd mod-cband-$CBAND_VERSION
  ./configure --with-apxs=/usr/local/apache/bin/apxs
  make
  make install
  cd ..
fi
# ----- End of cband installation


if [ $PHP == 1 ]; then
  if [ $IMAP == 1 ]; then
    # ----- IMAP Support
    cd imap-*/
	# The following 3 commands hack the system so this busted software
	#  compiles cleanly.
	mkdir -p /usr/local/ssl
	ln -s /usr/include /usr/local/ssl/include
	ln -s /etc/ssl/certs /usr/local/ssl/certs
	make slx
	#copy the imap c-client directory to /usr/local/lib/c-client
	cp -RL c-client /usr/local/lib/
	# Assuming this is the mail server:
	ln -s /var/qmail/bin/sendmail /usr/sbin/sendmail
    cd ../
    $CONF_IMAP=" --with-imap=/usr/local/lib --with-imap-ssl"
  fi

  cd php-$PHP_VERSION
# cant get force_cgi_redir to work right! \
#	--enable-force-cgi-redirect \
  ./configure \
	$CONF_IMAP \
	--enable-discard-path \
	--enable-ftp \
	--enable-memory-limit \
 	--with-pgsql=$PGSQL_DIR
# 	--with-pgsql=$PGSQL_DIR \
#	--with-mysql=$MYSQL_DIR
  make
  make install
  cd ..

  echo "Adding symlinks for php3,4,5..."
  ln -s /usr/local/bin/php /usr/local/bin/php3
  ln -s /usr/local/bin/php /usr/local/bin/php4
  ln -s /usr/local/bin/php /usr/local/bin/php5

  echo "setting up sessions directory..."
  mkdir -p /usr/local/lib/php/sessions
  chown www /usr/local/lib/php/sessions
  chgrp staff /usr/local/lib/php/sessions
  chmod 770 /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."
#  echo "A good php.ini can be found in /vsn/backups/www."
fi


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

