#!/bin/sh

echo "------------------------ Settings ---------------------------------------"
# Installation directory and software versions to be installed
# Using tcl version 8.4.14 because 8.5 causes problems

inst_dir=/usr/local/aolserver
v_aol=4.5
v_tcllib=1.10
v_thread=2.6.5
v_xotcl=1.6.0
v_tcl=8.4.17
v_daemon=0.76
v_pam=0.1
v_tdom=0.8.3

pg_incl=/usr/include/pgsql
pg_ld=/usr/local/pgsql/lib
pg_lib=/var/lib/pgsql

echo "
Installation Script for Aolserver (c) 2008 Malte Sussdorff, Nima Mazloumi

LICENSE    This program comes with ABSOLUTELY NO WARRANTY;
           This is free software, and you are welcome to redistribute it under certain conditions;
           For details see http://www.gnu.org/licenses.

SETTINGS   Install-Dir  ${inst_dir}
           Aolserver    ${v_aol}      latest from cvs with (snpostgres, nscache, nssha1)
           Tcllib       ${v_tcllib}
           Thread       ${v_thread}
           XoTcl        ${v_xotcl}
           TCL          ${v_tcl}
           tDOM         ${v_tdom}    latest from cvs, currently 
           Daemon       ${v_daemon}     uncomment if needed
           nspam        ${v_pam}      uncomment if needed

NOTE       You need make, cvs, wget, gcc
                    pam-dev (optional)
                    posgresql-dev (${pg_incl} ${pg_ld} ${pg_lib})

WARNING    Check Settings AND Cleanup section before running this script!
           If you know what you're doing take out the 'exit' command and run this script!
"

exit

echo "------------------------ Cleanup -----------------------------------------"
# First we clean up

rm -rf ${inst_dir}45
rm -r aolserver
rm -r openacs-4
rm -r nscache
rm -r nssha1     
rm -r nspostgres
rm -r nspam
rm -r tcl${v_tcl}
rm    tcl${v_tcl}-src.tar.gz
rm -r tcllib-${v_tcllib}
rm    tcllib-${v_tcllib}.tar.bz2
rm -r thread${v_thread}
rm    thread${v_thread}.tar.gz
rm -r xotcl-${v_xotcl}
rm    xotcl-${v_xotcl}.tar.gz
rm    nspam-${v_pam}.tar.gz
rm -r /package
rm aolserver45-nsd-conn.patch
#rm -r tDOM-${v_tdom}
rm -r tdom

echo "------------------------ Downloading sources ----------------------------"
# Let's download latest code
# Note: add -nostartfiles to aolserver/include/ns.mak for LDLIB for 64bit system

cvs -z3 -d:pserver:anonymous@aolserver.cvs.sourceforge.net:/cvsroot/aolserver login
cvs -z3 -d:pserver:anonymous@aolserver.cvs.sourceforge.net:/cvsroot/aolserver co aolserver
cvs -z3 -d:pserver:anonymous@aolserver.cvs.sourceforge.net:/cvsroot/aolserver co nscache
cvs -z3 -d:pserver:anonymous@aolserver.cvs.sourceforge.net:/cvsroot/aolserver co nspostgres
cvs -z3 -d:pserver:anonymous@aolserver.cvs.sourceforge.net:/cvsroot/aolserver co nssha1
cvs -d:pserver:anonymous@cvs.tdom.org:/usr/local/pubcvs login
cvs -z3 -d:pserver:anonymous@cvs.tdom.org:/usr/local/pubcvs co tdom
wget http://heanet.dl.sourceforge.net/sourceforge/tcllib/tcllib-${v_tcllib}.tar.bz2
wget http://heanet.dl.sourceforge.net/sourceforge/tcl/thread${v_thread}.tar.gz
wget http://media.wu-wien.ac.at/download/xotcl-${v_xotcl}.tar.gz
wget http://heanet.dl.sourceforge.net/sourceforge/tcl/tcl${v_tcl}-src.tar.gz
wget http://openacs.org/storage/view/nspam-${v_pam}.tar.gz

echo "------------------------ Installing TCL ---------------------------------"

