#!/bin/sh

OS=`uname`

#####################################################
# User Variables

APACHE_VER=1.3.29
MODSSL_VER=2.8.16
PHP_VER=4.3.4
GD_VER=2.0.15
CURL_VER=7.10.5
ZLIB_VER=1.1.4
PNG_VER=1.2.5
FRONTPAGE_VER=1.6.1
MCRYPT_VER=2.5.7
MODPERL_FILE=mod_perl-1.0-current.tar.gz
MODPERL_DIR=mod_perl-1.27

PHP_CONFIGURE=configure.php
PHP_INI=/usr/local/lib/php.ini
APACHE_SSL_CONFIGURE=configure.apache_ssl

HTTPDCONF=/etc/httpd/conf
HTTPD_CONF=${HTTPDCONF}/httpd.conf
WEBPATH=http://files.directadmin.com/services/customapache
WEBPATH_BACKUP=http://www.directadmin.com/services/customapache

if [ $OS = "FreeBSD" ]; then
	FRONTPAGE_EXT=fp50.freebsd.tar.gz
else
	FRONTPAGE_EXT=fp50.linux.tar.gz
fi
MOD_FRONTPAGE=mod_frontpage.c
FP_PATCH0=Makefile.tmpl.patch
FP_PATCH1=frontpage.patch
FRONTPAGE_PATH=/usr/local

if [ $OS = "FreeBSD" ]; then
	ZENDNAME=ZendOptimizer-2.1.0b-FreeBSD4.0-i386
else
	ZENDNAME=ZendOptimizer-2.1.0a-Linux_glibc21-i386
fi
ZENDFILE=${ZENDNAME}.tar.gz

JPEGFILE=jpegsrc.v6b.tar.gz
JPEGDIR=jpeg-6b

WEBALIZER=webalizer-2.01-10
WEBALIZER_FILE=webalizer-2.01-10-src.tgz

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

SERVICES=/usr/local/directadmin/data/admin/services.status

CWD=`pwd`
FORCE=0

showHelp() {
	echo "*************************************";
	echo "*                                   *";
	echo "* DirectAdmin WebServices Installer *";
	echo "*                                   *";
	echo "*************************************";
	echo "";
	echo "  To build everything run:";
	echo "     $0 all";
	echo "";
	echo "  Other options:";
	echo "     $0 php";
	echo "     $0 apache_mod_ssl";
	echo "     $0 gd";
	echo "     $0 libjpeg";
	echo "     $0 libpng";
	echo "     $0 zlib";
	echo "     $0 curl";
	echo "     $0 mcrypt";
	echo "     $0 mod_perl";
	echo "     $0 mod_frontpage";
	echo "     $0 frontpage_ext";
	echo "     $0 webalizer";
	echo "     $0 zend (not included with 'all')";
	echo "";
	echo "  Remove old build data:";
	echo "     $0 clean";
	echo "";
	echo "";
	echo "  Get lastest build script and data";
	echo "     $0 update";
	echo "";
	echo "  Get data for current build script";
	echo "     $0 update_data";

}

checkFile() {
	if [ ! -e $1 ]
	then
		echo "*** Cannot find $1. Aborting ***";
		exit 1;		
	else
		echo "Found $1";
	fi
}


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

preCheck() {
	checkFile /usr/bin/patch
	checkFile /usr/bin/gcc
}


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

checkCURL() {
	cd ${CWD};
	NUM=`cat ${PHP_CONFIGURE} | grep -c '\-\-with\-curl'`
	if [ $NUM = 0 ]
	then
		return;
	fi

	if [ -e /usr/local/lib/libcurl.so ]
	then
		echo -n "cURL is already installed. Do you want to build it again? (y/n) :";
		read yesno;
		echo "";
		if [ "$yesno" = "n" ]
		then
			return;
		fi	
	fi
	
	doCURL;	
}

