#!/bin/sh
# @(#) compile apache 1.3 with mod_perl, embperl 1.3.7, mod_ssl, php5, mod_gzip
# (c) 2007..2010 by Dirk Jagdmann <doj@cubic.org>
# tested with perl 5.10

APACHEVER=1.3.41
MODPERLVER=1.31
MODSSLVER=2.8.31-$APACHEVER
EMBPERLVER=1.3.7
PHPVER=5.3.2
GZIPVER=1.3.26.1a

APACHEPREFIX=/usr/local/apache-$APACHEVER # /opt/apache

APACHEDOWN=http://archive.apache.org/dist/httpd/
MODPERLDOWN=ftp://ftp.uni-erlangen.de/pub/source/CPAN/modules/by-module/Apache
MODSSLDOWN=ftp://ftp.modssl.org/source
EMBPERLDOWN=https://llg.cubic.org/tools
PHPDOWN=http://de2.php.net/get
GZIPDOWN=http://easynews.dl.sourceforge.net/sourceforge/mod-gzip

APXS=$APACHEPREFIX/bin/apxs

APACHEOPT="--disable-module=auth_db --disable-module=auth_dbm"
MODPERLOPT=
MODSSLOPT=
EMBPERLOPT=
PHPOPT="--with-apxs=$APXS --with-zlib --enable-dba --with-db4 --enable-exif --enable-ftp --with-pgsql --with-sqlite --with-snmp --with-openssl --with-imap-ssl"

CFLAGS=-O2
MAKEFLAGS="-j4"

##############################################################################

export CFLAGS

APACHE=apache_$APACHEVER
MODPERL=mod_perl-$MODPERLVER
MODSSL=mod_ssl-$MODSSLVER
EMBPERL=embperl-$EMBPERLVER
PHP=php-$PHPVER
GZIP=mod_gzip-$GZIPVER

APACHEFILE=$APACHE.tar.gz
MODPERLFILE=$MODPERL.tar.gz
MODSSLFILE=$MODSSL.tar.gz
EMBPERLFILE=$EMBPERL.tar.gz
PHPFILE=$PHP.tar.bz2
GZIPFILE=$GZIP.tgz

function clean()
{
  rm -rf $MODSSL
  rm -rf $MODPERL
  rm -rf $APACHE
  rm -rf $EMBPERL
  rm -rf $PHP
  rm -rf $GZIP
}

function distclean()
{
  clean
  rm -f $MODSSLFILE
  rm -f $MODPERLFILE
  rm -f $APACHEFILE
  rm -f $EMBPERLFILE
  rm -f $PHPFILE
  rm -f $GZIPFILE
}

function download
{
  echo downloading...
if [ ! -f $APACHEFILE ]
then wget $APACHEDOWN/$APACHEFILE
     if [ ! -f $APACHEFILE ]
     then echo could not download $APACHEFILE
          exit 1
     fi
fi

if [ ! -f $MODPERLFILE ]
then wget $MODPERLDOWN/$MODPERLFILE
     if [ ! -f $MODPERLFILE ]
     then echo could not download $MODPERLFILE
          exit 1
     fi
fi

if [ ! -f $MODSSLFILE ]
then wget $MODSSLDOWN/$MODSSLFILE
     if [ ! -f $MODSSLFILE ]
     then echo could not download $MODSSLFILE
          exit 1
     fi
fi

if [ ! -f $EMBPERLFILE ]
then wget $EMBPERLDOWN/$EMBPERLFILE
     if [ ! -f $EMBPERLFILE ]
     then echo could not download $EMBPERLFILE
          exit 1
     fi
fi

if [ ! -f $PHPFILE ]
then wget $PHPDOWN/$PHPFILE/from/this/mirror
     if [ ! -f $PHPFILE ]
     then echo could not download $PHPFILE
          exit 1
     fi
fi

if [ ! -f $GZIPFILE ] ; then
    wget $GZIPDOWN/$GZIPFILE
    if [ ! -f $GZIPFILE ] ; then
	echo could not download $GZIPFILE
	exit 1
    fi
fi
}

function extract
{
  echo extracting...
if [ ! -d $APACHE ]
then if ! tar xfz $APACHEFILE
     then echo could not extract $APACHEFILE
          exit 1
     fi
fi

if [ ! -d $MODPERL ]
then if ! tar xfz $MODPERLFILE
     then echo could not extract $MODPERLFILE
          exit 1
     fi
fi

if [ ! -d $MODSSL ]
then if ! tar xfz $MODSSLFILE
     then echo could not extract $MODSSLFILE
          exit 1
     fi
fi

if [ ! -d $EMBPERL ]
then if ! tar xfz $EMBPERLFILE
     then echo could not extract $EMBPERLFILE
          exit 1
     fi
fi

if [ ! -d $PHP ]
then if ! tar xfj $PHPFILE
     then echo could not extract $PHPFILE
          exit 1
     fi
fi

if [ ! -d $GZIP ] ; then
    if ! tar xfz $GZIPFILE ; then
	echo could not extract $GZIPFILE
	exit 1
    fi
fi
}

