#!/bin/sh

## Chris Pinchak, July 11, 2001
##	Modifications by Paul Lu, July 12, 2001

## Downloads and installs all of the software packages required for PBSWeb.
## You may edit this file to comment out any packages that have previously
## been installed.
##


## NOTE 1: 
##    	You have to *interactively* provide a passphrase (and other
##	information) related to the certificates used by SSL for encryption.
##	If you do not use SSL, then this script is completely automatic.


## NOTE 2:
##	This script is meant as a convenience.  It is NOT (yet) designed
##	to be particularly robust against failure modes (e.g., missing
##	executables in your path or running out of disk space).

## REQUIREMENTS:
##	Bourne shell (sh) or one of it's derivatives (ksh, bash, etc)
##	wget on your PATH

## OPTIONS:
##      --with-ssl: turns SSL support on

## Example Usage:
##	./build_single_acct
## or
##	./build_single_acct --with-ssl		(highly recommended)

###############################################################################
##                                                                           ##
##                             IMPORTANT                                     ##
##                                                                           ##
## You must edit and set the following variables before running:             ##
## 1.  I_HAVE_SET_VARIABLES                                                  ##
##	- our heavy-handed way of making sure you followed these             ##
##	  instructions :-)                                                   ##
##	- Change line to read "I_HAVE_SET_VARIABLES=1" when you are done     ##
##                                                                           ##
## 2.  HOST                                                                  ##
##      - i.e., fully qualified domain name (FQDN)                           ##
##	- give this the full hostname of the machine on which you want to    ##
##	  run the PBSWeb / Apache server.                                    ##
##	- the machine name is used to generate the certificates needed       ##
##	  for SSL-based encryption                                           ##
##                                                                           ##
## 3.  RANDOM_SEED_FILE                                                      ##
##	- set this to a path to a large file somewhere in your filesystem    ##
##	- this file is used to seed the random numbers used to generate      ##
##	  the encryption certificates.  See the documentation for SSL        ##
##	  for more details                                                   ##
##                                                                           ##
## 4.  HOME                                                                  ##
##      - path to root directory of the installation                         ##
##                                                                           ##
## 5.  POSTGRESQL_URL, OPENSSL_URL, APACHE_URL, MODSSL_URL, PHP_URL          ##
##     - URLs of the various software packages used by PBSWeb                ##
##     - currently, these point to our mirrored copies of the packages       ##
##     - we also provide an alternative "official" mirror of the packages    ##
##     - you are welcome to set these to your favourite mirror sites         ##
##                                                                           ##
###############################################################################

# ****************** Start of user configurable variables ***************
# 0 = Script not edited, 1 = Script has been edited
# MODIFY ME
I_HAVE_SET_VARIABLES=0
if [ $I_HAVE_SET_VARIABLES = 0 ]; then
	echo "*** PBSWeb Installation Script:"
	echo "*** Please edit this script as described in the embedded"
	echo "*** comments, before executing.  Thank you.  Exiting."
	exit -1
fi

# Example:
# Fully qualified domain name (FQDN) of the server
# MODIFY ME
HOST="brule.cs.ualberta.ca"

# Some large file to help seed random number generator for SSL
# MODIFY ME
RANDOM_SEED_FILE="/usr/brule6/prof/paullu/web_docs/Papers/aurora.vol2.chp6.ps.Z"

# Path to installation directory of PBSWeb
# MODIFY ME
HOME="/usr/brule6/prof/paullu/PBSWebInstall"



# Location of PostgreSQL package
POSTGRESQL_URL_1="ftp://looking-glass.usask.ca/pub/postgresql/source/v7.1.2/postgresql-7.1.2.tar.gz"
POSTGRESQL_URL_2="http://www.cs.ualberta.ca/~paullu/PBSWeb/postgresql-7.1.2.tar.gz"
POSTGRESQL_URL=$POSTGRESQL_URL_2

# Location of OpenSSL package
OPENSSL_URL_1="http://www.openssl.org/source/openssl-0.9.6a.tar.gz"
OPENSSL_URL_2="http://www.cs.ualberta.ca/~paullu/PBSWeb/openssl-0.9.6a.tar.gz"
OPENSSL_URL=$OPENSSL_URL_2