checkLibJpeg() {
	cd $CWD;
        NUM=`cat ${PHP_CONFIGURE} | grep -c '\-\-with\-jpeg\-dir'`
        if [ $NUM = 0 ]
        then
                return;
        fi

	if [ -e /usr/local/lib/libjpeg.a ]
	then
		if [ $FORCE = 1 ]
		then
			return;
		fi

		echo -n "LibJPEG is already installed. Do you want to build it again? (y/n) :";
		read yesno;
		echo "";
		if [ "$yesno" = "n" ]
		then
			return;
		fi
	fi

	doLibJpeg;
}

checkLibz() {
	#if this is called it has to be included :)

	if [ -e /usr/local/lib/libz.a ]
	then
		if [ $FORCE = 1 ]
		then
			return;
		fi
	
                echo -n "LibZ is already installed. Do you want to build it again? (y/n) :";
                read yesno;
                echo "";
                if [ "$yesno" = "n" ]
                then
                        return;
                fi
        fi

	doZlib;
}

checkLibPng() {

        cd $CWD;
        NUM=`cat ${PHP_CONFIGURE} | grep -c '\-\-with\-png\-dir'`
        if [ $NUM = 0 ]
        then
                return;
        fi

	checkLibz;

        if [ -e /usr/local/lib/libpng.a ]
        then
		if [ $FORCE = 1 ]
		then
			return;
		fi

                echo -n "LibPng is already installed. Do you want to build it again? (y/n) :";
                read yesno;
                echo "";
                if [ "$yesno" = "n" ]
                then
                        return;
                fi
        fi

        doLibPng;
}

checkGD() {
        cd ${CWD};
        NUM=`cat ${PHP_CONFIGURE} | grep -c '\-\-with\-gd'`
        if [ $NUM = 0 ]
        then
        	return;
        fi

	checkLibJpeg;
	checkLibPng;        
        
	if [ -e /usr/local/lib/libgd.a ]
	then
		echo -n "GD is already installed. Do you want to build it again? (y/n) :";
		read yesno;
		echo "";
		if [ "$yesno" = "n" ]
		then
			return;
		fi	
	fi
        
        doGD;        
}

checkMCrypt() {
        cd ${CWD};
        NUM=`cat ${PHP_CONFIGURE} | grep -c '\-\-with\-mcrypt'`
        if [ $NUM = 0 ]
        then
        	return;
        fi
        
	if [ -e /usr/local/lib/libmcrypt.so ]
	then
		echo -n "mCrypt is already installed. Do you want to build it again? (y/n) :";
		read yesno;
		echo "";
		if [ "$yesno" = "n" ]
		then
			return;
		fi	
	fi
        
        doMCrypt;
}

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

doPhp() {
	checkCURL
	checkGD
	checkMCrypt
	cd $CWD;
	FILE=${CWD}/php-${PHP_VER}.tar.gz
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";
	cd php-${PHP_VER}

	#make sure we have the sendmail link
	ln -sf exim /usr/sbin/sendmail

	#some reports of missing -lltdl, problem found to be simple missing link
	if [ ! -e /usr/lib/libltdl.so ]; then
		ln -s libltdl.so.3 /usr/lib/libltdl.so
	fi
	
	echo "Configuring php-${PHP_VER}...";

	${CWD}/${PHP_CONFIGURE};
	if [ $? -ne 0 ]
	then
		echo -e "\n*** There was an error while trying to configure php. Check the configure.php file\n";
		exit 1;
	fi

	echo "Done Configuration.";

	while
	echo "Trying to make php..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -n -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make Complete";

	while
	echo "Installing php...";
	do
	{
		make install

		if [ $? -ne 0 ]
		then
                        echo -e "\n*** The install has failed, do you want to try to install it again? (y,n): ";
                        read yesno;
                        echo "";
                        if [ "$yesno" = "n" ]
                        then
                                exit 0;
                        fi
                else
                        break;
                fi
	}
	done;
	
	echo "Copying php.ini..";
	if [ ! -e $PHP_INI ]
	then
		cp ./php.ini-dist $PHP_INI;
	else
		echo "$PHP_INI already exists, skipping.";
	fi

	echo "Enabling register_globals...";
	/usr/bin/perl -pi -e 's/^register_globals = Off/register_globals = On/' /usr/local/lib/php.ini

	echo "PHP Installed.";

	cd $CWD;
}

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

