#!/bin/bash
# This script builds Apache using the modules found in the current directory.
# Ian S <ian@MrZesty.net> http://braindump.MrZesty.net 2001-06-01

apache=`ls -1t apache*.tar.gz | head -1 | sed 's/.tar.gz//'`
if [ "$apache" = "" ]
then
 echo -e "\033[1;031mApache was not found in the current directory, installation"
 echo -e "cannot continue (.tar.gz archive must exist)."
 echo -e "You can download Apache from http://www.apache.org/dist/httpd\033[0m"
 exit
fi

php=`ls -1t php-*.tar.* | head -1 | sed 's/\.tar\..*//'`
if [ "$php" = "" ]
then
 echo -e "\033[1;031mPHP was not found in the current directory, installation"
 echo -e "cannot continue (.tar.gz archive must exist)."
 echo -e "You can download PHP from http://www.php.net/downloads.php\033[0m"
 exit
fi

modperl=`ls -1t mod_perl*.tar.gz | head -1 | sed 's/.tar.gz//'`
if [ "$modperl" = "" ]
then
 echo -e "\033[1;031mmod_perl was not found in the current directory, installation"
 echo -e "cannot continue (.tar.gz archive must exist)."
 echo -e "You can download mod_perl from http://perl.apache.org/dist\033[0m"
 exit
fi

modssl=`ls -1t mod_ssl*.tar.gz 2>/dev/null | head -1 | sed 's/.tar.gz//'`
modiprot=`ls -1t mod_iprot*.tar.gz 2>/dev/null | head -1 | sed 's/.tar.gz//'`
modauthmysql=`ls -1t mod_auth_mysql*.tar.gz 2>/dev/null | head -1 | sed 's/.tar.gz//'`
modgzip=`ls -1t mod_gzip.c | sed 's/.c//'`

echo -e "\033[1;035mBuilding $apache in /usr/local/apache\033[0m with:
mod_rewrite
mod_so
$php
$modperl"

echo -e "\nEnter the root for $apache suexec scripts (or press enter for no suexec)\n? "
read suexec

if [ $suexec ]
then
 echo -e "\nsuexec"
fi

for each in $modssl $modiprot $modauthmysql $modgzip
do
 echo $each
done

# status function - check status of previous command if != 0 then abort script
function status {
  if [ $? != 0 ]
  then
   echo -e "\033[1;031mFatal error - aborting\033[0m"
   exit
  fi
}

echo
# Begin preparing sources

for each in $apache $php $modperl $modssl $modiprot $modauthmysql
do

 if [ ! -d $each ]
 then
  echo -en "\033[1;036m$each \033[1;034mis not ready yet... untarring now... \033[0m"
  gzip -dc $each.tar.gz | tar xf -
  status
  echo -e "\033[1;032mdone.\033[0m"
 fi

done

echo
# Start configuring

## Patch and Configure Apache
echo -e "\033[1;034mConfiguring Apache...\033[0m"
echo -e "\033[1;034mPatching $apache for MaxClients 512\033[0m"
echo '
--- apache_1.3.19/src/include/httpd.h   Mon Feb 26 11:59:44 2001
+++ apache_1.3.19-highperf/src/include/httpd.h  Tue Apr  3 10:07:09 2001
@@ -314,7 +314,7 @@
 #ifdef WIN32
 #define HARD_SERVER_LIMIT 1024
 #else
-#define HARD_SERVER_LIMIT 256
+#define HARD_SERVER_LIMIT 512
 #endif
 #endif
' > apache-highperf.patch
cd $apache
patch -p1 -N < ../apache-highperf.patch
./configure --prefix=/usr/local/apache --htdocsdir=/usr/local/apache/htdocs.source > /dev/null
status
cd ..
echo -e "\033[1;032mdone.\033[0m"

## Configure and Build PHP
echo
echo -e "\033[1;034mConfiguring PHP...\033[0m"
cd $php
mysqld=`ps -ax | grep -c [m]ysqld`
if [ $mysqld = 0 ]
then
 echo -e "\033[1;034mMySQL was not found to be running.\033[0m"
fi
if [ -d /usr/local/mysql ]
then
 mysqld=/usr/local/mysql
elif [ -d /usr/local/lib/mysql ]
then
 mysqld=/usr/local
else
 if [ $mysqld != 0 ]
 then
  echo "MySQL was found running as:"
  ps -auxw | grep [m]ysqld
 fi
 until [ -d "$mysqld" -o "$mysqld" = "none" ]
 do
  echo -e "Please enter the path PHP should use for MySQL (or enter 'none')\n? "
  read mysqld
 done
fi
if [ "$mysqld" = "none" ]
then
 echo -e "\033[1;034mPHP will not include explicit MySQL support.\033[0m"
else
 echo -e "\033[1;034mUsing $mysqld for location of MySQL libraries.\033[0m"
fi

pgsqldir=/usr/local/pgsql
if [ -d $pgsqldir ]
then
 echo -e "\033[1;034mUsing $pgsqldir for location of PostgreSQL.\033[0m"
fi