# Location of Apache package
APACHE_URL_1="http://httpd.apache.org/dist/httpd/apache_1.3.20.tar.gz"
APACHE_URL_2="http://www.cs.ualberta.ca/~paullu/PBSWeb/apache_1.3.20.tar.gz"
APACHE_URL=$APACHE_URL_2;

# Location of modSSL package (for Apache)
MODSSL_URL_1="http://www.modssl.org/source/mod_ssl-2.8.4-1.3.20.tar.gz"
MODSSL_URL_2="http://www.cs.ualberta.ca/~paullu/PBSWeb/mod_ssl-2.8.4-1.3.20.tar.gz"
MODSSL_URL=$MODSSL_URL_2

# Location of PHP package (for Apache)
PHP_URL_1="http://www.php.net/do_download.php?download_file=php-4.0.6.tar.gz&source_site=www.php.net"
PHP_URL_2="http://www.cs.ualberta.ca/~paullu/PBSWeb/php-4.0.6.tar.gz"
PHP_URL=$PHP_URL_2

# ****************** End of user configurable variables ***************




## Determine if the --with-ssl option is provided.  It's the only option, so we
## might as well not use getopts.

# Process command line

PRINT_HELP=

USE_SSL=
if [ ! -z "$@" ]; then
    # Various ways to ask for help
    if [ "$@" = "--help" ]; then
	PRINT_HELP=1;
    fi
    if [ "$@" = "-help" ]; then
	PRINT_HELP=1;
    fi
    if [ "$@" = "-h" ]; then
	PRINT_HELP=1;
    fi
    if [ "$@" = "-h" ]; then
	PRINT_HELP=1;
    fi

    # Print help info, if appropriate
    if [ ! -z "$PRINT_HELP" ]; then
	echo "build_single_acct:  Usage:"
	echo "   ./build_single_acct --help       (see this output)"
	echo "   ./build_single_acct --with-ssl   (build with SSL; recommended)"
	echo "   ./build_single_acct              (build without SSL)"
	exit 0
    fi

    if [ "$@" = "--with-ssl" ]; then
	USE_SSL="yes";
	echo "*** Building with SSL, as requested"
    else
	echo "***** NOT building with SSL, as requested"
    fi
fi

## Check for patch file, which should have come with this script
if [ -e "php.ini.patch" ]; then
	echo "*** Found php.ini.patch.  OK."
else
	echo "***** Missing php.ini.patch.  Exiting."
	exit -1
fi

## Check for file to seed random number for SSL
if [ -e $RANDOM_SEED_FILE ]; then
	echo "*** Found file:  $RANDOM_SEED_FILE.  OK."
else
	echo "***** Missing file:  $RANDOM_SEED_FILE.  Exiting."
	exit -1
fi

## Download the PostgreSQL package
wget -P $HOME $POSTGRESQL_URL &&

## Unpack the PostgreSQL package
tar xvzf $HOME/postgresql-7.1.2.tar.gz -C $HOME/ &&

## Configure PostgreSQL with the required options
(cd $HOME/postgresql-7.1.2 && ./configure --prefix=$HOME/pgsql --with-perl --with-tcl) &&

## Make and install PostgreSQL
(cd $HOME/postgresql-7.1.2 && make) &&

(cd $HOME/postgresql-7.1.2 && make install) &&

## Initialize the PostgreSQL data directory
($HOME/pgsql/bin/initdb -D $HOME/pgsql/data) &&

if [ ! -z "$USE_SSL" ]; then

    ## Get the OpenSSL package

    wget -P $HOME/ $OPENSSL_URL &&
    tar xvzf $HOME/openssl-0.9.6a.tar.gz -C $HOME/ &&

    ## Install the OpenSSL package

    (cd $HOME/openssl-0.9.6a && ./config --prefix=$HOME/openssl) &&
    (cd $HOME/openssl-0.9.6a && make) &&
    (cd $HOME/openssl-0.9.6a && make test) &&
    (cd $HOME/openssl-0.9.6a && make install)

fi &&

## Get the Apache HTTPD
wget -P $HOME/ $APACHE_URL &&

tar xvzf $HOME/apache_1.3.20.tar.gz -C $HOME/ &&

