#!/bin/bash -x
#This script is meant to automate the installation procedure of LAMS 2.0 on *nix machines.
#You should not need to modify anything beyond the first section labeled "Declare variables".
#Date: 04-Sep-2006
#Author: Al-Faisal El-Dajani

#Declare variables
HOST=localhost:8080
LAMS_HOME=/usr/local/lams
TEMP_DIR=/var/opt/lams/tmp
DUMP_DIR=/var/opt/lams/dump
LANGUAGE=ar_JO
DIRECTION=RTL
DB_PASSWORD=123
SMTP_SERVER=""
SUPPORT_MAIL=""
PLATFORM=unix
JBOSS_HOME=/usr/local/jboss-4.0.2

#Do NOT modify beyond this point, unless if you know what you are doing. You have been warned!!

#Stop JBoss
cd ${JBOSS_HOME}/bin
./shutdown.sh -S

#Remove previous installation
rm -rf ${LAMS_HOME}
mysqladmin -u root drop lams -f

#Prepare new database
mysqladmin -u root create lams
mysql -u root -e "grant all privileges on lams.* to 'lams'@'localhost' identified by '123'" lams

#Get LAMS 2.0 from CVS
cd ~
rm -rf lams
mkdir lams
cd lams
export CVSROOT=:pserver:anonymous@lamscvs.melcoe.mq.edu.au:/usr/local/cvsroot
cvs co lams_admin
cvs co lams_build
cvs co lams_central
cvs co lams_common
cvs co lams_contentrepository
cvs co lams_learning
cvs co lams_monitoring
cvs co lams_tool_chat
cvs co lams_tool_forum
cvs co lams_tool_lamc
cvs co lams_tool_laqa
cvs co lams_tool_larsrc
cvs co lams_tool_nb
cvs co lams_tool_sbmt
cvs co lams_tool_vote
cvs co lams_tool_notebook
cvs co lams_www
cd ../
cp -R lams ${LAMS_HOME}

#Modify configuration files
#Since this is my first experience with sed, I couldn't make variable substitution work, so i hard coded them to get them working on my machine.
cd ${LAMS_HOME}/lams_build/conf/${PLATFORM}/lams
cat lams.xml > temp; cat temp | sed -e "s/<ServerURL>http:\/\/shaun.melcoe.mq.edu.au/<ServerURL>http:\/\/localhost:8080/" > lams.xml
cat lams.xml > temp; cat temp | sed -e "s/<LamsHome>\/var\/opt\/lams/<LamsHome>\/usr\/local\/lams/" > lams.xml
#cat lams.xml > temp; cat temp | sed -e "s/<TempDir>\/var\/opt\/lams\/temp/<Tempdir>${TEMP_DIR}/" > lams.xml
#cat lams.xml > temp; cat temp | sed -e "s/<DumpDir>\/var\/opt\/lams\/dump/<DumpDir>${DUMP_DIR}/" > lams.xml
#cat lams.xml > temp; cat temp | sed -e "s/<EARDir>\/usr\/local\/jboss-4.0.2/<EARDir>${JBOSS_HOME}/" > lams.xml
#cat lams.xml > temp; cat temp | sed -e "s/mail.lib.mq.edu.au//" > lams.xml
#cat lams.xml > temp; cat temp | sed -e "s/lams_support@melcoe.mq.edu.au//" > lams.xml
cat lams.xml > temp; cat temp | sed -e "s/en_AU/ar_JO/g" > lams.xml
cat lams.xml > temp; cat temp | sed -e "s/LTR/RTL/" > lams.xml
rm temp

cd ${LAMS_HOME}/lams_build
cat common.properties > temp; cat temp | sed -e "s/osPropertiesName=windows/osPropertiesName=unix/g" > common.properties
cat common.properties > temp; cat temp | sed -e "s/lamsdemo/123/g" > common.properties
rm temp

cd ${LAMS_HOME}/lams_build/conf/${PLATFORM}/jboss/service
cat mysql-ds.xml > temp; cat temp | sed -e "s/lamsdemo/123/g" > mysql-ds.xml
rm temp

#Build LAMS
cd ${LAMS_HOME}/lams_build
/usr/local/apache-ant/bin/ant rebuild-db
/usr/local/apache-ant/bin/ant assemble-ear
/usr/local/apache-ant/bin/ant deploy-ear
/usr/local/apache-ant/bin/ant deploy-tools
/usr/local/apache-ant/bin/ant copyfiles

#Run JBoss
cd ${JBOSS_HOME}/bin
./run.sh &
