#!/bin/bash

VERSION_APACHE=1.3.26
VERSION_MOD_SSL=2.8.10
VERSION_MOD_PERL=1.27
VERSION_PHP=4.2.1

# clean
#####################################################################################################
clean()
{
    rm -rf apache_${VERSION_APACHE}
    rm -rf mod_ssl-${VERSION_MOD_SSL}-${VERSION_APACHE}
    rm -rf mod_perl-${VERSION_MOD_PERL}
    rm -rf php-${VERSION_PHP}
}

# unzip
#####################################################################################################
unzip_all()
{
    gunzip -c apache_${VERSION_APACHE}.tar.gz | tar xvf - > /dev/nul
    gunzip -c mod_ssl-${VERSION_MOD_SSL}-${VERSION_APACHE}.tar.gz | tar xvf - > /dev/nul
    gunzip -c mod_perl-${VERSION_MOD_PERL}.tar.gz | tar xvf - > /dev/nul
    bunzip2 -c php-${VERSION_PHP}.tar.bz2 | tar xvf - > /dev/nul
}

# mod_ssl
#####################################################################################################
config_mod_ssl()
{
    cd mod_ssl-${VERSION_MOD_SSL}-${VERSION_APACHE}

    SSL_BASE=SYSTEM \
    EAPI_MM=SYSTEM \
    ./configure \
	--with-apache=../apache_${VERSION_APACHE}

    cd ..
}

# mod_perl
#####################################################################################################
config_mod_perl()
{
    cd mod_perl-${VERSION_MOD_PERL}

    perl Makefile.PL \
	EVERYTHING=1 \
        APACHE_SRC=../apache_${VERSION_APACHE}/src \
        USE_APACI=1 \
        PREP_HTTPD=1 \
        DO_HTTPD=1

    cd ..
}

make_mod_perl()
{
    cd mod_perl-${VERSION_MOD_PERL}

    make
    make install
    
    cd ..
}

# PHP
#####################################################################################################
config_php()
{
    cd php-${VERSION_PHP}

#        --with-apxs=/usr/sbin/apxs \

    EXTRA_LIBS=-L/usr/local/freetds/lib \
    ./configure \
	i386-redhat-linux \
        --with-apache=../apache_${VERSION_APACHE} \
        --prefix=/usr \
        --exec-prefix=/usr \
        --bindir=/usr/bin \
        --sbindir=/usr/sbin \
        --sysconfdir=/etc \
        --datadir=/usr/share \
        --includedir=/usr/include \
        --libdir=/usr/lib \
        --libexecdir=/usr/libexec \
        --localstatedir=/var \
        --sharedstatedir=/usr/com \
        --mandir=/usr/share/man \
        --infodir=/usr/share/info \
        --prefix=/usr \
        --with-config-file-path=/etc \
        --disable-debug \
        --enable-pic \
        --disable-rpath \
        --enable-inline-optimization \
        --with-bz2 \
        --with-db3 \
        --with-exec-dir=/usr/bin \
        --with-gd \
        --with-gdbm \
        --with-gettext \
        --with-jpeg-dir=/usr \
        --with-mm \
        --with-openssl \
        --with-png \
        --with-regex=system \
        --with-ttf \
        --with-zlib \
        --with-layout=GNU \
        --enable-debugger \
        --enable-ftp \
        --enable-magic-quotes \
        --enable-safe-mode \
        --enable-sockets \
        --enable-sysvsem \
        --enable-sysvshm \
        --enable-track-vars \
        --enable-yp \
        --enable-wddx \
        --without-mysql \
        --without-unixODBC \
        --without-oracle \
        --without-oci8 \
        --with-pspell \
        --with-xml \
        --with-sybase=/usr/local/freetds

    cd ..
}

make_php()
{
    cd php-${VERSION_PHP}

    make
    make install

    cd ..
}

# Apache
#####################################################################################################
config_apache()
{
    cd apache_${VERSION_APACHE}

#    --prefix=/etc/httpd \
#    --exec-prefix=/usr \
#    --bindir=/usr/bin \
#    --sbindir=/usr/sbin \
#    --mandir=/usr/share/man \
#    --sysconfdir=/etc/httpd/conf \
#    --includedir=/usr/include/apache \
#    --libexecdir=/usr/lib/apache \
#    --datadir=/var/www \
#    --iconsdir=/var/www/icons \
#    --htdocsdir=/var/www/html \
#    --manualdir=/var/www/html/manual \
#    --cgidir=/var/www/cgi-bin \
#    --localstatedir=/var \
#    --runtimedir=/var/run \
#    --logfiledir=/var/log/httpd \
#    --proxycachedir=/var/cache/httpd \

    SSL_BASE=SYSTEM \
    EAPI_MM=SYSTEM \
    EXTRA_LIBS=-L/usr/local/freetds/lib \
    ./configure \
        --enable-module=ssl \
        --activate-module=src/modules/perl/libperl.a \
        --enable-module=perl \
        --activate-module=src/modules/php4/libphp4.a \
	--enable-module=php4 \
        --enable-module=auth_dbm \
        --enable-module=all \
        --enable-shared=max \
        --disable-shared=perl \
	--disable-shared=ssl \
	--disable-shared=php4 \
        --enable-rule=EAPI \
        --disable-rule=WANTHSREGEX \
        --with-perl=/usr/bin/perl \
        --enable-suexec \
        --suexec-docroot=/var/www \
        --suexec-caller=apache \
	--prefix=/etc/httpd \
	--exec-prefix=/usr \
        --bindir=/usr/bin \
        --sbindir=/usr/sbin \
        --mandir=/usr/share/man \
        --sysconfdir=/etc/httpd/conf \
        --includedir=/usr/include/apache \
        --libexecdir=/usr/lib/apache \
        --datadir=/var/www \
        --iconsdir=/var/www/icons \
        --htdocsdir=/var/www/html \
        --manualdir=/var/www/html/manual \
        --cgidir=/var/www/cgi-bin \
        --localstatedir=/var \
        --runtimedir=/var/run \
        --logfiledir=/var/log/httpd \
        --proxycachedir=/var/cache/httpd \

    cd ..
}

make_apache()
{
    cd apache_${VERSION_APACHE}

    EXTRA_LIBS=-L/usr/local/freetds/lib \
    make
    
    cd ..    
}


# support
#####################################################################################################

do_make()
{
    config_mod_ssl
    config_mod_perl
    make_mod_perl
    config_php
    make_php
    config_apache
    make_apache
}

# main
#####################################################################################################
case "$1" in
    clean)
	clean
	;;
    unzip)
	unzip_all
	;;
    make)
        do_make
	;;
    *)
	echo $"Usage: do { clean | unzip | make }"
	exit 1
esac