if [ ! -z "$USE_SSL" ]; then

    ## Get the modSSL package

    wget -P $HOME/ $MODSSL_URL &&
    tar xvzf $HOME/mod_ssl-2.8.4-1.3.20.tar.gz -C $HOME/ &&

    ## Apply the modSSL patch to Apache

    (cd $HOME/mod_ssl-2.8.4-1.3.20 &&
	./configure --with-apache=../apache_1.3.20)
fi &&

## Preconfigure Apache for PHP.
(cd $HOME/apache_1.3.20 && ./configure --prefix=$HOME/apache) &&

## Get the PHP package.
wget -P $HOME/ $PHP_URL &&

tar xvzf $HOME/php-4.0.6.tar.gz -C $HOME/ &&

## Install PHP
(cd $HOME/php-4.0.6 && CFLAGS="-O2 -I$HOME/inst/openssl/include" \
	./configure --prefix=$HOME/php \
	--with-apache=$HOME/apache_1.3.20 \
	--with-pgsql=$HOME/pgsql \
	--with-config-file-path=$HOME/php/lib \
	--without-mysql \
	--enable-memory-limit=yes \
	--enable-debug=no \
	--enable-force-cgi-redirect --enable-discard-path \
	--enable-trans-sid) &&

(cd $HOME/php-4.0.6 && make) &&

(cd $HOME/php-4.0.6 && make install) &&

## Modify the php.ini file.
cp $HOME/php-4.0.6/php.ini-dist $HOME/php/lib/php.ini &&

patch $HOME/php/lib/php.ini php.ini.patch &&

echo "******* SSL step..." &&

## Generate keys and certificates.
if [ ! -z "$USE_SSL" ]; then
    echo "*****************************************************************" &&
    echo "*** BE PREPARED TO ENTER A PASSPHRASE                         ***" &&
    echo "*****************************************************************" &&
    $HOME/openssl/bin/openssl genrsa -des3 -rand "$RANDOM_SEED_FILE" -out "$HOST.key" 1024 &&
    echo "*****************************************************************" &&
    echo "*** USE WHAT HOST IS SET TO FOR THE COMMON NAME, OR IT WON'T WORK" &&
    echo "*** i.e., When asked for 'Common Name (eg, YOUR name)' give the  " &&
    echo "***    fully qualified domain name (FQDN) of your *server*.      " &&
    echo "*** See http://www.thawte.com/certs/server/keygen/mod_ssl.html   " &&
    echo "***    especially Point 5.                                       " &&
    echo "*****************************************************************" &&
    $HOME/openssl/bin/openssl req -new -key "$HOST.key" -out "$HOST.csr" &&
    $HOME/openssl/bin/openssl x509 -req -days 30 -in "$HOST.csr" -signkey "$HOST".key -out "$HOST.crt" &&
    mkdir -p $HOME/PrivateKeys/$HOST &&
    mv "$HOST.key" "$HOST.csr" "$HOST.crt" $HOME/PrivateKeys/$HOST
else
    echo "*** Skipping SSL step..."
fi &&

## Install Apache with SSL support
if [ ! -z "$USE_SSL" ]; then
    (cd $HOME/apache_1.3.20 && SSL_BASE=$HOME/openssl \
	./configure \
	--prefix=$HOME/apache \
	--enable-module=ssl \
	--activate-module=src/modules/php4/libphp4.a \
	--enable-module=php4) &&
    (cd $HOME/apache_1.3.20 && make) &&
    (cd $HOME/apache_1.3.20 && make certificate TYPE=existing \
	CRT=$HOME/PrivateKeys/$HOST/$HOST.crt \
	KEY=$HOME/PrivateKeys/$HOST/$HOST.key) &&
    (cd $HOME/apache_1.3.20 && make install)
## Install Apache without SSL support
else
    (cd $HOME/apache_1.3.20 && ./configure --prefix=$HOME/apache \
	--activate-module=src/modules/php4/libphp4.a \
	--enable-module=php4) &&
    (cd $HOME/apache_1.3.20 && make) &&
    (cd $HOME/apache_1.3.20 && make install)
fi

## Print a message when finished.
if [ $? -ne 0 ]; then
    echo ""
    echo "*** Installation failed"
else
    echo ""
    echo "*** Installation successful"
    echo "*** Continue from Step 17 of README.PBSWeb"
fi