tar xfz tcl${v_tcl}-src.tar.gz
cd tcl${v_tcl}/unix
#export ac_cv_func_strtod=yes
#export tcl_cv_strtod_buggy=1
./configure --enable-threads --prefix=${inst_dir}45
make install

echo "------------------------ Installing AOLServer ---------------------------"

cd ../../aolserver
#wget http://media.wu-wien.ac.at/download/aolserver45-nsd-conn.patch
#patch -p0 <aolserver45-nsd-conn.patch
#export CPPFLAGS="-nostartfiles"
#./configure TCLSH=${inst_dir}45/bin/tclsh8.4 --prefix=${inst_dir}45 --with-tcl=${inst_dir}45/lib
${inst_dir}45/bin/tclsh8.4 ./nsconfig.tcl
make -i install

# Remove the symbolic link to aolserver.
# If this is not a symbolic link but the full path to your aolserver
# Make sure it becomes a symbolic link by issuing
# mv /usr/local/aolserver /usr/local/aolserver<yourversion>
rm ${inst_dir}
ln -s ${inst_dir}45 ${inst_dir}

# Make sure to call nsd-postgres instead of nsd if you use postgresql
cd ..
cp nsd-postgres.txt ${inst_dir}45/bin/nsd-postgres
chmod 755 ${inst_dir}45/bin/nsd-postgres

echo "------------------------ Installing Cache -------------------------------"

cd nscache
make install NSHOME=${inst_dir}45

echo "------------------------ Installing SHA1 --------------------------------"

cd ../nssha1
make install NSHOME=${inst_dir}45

#echo "------------------------ Installing nspam ------------------------------"

#cd ..
#tar xfz nspam-${v_pam}.tar.gz
#cd nspam
#make install NSHOME=${inst_dir}45

echo "------------------------ Installing NSPostgres --------------------------"

cd ../nspostgres
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pg_ld}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pg_lib}
cp ${pg_incl}/libpq-fe.h .
cp ${pg_incl}/postgres_ext.h .
make install POSTGRES=${pg_lib} ACS=1 NSHOME=${inst_dir}45

echo "------------------------ Installing tdom --------------------------------"

cd ../tdom
#tar xfz tDOM-${v_tdom}.tar.gz
#cd tDOM-${v_tdom}/unix
# Note, if you use bash31 you need to apply a patch
# See http://openacs.org/forums/message-view?message_id=369867 for details
./configure --enable-threads --disable-tdomalloc --prefix=${inst_dir}45 --exec-prefix=${inst_dir}45 --with-aolserver=${inst_dir}45 --with-tcl=../tcl${v_tcl}/unix
make install

echo "------------------------ Installing TCLLib ------------------------------"

cd ..
tar xvfj tcllib-${v_tcllib}.tar.bz2
cd tcllib-${v_tcllib}
./configure --prefix=${inst_dir}45
make install

echo "------------------------ Installing Thread ------------------------------"

cd ..
tar xfz thread${v_thread}.tar.gz
cd thread${v_thread}/unix/
# Note, if you use bash31 you need to apply a patch
# See http://openacs.org/forums/message-view?message_id=369867
../configure --enable-threads --prefix=${inst_dir}45 --exec-prefix=${inst_dir}45 --with-aolserver=${inst_dir}45 --with-tcl=../../tcl${v_tcl}/unix
make
make install

echo "------------------------ Installing XoTCL -------------------------------"

cd ../..
tar xvfz xotcl-${v_xotcl}.tar.gz
cd xotcl-${v_xotcl}/
export CC=gcc
./configure --enable-threads --enable-symbols --prefix=${inst_dir}45 --exec-prefix=${inst_dir}45 --with-tcl=../tcl${v_tcl}/unix
make
make install-aol

#echo "------------------------ Installing Deamontools ------------------------"

#mkdir -p /package
#chmod 1755 /package
#cd /package
#wget http://cr.yp.to/daemontools/daemontools-${v_daemon}.tar.gz
#wget http://www.qmail.org/moni.csi.hu/pub/glibc-2.3.1/daemontools-${v_daemon}.errno.patch
#tar xvfz daemontools-${v_daemon}.tar.gz
#cd admin/daemontools-${v_daemon}
#patch -Np1 -i ../../daemontools-${v_daemon}.errno.patch
#package/install
