#!/bin/sh

OSSL="N"     # Install OpenSSL
CURL="N"     # Install CURL
MYSQL="N"    # Install MYSQL
IMAP="N"     # Install IMAP library
PHP="Y"      # Install PHP
 PHP_CGI="N"  # Build CGI version
 PHP_APA="Y"  # Build Apache module
 PHP_ORA="N"  # Build PHP with Oracle support

OSSLVER="0.9.6c"
CURLVER="7.9.2"
MYSQLVER="3.22.32"
PHPVER="4.1.1"

if [ "$OSSL" = "Y" ]; then
	[ -d openssl-${OSSLVER} ] || tar xvzf openssl-${OSSLVER}.tar.gz
	cd openssl-${OSSLVER}
	./config --prefix=/usr/local/openssl shared
	make
	make install
	cd ..
fi

if [ "$CURL" = "Y" ]; then
	[ -d curl-${CURLVER} ] || tar xvzf curl-${CURLVER}.tar.gz
	cd curl-${CURLVER}
	./configure --prefix=/usr/local/curl -with-openssl=/usr/local/openssl
	make
	make install
	ln -sf /usr/local/curl/bin/curl /usr/bin/curl
	cd ..
fi

if [ "$MYSQL" = "Y" ]; then
	[ -d mysql-${MYSQLVER} ] || tar xvzf mysql-${MYSQLVER}.tar.gz
	cd mysql-${MYSQLVER}

	/usr/sbin/groupadd mysql
	/usr/sbin/useradd mysql -g mysql -s /bin/false -d /usr/local/mysql

	./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql
	make
	make install
	./scripts/mysql_install_db

	chown -R mysql.mysql /usr/local/mysql/var
	ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
	cp support-files/mysql.server /etc/rc.d/init.d/mysql
	chmod +x /etc/rc.d/init.d/mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc0.d/K99mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc1.d/K99mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc2.d/S99mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc3.d/S99mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc5.d/S99mysql
	ln -sf /etc/rc.d/init.d/mysql /etc/rc.d/rc6.d/K99mysql
	cd ..
	echo "NOW RUN  /etc/rc.d/init.d/mysql start"
fi

if [ "$IMAP" = "Y" ]; then
	[ -d imap-2001a ] || tar xzvf c-client.tar.Z
	cd imap-2001a
	make lnp
	unalias cp
	mkdir /usr/include/imap 2>/dev/null
	cp c-client/c-client.a /usr/lib/.
	cp c-client/*.h /usr/inclide/imap/.
	cd ..
fi

if [ "$PHP" = "Y" ]; then
	[ -d php-${PHPVER} ] || tar xvzf php-${PHPVER}.tar.gz
	cd php-${PHPVER}
	CURL="--with-curl=/usr/local/curl "
#	MYSQL="--with-mysql=/usr/mysql/mysql "
	PGSQL="--with-pgsql=/usr/local/pgsql "
	IMAP="--with-imap --with-kerberos"
	INTERBASE="--with-interbase=/usr "
	ORA_PATH="/u01/app/oracle/products/8.1.7"
	ORACLE="--with-oracle=$ORA_PATH --with-oci8=$ORA_PATH --enable-sigchild "
	[ "$PHP_ORA" = "Y" ] || ORACLE=""

	if [ "$PHP_CGI" = "Y" ]; then
	    ./configure \
	      --enable-force-cgi-redirect --enable-discard-path \
	      $INTERBASE \
	      $ORACLE \
	      $MYSQL \
	      $PGSQL \
	      $CURL \
	      $IMAP
	    make
	    make install
	fi
	if [ "$PHP_APA" = "Y" ]; then
	    make clean distclean
    	    ./configure \
	      --with-apxs=/usr/sbin/apxs \
	      $INTERBASE \
	      $ORACLE \
	      $MYSQL \
	      $PGSQL \
	      $CURL \
	      $IMAP

	    make
	    make install
	fi
	cp ../php.ini /usr/local/lib/.
fi