function build
{
##### SSL

echo configuring modssl
cd $MODSSL
if [ ! -f build.configure ]
then if ! ./configure --with-apache=../$APACHE $MODSSLOPT
     then echo could not configure modssl
          exit 1
     fi
     touch build.configure
fi
cd ..

##### Apache

echo configuring apache
cd $APACHE
if [ ! -f build.configure ]
then if ! ./configure \
 --prefix=$APACHEPREFIX \
 --enable-module=ssl \
 --enable-module=all \
 --enable-shared=max \
 $APACHEOPT
     then echo could not configure apache
          exit 1
     fi
     touch build.configure
fi

echo make apache
if [ ! -f build.make ]
then if ! make $MAKEFLAGS
     then echo could not make apache
          exit 1
     fi
     touch build.make
fi

echo make certificate apache
if [ ! -f build.cert ]
then if ! make certificate
     then echo could not make certificate
          exit 1
     fi
     touch build.cert
fi

echo make install apache
if [ ! -f build.install ]
then if ! make install
     then echo could not make install apache
          exit 1
     fi
     touch build.install
fi
cd ..

#### modperl

echo configuring modperl
cd $MODPERL
if [ ! -f build.configure ]
then if ! perl Makefile.PL EVERYTHING=1 USE_APXS=1 WITH_APXS=$APXS $MODPERLOPT
     then echo could not configure modperl
          exit 1
     fi
     touch build.configure
fi

echo make modperl
if [ ! -f build.make ]
then if ! make
     then echo could not make modperl
          exit 1
     fi
     touch build.make
fi

echo make install modperl
if [ ! -f build.install ]
then if ! make install
     then echo could not make install modperl
          exit 1
     fi
     touch build.install
fi
cd ..

### PHP
cd $PHP
if [ ! -f build.config ] ; then
    echo config PHP
    if ! ./configure --with-apxs=$APXS $PHPOPT
     then echo could not configure php
          exit 1
     fi
     touch build.config
fi

if [ ! -f build.make ] ; then
    echo make PHP
    if ! make $MAKEFLAGS
     then echo could not make php
          exit 1
     fi
     touch build.make
fi

if [ ! -f build.install ] ; then
    echo install PHP
    if ! make install
     then echo could not make install php
          exit 1
     fi
     touch build.install
fi
cd ..

### mod_gzip
cd $GZIP
if [ ! -f build.make ] ; then
    echo make mod_gzip

    TMP=/tmp/$$
cat > $TMP <<'EOF'
Index: mod_gzip-1.3.26.1a/Makefile
===================================================================
--- mod_gzip-1.3.26.1a.orig/Makefile	2002-10-01 00:29:49.000000000 -0700
+++ mod_gzip-1.3.26.1a/Makefile	2008-04-25 16:33:32.000000000 -0700
@@ -3,7 +3,7 @@
 RM=/bin/rm
 
 build:
-	$(APXS) -Wc,-Wall,-O3,-fomit-frame-pointer,-pipe -c mod_gzip.c mod_gzip_debug.c mod_gzip_compress.c -o mod_gzip.so
+	$(APXS) -c mod_gzip.c mod_gzip_debug.c mod_gzip_compress.c -o mod_gzip.so
 
 install:
 	$(APXS) -A -i mod_gzip.so
EOF

    patch -p1 < $TMP
    rm -f $TMP

    if ! make APXS=$APXS
     then echo could not make mod_gzip
          exit 1
     fi
     touch build.make
fi

if [ ! -f build.install ] ; then
    echo install mod_gzip
    if ! make install APXS=$APXS
     then echo could not make install mod_gzip
          exit 1
     fi
     touch build.install
fi
cd ..

#### embperl

echo configuring embperl
cd $EMBPERL
if [ ! -f build.config ]
then if ! perl Makefile.PL
     then echo could not configure embperl
          exit 1
     fi
     touch build.config
fi

echo make embperl
if [ ! -f build.make ]
then if ! make
     then echo could not make embperl
          exit 1
     fi
     touch build.make
fi

echo installing embperl
if [ ! -f build.install ]
then if ! make install
     then echo could not make install embperl
          exit 1
     fi
     touch build.install
fi
cd ..

}

# parse arguments

if [ "$1" = "clean" ]
then clean
     exit 0
elif [ "$1" = "distclean" ]
then distclean
     exit 0
elif [ "$1" = "download" ]
then download
     exit 0
elif [ "$1" = "extract" ]
then download
     extract
     exit 0
else
  download
  extract
  build
  echo finished
fi