doModFrontpage() {
	cd $CWD;
	FILE=${CWD}/mod_frontpage-${FRONTPAGE_VER}.tar.gz
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";

	cd mod_frontpage-${FRONTPAGE_VER}
	echo "Configuring mod_frontpage-${FRONTPAGE_VER}...";
	/usr/bin/perl -pi -e 's/$thechoice="\/usr\/local\/sbin\/httpd";/$thechoice="\/usr\/sbin\/httpd";\n\$user="apache";\n/' Makefile.PL;
	/usr/bin/perl Makefile.PL;	
		
	make clean

	echo "Done. Making mod_frontpage-${FRONTPAGE_VER}...";
	while
	echo "Trying to make mod_frontpage..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";

	echo "Installing mod_frontpage-${FRONTPAGE_VER}...";
	make install
	echo "Done mod_frontpage.";

	cd $CWD
}

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

doFrontpageExt() {

	cd $CWD;
	FILE=${CWD}/${FRONTPAGE_EXT};
	checkFile $FILE;	
	echo "Extracting Frontpage Extensions...";
	tar xzf $FILE -C ${FRONTPAGE_PATH}
	echo "Setting the suidkey...";
	FPDIR=${FRONTPAGE_PATH}/frontpage
	FPFILE=${FPDIR}/version5.0/apache-fp/suidkey
	ps ea | tail -10 | sed -e 's/ //g' | cut -c10-137 > ${FPFILE}
	chown root ${FPFILE}
	chmod 600 ${FPFILE}
	${FPDIR}/version5.0/set_default_perms.sh

	echo "Creating we80.cnf...";	
	FPFILE=${FPDIR}/we80.cnf

	if [ -e $FPDIR ]
	then
		if [ ! -e $FPFILE ]
		then
			echo "vti_encoding:SR|utf8-nl" > $FPFILE;
			echo "frontpageroot:/usr/local/frontpage/version5.0" >> $FPFILE;
			echo "authoring:enabled" >> $FPFILE;
			echo "servertype:apache-fp" >> $FPFILE;
			echo "serverconfig:${HTTPD_CONF}" >> $FPFILE;
			echo "SMTPHost:127.0.0.1" >> $FPFILE;
			echo "SendmailCommand:/usr/sbin/sendmail" >> $FPFILE;
			echo "MailSender:webmaster@" >> $FPFILE;
		fi
	fi
	echo "Done.  Frontpage Extension install complete.";

}

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

doModSSL() {
	
	cd $CWD;
	FILE=${CWD}/mod_ssl-${MODSSL_VER}-${APACHE_VER}.tar.gz
	checkFile $FILE;
	echo "Extracting $FILE...";
	
	tar xzf $FILE
	cd mod_ssl-${MODSSL_VER}-${APACHE_VER}
	
	echo "Configuring mod_ssl-${MODSSL_VER}-${APACHE_VER}...";

	${CWD}/${APACHE_SSL_CONFIGURE};
	if [ $? -ne 0 ]
	then
		echo -e "\n*** There was an error while trying to configure apache+mod_ssl. Check the ${APACHE_SSL_CONFIGURE} file\n";
		exit 1;
	fi

	echo "Done Configuration.";

	cd $CWD;
}

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

