#!/bin/sh
# Installation von apache-ssl-php-mysql-ldap
# Achtung mit ldap: --with-ldap beim php
# - make gso bei Solaris statt slx

# if you want to repeat a step of the procedure,
# you need to remove the corresponding file in
# the status directory and to reinvoke install.sh!


#####################################################################
######                        CONFIG                           ######
#####################################################################

# commands
export PATH=/usr/local/bin:$PATH
UNAME=`uname`
if [ "$UNAME" = Linux ]; then
    TAR=tar
    TAR_V=v
    MAKE=make
    MAKEFLAGS="-s -j2"
    PATCH=patch
else
    TAR=gtar
    #TAR_V=v
    MAKE=gmake
    MAKEFLAGS="-s -j2"
    PATCH=gpatch
fi

# PATH's
WWW=/home/httpd			# httpds default server root
SRC=/usr/local/install/apache			# where the sources resides
STAT=$SRC/status		# location of status files
JAVA=/usr/local/java	# java home dir


# path to certain sources
# if a path here is commented out, it will not 
# compiled into apache!
SRC_PHP=$SRC/php-3.0.14
SRC_AP=$SRC/apache_1.3.12
SRC_AP_MYSQL=$SRC/mod_auth_mysql-2.20
SRC_AP_SSL=$SRC/mod_ssl-2.6.2-1.3.12
SRC_AP_PERL=$SRC/mod_perl-1.22
SRC_AP_ROAM=$SRC/mod_roaming-1.0.1
#SRC_AP_LDAP=$SRC/mod_auth_ldap
SRC_LDAP=$SRC/openldap-1.2.9
SRC_IMAP=$SRC/imap-4.7b
SRC_SSL=$SRC/openssl-0.9.5a
SRC_GD=$SRC/gd-1.8.1
#SRC_FP_P=$SRC/mod_frontpage
#SRC_JSDK=$SRC/JSDK2.0
#SRC_JSRV=$SRC/ApacheJServ-1.1


# destinations, be carefull!
DST_AP=/usr/local/apache
DST_LDAP=/usr/local/openldap
DST_IMAP=/usr/local/imap
DST_SSL=/usr/local/openssl
DST_GD=/usr/local/gd
DST_FP=/usr/local/frontpage
DST_JSDK=/usr/local/JSDK2.0


# source tarballs
APACHE=apache_1.3.12.tar.gz
OPENLDAP=openldap-1.2.9.tgz
PHP=php-3.0.14.tar.gz
GD=gd-1.8.1.tar.gz
IMAP=imap-4.7b.tar.Z
MOD_AUTH_LDAP=mod_auth_ldap-0.5.tar.gz
MOD_ROAMING=mod_roaming-1.0.1.tar.gz
MOD_SSL=mod_ssl-2.6.2-1.3.12.tar.gz
MOD_PERL=mod_perl-1.22.tar.gz
MOD_AUTH_MYSQL=mod_auth_mysql-2.20.tar.gz
OPENSSL=openssl-0.9.5a.tar.gz
#APACHE_JSERV=ApacheJServ-1.1.tar.gz
#JSDK=jsdk20-solaris2-sparc.tar.Z
#MOD_FRONTPAGE=fp40.Linux.tar.Z
#FP_PATCH=fp-patch-apache_1.3.12-4.0

# end of user editable section
#######################################################################