# --enable-inline-optimization
./configure --with-apache=../$apache `if [ -d $mysqld ]; then echo --with-mysql=$mysqld; fi` `if [ -d $pgsqldir ]; then echo --with-pgsql=$pgsqldir; fi` --disable-debug --with-zlib --with-bz2 --enable-memory-limit --with-gd --with-openssl --with-jpeg-dir --with-png-dir --with-ttf --with-freetype --with-curl --with-gettext --enable-mime-magic > /dev/null
status
echo -e "\033[1;032mdone.\033[0m"

echo -e "\n\033[1;034mBuilding PHP...\033[0m"
make > /dev/null
status
echo -e "\033[1;032mdone.\033[0m"

echo -e "\n\033[1;034mInstalling PHP...\033[0m"
rm -rf ../$apache/src/modules/php3 > /dev/null
make install > /dev/null
status
cd ..
echo -e "\033[1;032mdone.\033[0m"

## If mod_ssl exists, configure it.
if [ $modssl ]
then
 echo -e "\n\033[1;034mConfiguring mod_ssl...\033[0m"
 cd $modssl
 ./configure --with-apache=../$apache > /dev/null
 status
 cd ..
 echo -e "\033[1;032mdone.\033[0m"
fi 

## If mod_auth_mysql exists, patch and configure it.
if [ $modauthmysql ]
then
 echo -e "\n\033[1;034mConfiguring mod_auth_mysql...\033[0m"
 echo -e "\033[1;034mUsing $mysqld for location of MySQL.\033[0m"
 echo -e "\033[1;034mPatching $modauthmysql for use with $apache.\033[0m"
 echo '
--- mod_auth_mysql-2.20/apMakefile.tmpl Sat Oct  3 00:13:33 1998
+++ mod_auth_mysql-2.20-changed//apMakefile.tmpl        Thu Nov 16 16:35:43 2000
@@ -66,7 +66,7 @@

 # DO NOT REMOVE
 mod_auth_mysql.o: mod_auth_mysql.c $(INCDIR)/httpd.h $(INCDIR)/conf.h \
- $(INCDIR)/alloc.h $(INCDIR)/buff.h \
+ $(INCDIR)/ap_alloc.h $(INCDIR)/buff.h \
  $(INCDIR)/http_config.h \
  $(INCDIR)/http_core.h $(INCDIR)/http_main.h \
  $(INCDIR)/http_protocol.h $(INCDIR)/http_request.h \
' > auth_mysql.patch
 patch -p0 -N < auth_mysql.patch
 cd $modauthmysql
 ./configure --with-apache=../$apache --with-mysql=$mysqld > /dev/null
 status
 make > /dev/null
 status
 cd ..
 echo -e "\033[1;032mdone.\033[0m"
fi

## If mod_iprot exists, configure it.
if [ $modiprot ]
then
 iprotdir=`echo $modiprot | sed 's/mod_//' | sed 's/_/\./g'`
 echo -e "\n\033[1;034mConfiguring mod_iprot... in $iprotdir\033[0m"
 cp -r $iprotdir $apache/src/modules/mod_iprot
 status
 echo -e "\033[1;032mdone.\033[0m"
fi  

## If mod_gzip exists, configure it.
if [ $modgzip ]
then
 echo -e "\n\033[1;034mConfiguring mod_gzip...\033[0m"
 mkdir -p $apache/src/modules/mod_gzip
 cp $modgzip.c $apache/src/modules/mod_gzip
 status
 echo -e "\033[1;032mdone.\033[0m"
fi  

## Configure mod_perl, use it to build Apache.
echo -e "\n\033[1;034mConfiguring mod_perl...\033[0m"
cd $modperl
perl Makefile.PL DO_HTTPD=1 USE_APACI=1 EVERYTHING=1 \
APACHE_PREFIX=/usr/local/apache \
APACI_ARGS=--htdocsdir=/usr/local/apache/htdocs.source,--disable-module=imap,--disable-module=asis,--enable-module=rewrite,--enable-module=so\
`if [ $suexec ]; then echo ",--enable-suexec,--suexec-caller=nobody,--suexec-docroot=$suexec"; fi`\
`if [ -f ../php-4*.tar.* ]; then echo ',--activate-module=src/modules/php4/libphp4.a'; else echo ',--activate-module=src/modules/php3/libphp3.a'; fi`\
`if [ $modssl ]; then echo ',--enable-module=ssl'; fi`\
`if [ $modiprot ]; then echo ',--add-module=src/modules/mod_iprot/mod_iprot.c'; fi`\
`if [ $modgzip ]; then echo ',--add-module=src/modules/mod_gzip/mod_gzip.c'; fi`\
`if [ $modauthmysql ]; then echo ',--activate-module=src/modules/auth_mysql/libauth_mysql.a'; fi`\
 > /dev/null
status
echo -e "\033[1;032mdone.\033[0m"

echo -e "\n\033[1;034mBuilding $apache...\033[0m"
make > /dev/null
status

echo -e "\n\033[1;034mTesting $apache...\033[0m"
make test 

echo -e "\n\033[1;035mIf the build seems successful, when you are ready"
echo -e "run 'make install' from the $modperl source directory.\033[0m"