backupHttp() {
	echo "Backing up certificate and key, and turning off httpd for DirectAdmins's check.";

	if [ -e ${HTTPDCONF}/ssl.crt/server.crt ]
	then
		cp -f ${HTTPDCONF}/ssl.crt/server.crt ${HTTPDCONF}/ssl.crt/server.crt.backup
	fi
	if [ -e ${HTTPDCONF}/ssl.key/server.key ]
	then
		cp -f ${HTTPDCONF}/ssl.key/server.key ${HTTPDCONF}/ssl.key/server.key.backup
	fi
	if [ -e ${HTTPD_CONF} ]
	then
		cp -f ${HTTPD_CONF} ${HTTPD_CONF}.backup
	fi
	
	#turn off httpd service checking
	if [ -e $SERVICES ]
	then
		/usr/bin/perl -pi -e 's/httpd=ON/httpd=OFF/' ${SERVICES};
	fi
	
}

restoreHttp() {
	echo "Restoring certificate and key, and turning on httpd for DirectAdmins's check.";

	if [ -e ${HTTPDCONF}/ssl.crt/server.crt.backup ]
	then
		cp -f ${HTTPDCONF}/ssl.crt/server.crt.backup ${HTTPDCONF}/ssl.crt/server.crt
	fi
	if [ -e ${HTTPDCONF}/ssl.key/server.key.backup ]
	then
		cp -f ${HTTPDCONF}/ssl.key/server.key.backup ${HTTPDCONF}/ssl.key/server.key
	fi
	if [ -e ${HTTPDCONF}/httpd.conf.backup ]
	then
		cp -f ${HTTPDCONF}/httpd.conf.backup ${HTTPDCONF}/httpd.conf
	fi

	#turn on httpd service checking
	if [ -e $SERVICES ]
	then
		/usr/bin/perl -pi -e 's/httpd=OFF/httpd=ON/' ${SERVICES};
	fi
}

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

checkRPMS() {
	if [ $OS = "FreeBSD" ]; then
		return;
	fi

	echo "Removing all apache related rpms...";
	rpm -e --nodeps mod_auth_pgsql 2> /dev/null
	rpm -e --nodeps mod_python 2> /dev/null
	rpm -e --nodeps mod_auth_mysql 2> /dev/null
	rpm -e --nodeps mod_auth_any 2> /dev/null
	rpm -e --nodeps mod_dav 2> /dev/null
	rpm -e --nodeps mod_ssl 2> /dev/null
	rpm -e --nodeps mod_perl 2> /dev/null
	rpm -e --nodeps mod_fpse 2> /dev/null
	rpm -e --nodeps apache-fp 2> /dev/null
	rpm -e --nodeps apache-fp-devel 2> /dev/null
	rpm -e --nodeps apache-manual 2> /dev/null
	rpm -e --nodeps apacheconf 2> /dev/null
	rpm -e --nodeps apache-devel 2> /dev/null
	rpm -e --nodeps apache 2> /dev/null
	rpm -e --nodeps httpd 2> /dev/null
	rpm -e --nodeps httpd-devel 2> /dev/null
	rpm -e --nodeps php 2> /dev/null
	echo "All apache related rpms have been removed.";
}

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

addUserGroup() {

	if [ $OS = "FreeBSD" ]; then
		PW=/usr/sbin/pw
		if ! /usr/bin/grep -q "^${2}:" < /etc/group; then
			$PW groupadd ${2}
		fi
		if ! /usr/bin/id ${1} > /dev/null; then
			$PW useradd -g ${2} -n ${1}
		fi
	else
		if ! /usr/bin/id ${1} > /dev/null; then				
			/usr/sbin/useradd ${1} -r
		fi
	fi
}

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