die ()
{
    [ $# -gt 0 ] && echo "$@" >&2
    exit 1
}

msg ()
{
	echo "<=========================================================>"
	echo "     $1"
	echo "<=========================================================>"
}

# vorab alle evtl. vorhandene Quell-Verzeichnisse loeschen
cd $SRC
if [ ! -f $STAT/rmdirs ] ; then
	msg "removing sourcedirs"
	rm -fr $SRC_AP
	rm -fr $SRC_AP_SSL
	rm -rf $SRC_AP_MYSQL
	rm -rf $SRC_AP_PERL
	rm -fr $SRC_PHP
	rm -fr $SRC_GD
	rm -fr $SRC_IMAP
	rm -fr $SRC_AP_LDAP
	rm -fr $SRC_SSL
	rm -fr $SRC_LDAP
	rm -fr $SRC_AP_ROAM
	touch $STAT/rmdirs
fi

if [ ! -d $WWW ] ; then
	msg "mkdir -p $WWW"
	mkdir -p $WWW
fi

# alle gewuenschten Quellen auspacken
if [ ! -f $STAT/tar ] ; then
	msg "untaring source tarballs"
	$TAR x${TAR_V}zf $APACHE
	[ -n "$SRC_LDAP" ] && $TAR x${TAR_V}zf $OPENLDAP
	[ -n "$SRC_PHP" ] && $TAR x${TAR_V}zf $PHP
	[ -n "$SRC_GD" ] && $TAR x${TAR_V}zf $GD
	[ -n "$SRC_IMAP" ] && $TAR x${TAR_V}zf $IMAP
	[ -n "$SRC_AP_LDAP" ] && $TAR x${TAR_V}zf $MOD_AUTH_LDAP
	[ -n "$SRC_AP_ROAM" ] && $TAR x${TAR_V}zf $MOD_ROAMING
	[ -n "$SRC_AP_SSL" ] && $TAR x${TAR_V}zf $MOD_SSL
	[ -n "$SRC_AP_PERL" ] && $TAR x${TAR_V}zf $MOD_PERL
	[ -n "$SRC_AP_MYSQL" ] && $TAR x${TAR_V}zf $MOD_AUTH_MYSQL
	[ -n "$SRC_SSL" ] && $TAR x${TAR_V}zf $OPENSSL
	[ -n "$SRC_JSRV" ] && $TAR x${TAR_V}zf $APACHE_JSERV
	[ -n "$SRC_JSDK" ] && $TAR x${TAR_V}zf $JSDK
	[ -n "$SRC_FP_P" ] && $TAR x${TAR_V}zf $MOD_FRONTPAGE
	touch $STAT/tar
fi

if [ -n "$SRC_JSDK"  -a -d  "$SRC_JSDK" ]; then
    if [ ! -f $STAT/jsdk ]; then
	  msg "cp -r $SRC_JSDK $DST_JSDK"
	  cp -r $SRC_JSDK $DST_JSDK
	  touch $STAT/jsdk
    fi
fi		

if [ -n "$SRC_LDAP" -a -d "$SRC_LDAP" ]; then
        if [ ! -f $STAT/ldap ] ; then
			msg "build openldap"
    	  	cd $SRC_LDAP
    	  	./configure --prefix=$DST_LDAP --without-threads --disable-slurpd --without-subdir || die "configure $SRC_LDAP failed"
    		$MAKE $MAKEFLAGS depend || die "make depend in $SRC_LDAP failed"
    		$MAKE $MAKEFLAGS || die "make in $SRC_LDAP failed"
    		$MAKE $MAKEFLAGS install || die "make install in $SRC_LDAP failed"
        	touch $STAT/ldap
        fi
fi

if [ -d "$SRC_SSL" ]; then
        if [ ! -f $STAT/ssl ] ; then
			msg "build openssl"
    		cd $SRC_SSL
    		./config --prefix=$DST_SSL --openssldir=$DST_SSL || die "config $SRC_SSL failed"
    		$MAKE $MAKEFLAGS || die "make in $SRC_SSL failed"
    		$MAKE $MAKEFLAGS test || die "make test in $SRC_SSL failed"
    		$MAKE $MAKEFLAGS install || die "make install in $SRC_SSL failed"
        	touch $STAT/ssl
        fi
fi

if [ -n "$SRC_IMAP" -a -d "$SRC_IMAP" ]; then
        if [ ! -f $STAT/imap ] ; then 
			msg "build UW imap libs"
    		cd $SRC_IMAP
    		rm -fr $DST_IMAP
    		# Solaris: $MAKE $MAKEFLAGS gso
    		if [ "$UNAME" = SunOS ]; then
    	    	$MAKE $MAKEFLAGS gso || die "make gso in $SRC_IMAP failed"
    		elif [ "$UNAME" = Linux ]; then
    		    $MAKE $MAKEFLAGS slx || die "make slx in $SRC_IMAP failed"
    		else
    		    die "make in $SRC_IMAP failed"
    		fi
    		cd c-client
    		mkdir -p $DST_IMAP/lib
    		mkdir $DST_IMAP/include
    		mkdir $DST_IMAP/bin
    		cp *.h $DST_IMAP/include
    		cp c-client.a $DST_IMAP/lib
    		cd ..
    		cp imapd/imapd ipopd/ipop[23]d $DST_IMAP/bin
        	touch $STAT/imap
       fi
fi

if [ -d "$SRC_GD" ]; then
  if [ ! -f $STAT/gd ] ; then
	msg "build GD lib"
    cd $SRC_GD
    $MAKE $MAKEFLAGS || die "make in $SRC_GD failed"
    rm -fr $DST_GD
    mkdir -p $DST_GD/lib
    mkdir $DST_GD/include
    mkdir $DST_GD/bin
    cp bdftogd gddemo pngtogd webpng $DST_GD/bin
    cp *.h $DST_GD/include
    cp libgd.a $DST_GD/lib
	touch $STAT/gd
  fi
fi

if [ -d "$SRC_AP" ]; then
    # angepasste Version von fp40
    if [ -n "$SRC_FP_P" ]; then
        if [ ! -f $STAT/fp ] ; then
			msg "patching apache sources with $SRC_FP_P"
			cd $SRC_AP
            $PATCH -p0 < $SRC_FP_P/$FP_PATCH
            touch $STAT/fp
        fi
fi

cd $SRC_AP
#######################################################################
######                    config apache                         #######
#######################################################################

	msg "build modules"

    # Modul mod_ssl
    if [ -n "$SRC_AP_SSL" -a -d "$SRC_AP_SSL" ]; then
	  if [ ! -f $STAT/modssl ] ; then
		msg "build mod_ssl"
        cd $SRC_AP_SSL
        ./configure --with-apache=$SRC_AP || die "configure in $SRC_AP_SSL failed"
        touch $STAT/modssl
      fi
	fi

    # Modul mod_perl
    if [ -n "$SRC_AP_PERL" -a -d "$SRC_AP_PERL" ]; then
	  if [ ! -f $STAT/modperl ] ; then
		msg "build mod_perl"
        cd $SRC_AP_PERL
        perl Makefile.PL APACHE_SRC=$SRC_AP/src \
            DO_HTTPD=1 \
            USE_APACI=1 \
            PREP_HTTPD=1 \
	    PERL_DEBUG=1 \
            EVERYTHING=1 || die "#!/usr/bin/perl Makefile.PL in $SRC_AP_PERL failed"
        $MAKE $MAKEFLAGS || die "make in $SRC_AP_PERL failed"
        $MAKE $MAKEFLAGS install || die "make install in $SRC_AP_PERL failed"
        touch $STAT/modperl
      fi
	fi

    # PHP
    if [ -n "$SRC_PHP" -a -d "$SRC_PHP" ]; then
	  if [ ! -f $STAT/php ] ; then
		msg "build mod_php"
        cd $SRC_PHP
        ARGS="--with-apache=$SRC_AP"
        ARGS="$ARGS --enable-debug --enable-debugger --with-mysql --with-xml"
        ARGS="$ARGS --enable-safe-mode --enable-sysvsem --enable-sysvshm"
        ARGS="$ARGS --enable-bcmath --with-zlib=yes"
        ARGS="$ARGS --with-config-file-path=$WWW/etc"
        [ -n "$SRC_LDAP" ] && ARGS="$ARGS --with-ldap=$DST_LDAP"
        [ -n "$SRC_IMAP" ] && ARGS="$ARGS --with-imap=$DST_IMAP"
        [ -n "$SRC_GD" ] && ARGS="$ARGS --with-gd=$DST_GD"
        ./configure $ARGS || die "configure in $SRC_PHP failed"
        $MAKE $MAKEFLAGS || die "make in $SRC_PHP failed"
        $MAKE $MAKEFLAGS install || die "make install in $SRC_PHP failed"
        touch $STAT/php
      fi
	fi

    # Modul mod_auth_mysql
    if [ -n "$SRC_AP_MYSQL" -a -d "$SRC_AP_MYSQL" ]; then
		if [ ! -f $STAT/modauthmysql ] ; then
		msg "build mod_auth_mysql"
        cd $SRC_AP_MYSQL
        ARGS="--with-apache=$SRC_AP --with-shared-apache=$SRC_AP --with-mysql=/usr"
        ./configure $ARGS || die "configure in $SRC_AP_MYSQL failed"
        $MAKE $MAKEFLAGS || die "make in $SRC_AP_MYSQL failed"
		touch $STAT/modauthmysql
		fi
    fi

	if [ ! -f $STAT/confapache ] ; then
    	cd $SRC_AP
		msg "configure apache"
   	 	export SSL_BASE=$DST_SSL 
    	[ -n "$SRC_LDAP" ] && cp $DST_LDAP/include/*.h src/include
    	ARGS="--prefix=$DST_AP --sysconfdir=$WWW/etc --logfiledir=/var/log/www"
    	ARGS="$ARGS --datadir=$WWW --localstatedir=$WWW --includedir=$DST_AP/include"
    	#ARGS="$ARGS --mandir=/usr/local/man --bindir=$DST_AP/bin --sbindir=$DST_AP/bin"
    	ARGS="$ARGS --enable-module=expires --enable-module=all"
    	ARGS="$ARGS --enable-shared=max --enable-module=so"
    	[ -n "$SRC_AP_LDAP" ] && ARGS="$ARGS --add-module=$SRC_AP_LDAP/mod_auth_ldap.c"
    	[ -n "$SRC_PHP" ] && ARGS="$ARGS --activate-module=src/modules/php3/libphp3.a"
    	[ -n "$SRC_SSL" ] && ARGS="$ARGS --enable-module=ssl"
    	[ -n "$SRC_AP_PERL" ] && ARGS="$ARGS --activate-module=src/modules/perl/libperl.a"
    	[ -n "$SRC_AP_ROAM" ] && ARGS="$ARGS --add-module=$SRC_AP_ROAM/mod_roaming.c"
    	[ -n "$SRC_FP_P" ] && ARGS="$ARGS --activate-module=src/modules/frontpage/mod_frontpage.c"
		[ -n "$SRC_FP_P" ] && ARGS="$ARGS --fpexec-caller=wwwrun --fpexec-uidmin=20 --fpexec-gidmin=2 --enable-shared=frontpage"
    	[ -n "$SRC_AP_MYSQL" ] && ARGS="$ARGS --activate-module=src/modules/auth_mysql/libauth_mysql.a"
		[ -n "$SRC_JSRV" ] && ARGS="$ARGS --activate-module=src/modules/jserv/libjserv.a"
		echo "./configure $ARGS"
    	./configure $ARGS || die "configure in $SRC_AP failed"
		touch $STAT/confapache
	fi

	  # JSRV requires to be configured right *after* apache :-(
      if [ -n "$SRC_JSRV" -a -d "$SRC_JSRV" ]; then
        if [ ! -f $STAT/jsrv ] ; then
          msg "build mod_jserv"
          cd $SRC_JSRV
	  CLASSPATH=.:$DST_JSDK/lib/jsdk.jar:$JAVA/lib/classes.zip:$DST_JSDK/src/javax/servlet
	  export CLASSPATH
echo "===> CLASSPATH: $CLASSPATH <===="
          ARGS="--prefix=/usr/local/jserv --enable-apache-conf --enable-EAPI --disable-debugging"
          ARGS="$ARGS --with-jdk-home=$JAVA --with-JSDK=$DST_JSDK/lib/jsdk.jar"
          ARGS="$ARGS --with-apache-src=$SRC_AP"
          ./configure $ARGS || die "configure in $SRC_JSRV failed"
          echo "$ARGS"
          $MAKE $MAKEFLAGS || die "make in $SRC_JSRV failed"
          $MAKE $MAKEFLAGS install || die "make install in $SRC_JSRV failed"
          touch $STAT/jsrv
        fi
    fi


    msg "build apache"
	echo "$ARGS"
	
    $MAKE $MAKEFLAGS || die "make in $SRC_AP failed"

    # make certificate
    $MAKE $MAKEFLAGS install || die "make install in $SRC_AP failed"


    cd $WWW
    [ ! -e conf ] && ln -s etc conf
    #chown -R www:internet .

    if [ ! -d "$DST_FP" -a -r $SRC/fp40.$UNAME.tar.Z ]; then
		msg "installing MicroSchrott frontpage Server Extensions (FP 2000)"
        cp -r $SRC/frontpage $DST_FP
		cd $DST_FP/version4.0
        ./fp_install.sh
    fi
fi


msg "done. really?!"