doApache() {

	addUserGroup apache apache	

	backupHttp;
	
	cd $CWD;
	FILE=${CWD}/apache_${APACHE_VER}.tar.gz
	checkFile $FILE;
	
	#removing old apache dir, because the patch may get applied twice (might confuse the user)
	rm -rf ${CWD}/apache_${APACHE_VER};
	
	echo "Extracting $FILE...";
	
	tar xzf $FILE
	cd apache_${APACHE_VER}
	
	echo "Done. Applying patches...";
	
	cp ../$MOD_FRONTPAGE .
	patch -p0 < ../$FP_PATCH0
	#patch -p0 < ../fp-patch-apache_1.3.22
	#patch -p0 < ../fp-patch-suexec
	
	echo "Increasing hard limit to 32768...";
	/usr/bin/perl -pi -e 's/^#define HARD_SERVER_LIMIT 256/#define HARD_SERVER_LIMIT 32768/' ./src/include/httpd.h;
	/usr/bin/perl -pi -e 's/\/\* Majority of os/#undef FD_SETSIZE\n#define FD_SETSIZE 32768\n\n\/\* Majority of os/' ./src/include/ap_config.h
	/usr/bin/perl -pi -e 's/__FD_SETSIZE\s1024/__FD_SETSIZE\t32768/' /usr/include/bits/types.h

	if [ $OS = "FreeBSD" ]; then
		# do nothing
		echo "";
	else
		echo "131072" > /proc/sys/fs/file-max
		
	fi

	if [ "$1" = "fake" ]; then
                ./configure \
                        --prefix=/etc/httpd \
                        --exec-prefix=/etc/httpd \
                        --bindir=/usr/bin \
                        --sbindir=/usr/sbin \
                        --sysconfdir=/etc/httpd/conf \
			--enable-module=all \
			--enable-shared=max \
                        --includedir=/usr/include/apache \
			--libexecdir=/usr/lib/apache \
                        --datadir=/var/www \
                        --localstatedir=/var \
                        --runtimedir=/var/run \
                        --logfiledir=/var/log/httpd \
                        --proxycachedir=/var/cache/httpd \
			--disable-module=auth_db --disable-module=auth_dbm
	else

		echo "Setting up mod_ssl...";
	
		#configure apache through mod_ssl
		doModSSL;
	
		#cd ${CWD}/apache_${APACHE_VER}/src/modules;
		#ln -sf ../../../mod_frontpage-${FRONTPAGE_VER} frontpage
	fi

	cd ${CWD}/apache_${APACHE_VER};
	
	patch -p0 < ../$FP_PATCH1

	while
	echo "Trying to make apache..."
	do
	{
		C_INCLUDE_PATH=/usr/kerberos/include make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";
	
	checkRPMS;
	
	echo "Installing Apache...";
	make install
	
	make certificate TYPE=dummy;
	cp -f ./conf/ssl.crt/server.crt ${HTTPDCONF}/ssl.crt/server.crt
	cp -f ./conf/ssl.key/server.key ${HTTPDCONF}/ssl.key/server.key

	echo "Done installing Apache+Mod_SSL+Frontpage.";
		
	cd $CWD

	restoreHttp;
	
	#remove the auth modules from the httpd.conf
	/usr/bin/perl -pi -e 's/^LoadModule db_auth_module/#LoadModule db_auth_module/' ${HTTPD_CONF};
	/usr/bin/perl -pi -e 's/^AddModule mod_auth_db.c/#AddModule mod_auth_db.c/' ${HTTPD_CONF};
	
	ln -sf ../../var/log/httpd /etc/httpd/logs
	ln -sf ../../usr/lib/apache /etc/httpd/modules 
	chmod 755 /var/log/httpd

	if [ $OS = "FreeBSD" ]
	then
		cp -f ${CWD}/httpd_freebsd /usr/local/etc/rc.d/httpd
		chmod 755 /usr/local/etc/rc.d/httpd
	else
		cp -f ${CWD}/httpd /etc/rc.d/init.d/httpd
		chmod 755 /etc/rc.d/init.d/httpd
		/sbin/chkconfig httpd on
	fi

	if [ ! -e /etc/mime.types ]
	then
		cp ${CWD}/mime.types /etc/mime.types
	fi
	
	mkdir -p /var/www/html

	if [ ! -e /var/www/html/index.html ]
	then
        	if [ -e /var/www/html/index.html.en ]
	        then
        	        cp -f /var/www/html/index.html.en /var/www/html/index.html
	        else
        	        echo "<html>Apache is functioning normally</html>" > /var/www/html/index.html
	        fi
	fi
	#echo "<?phpinfo();?>" > /var/www/html/info.php

        #log rotate
        if [ ! -e /etc/logrotate.d/apache ] && [ $OS != "FreeBSD" ]
        then
                wget http://files.directadmin.com/services/customapache/apache.logrotate -O /etc/logrotate.d/apache
        fi

}	

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

doGD() {
	if [ $FORCE = 0 ]
	then
		echo -n "Do you want to build GD? (y,n): ";
		read yesno;
		echo "";
		if [ "$yesno" = "n" ]
		then
			return;
		fi
	fi
	
	cd $CWD;
	FILE=${CWD}/gd-${GD_VER}.tar.gz
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";
	cd gd-${GD_VER}
	echo "Configuring gd-${GD_VER}...";

	./configure --with-png=/usr/local --with-jpeg=/usr/local

	echo "Done. Making gd-${GD_VER}...";
	
	
	while
	echo "Trying to make gd..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";
	
	echo "Installing gd-${GD_VER}...";
	make install
	echo "Done gd.";

	cd $CWD
}

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

doCURL() {

	if [ $FORCE = 0 ]
	then
		echo -n "Do you want to build CURL? (y,n): ";
		read yesno;
		echo ""
		if [ "$yesno" = "n" ]
		then
			return;
		fi
	fi
	
	cd $CWD;
	FILE=${CWD}/curl-${CURL_VER}.tar.gz
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";
	cd curl-${CURL_VER}
	echo "Configuring curl-${CURL_VER}...";
	./configure
	/usr/bin/perl -pi -e 's/\#define HAVE_OPENSSL_ENGINE_H 1/\/\/\#define HAVE_OPENSSL_ENGINE_H 0/' ./lib/config.h;
	echo "Done. Making curl-${CURL_VER}...";
	while
	echo "Trying to make cURL..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";
	echo "Installing curl-${CURL_VER}...";
	make install
	echo "Done curl.";

	cd $CWD
}

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

doMCrypt() {
	if [ $FORCE = 0 ]
	then
		echo -n "Do you want to build mCrypt? (y,n): ";
		read yesno;
		echo ""
		if [ "$yesno" = "n" ]
		then
			return;
		fi
	fi
	
	cd $CWD;
	FILE=${CWD}/libmcrypt-${MCRYPT_VER}.tar.gz
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";
	cd libmcrypt-${MCRYPT_VER}
	echo "Configuring libmcrypt-${MCRYPT_VER}...";
	./configure
	echo "Done. Making libmcrypt-${MCRYPT_VER}...";
	while
	echo "Trying to make mCrypt..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				exit 0;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";
	echo "Installing mcrypt-${MCRYPT_VER}...";
	make install
	echo "Done mcrypt.";

	cd $CWD
}

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

doModPerl() {
	cd $CWD;
	FILE=${CWD}/${MODPERL_FILE}
	checkFile $FILE
	echo "Extracting ...";
	tar xzf $FILE
	echo "Done.";
	cd ${MODPERL_DIR}
	echo "Configuring ${MODPERL_DIR}...";
	perl Makefile.PL USE_APXS=1 WITH_APXS=/usr/sbin/apxs EVERYTHING=1
	echo "Done. Making ${MODPERL_DIR}...";
	while
	echo "Trying to make mod_perl..."
	do
	{
		make

		if [ $? -ne 0 ]
		then
			echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
			read yesno;
			echo "";
			if [ "$yesno" = "n" ]
			then	
				return;
			fi
		else
			break;
		fi
	}
	done
	echo "Make complete";
	echo "Installing ${MODPERL_DIR}...";
	make install
	echo "Done mod_perl.";

	cd $CWD
}

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

doWebalizer() {
	
	PREFIX=/usr
	if [ $OS = "FreeBSD" ]; then
		PREFIX=/usr/local
	fi

	if [ -e ${PREFIX}/bin/webalizer ]; then
		return;
	fi

	

        cd $CWD;
        getFile $WEBALIZER_FILE
        tar xzf $WEBALIZER_FILE
        cd $WEBALIZER

        ./configure --prefix=${PREFIX} --with-png=/usr/local/lib --with-gdlib=/usr/local/lib --with-gd=/usr/local/include
	
	/usr/bin/perl -pi -e 's/^LIBS   = -lgd -lpng -lz -lm/LIBS   = -lgd -lpng -lz -lm -liconv/' Makefile

        while
        echo "Trying to make webalizer..."
        do
        {
                make

                if [ $? -ne 0 ]
                then
                        echo -n -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
                        read yesno;
                        echo "";
                        if [ "$yesno" = "n" ]
                        then
                                return;
                        fi
                else
                        break;
                fi
        }
        done

        make install

	if [ -e /etc/webalizer.conf ]; then
		mv -f /etc/webalizer.conf /etc/webalizer.conf.moved
	fi
}

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

doAll() {
	FORCE=1;
	
	#doApache "fake";	
	#doModFrontpage;
	doApache;
	doModFrontpage;
	doPhp;
	doModPerl;
	#doModFrontpage;
	doFrontpageExt;
	doWebalizer;

	#if [ $OS = "FreeBSD" ]
	#then
	#	/usr/local/etc/rc.d/httpd restart
	#else
	#	/sbin/service httpd restart
	#fi
	
	echo -e "\n\n\n";
	echo "*************************************";
	echo "*                                   *";
	echo "*   All parts have been installed   *";
	echo "*                                   *";
	echo "*************************************";
	echo "";
	echo "Type: service httpd restart";
}

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

doClean() {
	cd $CWD
	rm -rf apache_${APACHE_VER};
	rm -rf mod_ssl-${MODSSL_VER}-${APACHE_VER};
	rm -rf php-${PHP_VER};
	rm -rf ${MODPERL_DIR};
	rm -rf curl-${CURL_VER};
	rm -rf gd-${GD_VER};
	rm -rf zlib-${ZLIB_VER};
	rm -rf ${JPEGDIR}
	rm -rf libpng-${PNG_VER};
	rm -rf libmcrypt-${MCRYPT_VER};
	rm -rf mod_frontpage-${FRONTPAGE_VER};
	rm -rf ${ZENDNAME}
	rm -rf ${WEBALIZER}
	
	echo "All clean!";
}

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

doUpdate() {
	cd $CWD
	if [ $OS = "FreeBSD" ]
	then
		fetch -o ${CWD}/build.new ${WEBPATH}/build
		fetch -o ${CWD}/README ${WEBPATH}/README
	else
		wget ${WEBPATH}/build -O ${CWD}/build.new
		wget ${WEBPATH}/README -O ${CWD}/README
	fi
	
	mv -f build.new build
	chmod 755 build
	
	./build update_data

}

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

getFile() {
	if [ ! -e $1 ]
	then
		echo -e "Downloading\t\t$1...";
		if [ $OS = "FreeBSD" ]; then
			fetch ${WEBPATH}/${1};
		else
			wget ${WEBPATH}/${1} -O ${CWD}/${1};
		fi
		if [ ! -e $1 ]
		then
			echo "Fileserver is down, using the backup file server..";
			if [ $OS = "FreeBSD" ]; then
				fetch ${WEBPATH_BACKUP}/${1};
			else
				wget ${WEBPATH_BACKUP}/${1} -O ${CWD}/${1};
			fi
		fi
		
	else
		echo -e "File already exists:\t${1}";
	fi
	
}

doUpdateData() {
	cd $CWD
	getFile apache_${APACHE_VER}.tar.gz
	getFile configure.apache_ssl
	chmod 755 configure.apache_ssl
	getFile configure.php
	chmod 755 configure.php
	getFile $FP_PATCH0
	getFile $FP_PATCH1
	#getFile fp-patch-apache_1.3.22
	#getFile fp-patch-suexec
	getFile httpd
	getFile httpd_freebsd
	getFile mime.types
	getFile $MODPERL_FILE
	getFile mod_ssl-${MODSSL_VER}-${APACHE_VER}.tar.gz
	getFile curl-${CURL_VER}.tar.gz
	getFile gd-${GD_VER}.tar.gz
	getFile libmcrypt-${MCRYPT_VER}.tar.gz
	getFile php-${PHP_VER}.tar.gz
	getFile mod_frontpage-${FRONTPAGE_VER}.tar.gz
	getFile ${FRONTPAGE_EXT}
	getFile ${WEBALIZER_FILE}
}

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

doZend() {

	cd $CWD;
	getFile $ZENDFILE
	tar xzf $ZENDFILE
	cd $ZENDNAME;
	echo "";
	echo -e "Location of php.ini:\n  /usr/local/lib";
	echo "Press return to continue...";
	read bogusdata;
	
	./install.sh
	
	cd $CWD;
}

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

doLibJpeg() {

        cd $CWD;
        getFile $JPEGFILE
        tar xzf $JPEGFILE
        cd $JPEGDIR

	./configure

        while
        echo "Trying to make libjpeg..."
        do
        {
                make libjpeg.a

                if [ $? -ne 0 ]
                then
                        echo -n -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
                        read yesno;
                        echo "";
                        if [ "$yesno" = "n" ]
                        then
                                exit 0;
                        fi
                else
                        break;
                fi
        }
        done

	make install-lib
}

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

doZlib() {

        cd $CWD;
        getFile zlib-${ZLIB_VER}.tar.gz
        tar xzf zlib-${ZLIB_VER}.tar.gz
        cd zlib-${ZLIB_VER}

        ./configure --shared

        while
        echo "Trying to make libz..."
        do
        {
                make

                if [ $? -ne 0 ]
                then
                        echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
                        read yesno;
                        echo "";
                        if [ "$yesno" = "n" ]
                        then
                                exit 0;
                        fi
                else
                        break;
                fi
        }
        done

        make install
}

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

doLibPng() {

        cd $CWD;
        getFile libpng-${PNG_VER}.tar.gz
        tar xzf libpng-${PNG_VER}.tar.gz
        cd libpng-${PNG_VER}

	cp scripts/makefile.linux makefile

        while
        echo "Trying to make libpng"
        do
        {
                make test

                if [ $? -ne 0 ]
                then
                        echo -e "\n*** The make has failed, do you want to try to make again? (y,n): ";
                        read yesno;
                        echo "";
                        if [ "$yesno" = "n" ]
                        then
                                exit 0;
                        fi
                else
                        break;
                fi
        }
        done

        make install
}

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

case "$1" in
    all)
	doAll;
        ;;
    php)
	doPhp;
	;;
    gd) doGD;
    	;;
    libjpeg) doLibJpeg;
	;;
    libpng) doLibPng;
	;;
    zlib) doZlib;
	;;
    curl) doCURL;
    	;;
    mcrypt) doMCrypt;
    	;;
    apache_mod_ssl) doApache;
    	;;
    mod_perl) doModPerl;
    	;;
    mod_frontpage) doModFrontpage;
    	;;
    frontpage_ext) doFrontpageExt;
    	;;
    clean) doClean;
    	;;
    update) doUpdate;
    	;;
    update_data) doUpdateData;
    	;;
    webalizer) doWebalizer;
	;;
    zend) doZend;
    	;;
    db4) doDB4;
    	;;    
    * )	showHelp;
	exit 0;
        ;;
esac

exit 0;
