#!/bin/bash

echo "INFO: define required variables"
ARTIFACTS_HG_REPO="http://wald.intevation.org/hg/dive4elements/artifacts"
HTTPCLIIENT_HG_REPO="http://wald.intevation.org/hg/dive4elements/http-client"
FLYS_HG_REPO="http://wald.intevation.org/hg/dive4elements/flys"

ARTIFACTS_HG="hg.artifacts"
HTTPCLIENT_HG="hg.http-client"
FLYS_HG="hg.flys"

PREFIX="flys-"
RELEASE=${RELEASE:-default}
RELEASE_DATE=`date +'%Y-%m-%d'`
DIRECTORY=$PREFIX$RELEASE-$RELEASE_DATE

ARTIFACT_PORT=${ARTIFACT_PORT:-9002}
TOMCAT_PORT=${TOMCAT_PORT:-8005}

MAPSERVER_URL=${MAPSERVER_URL:-czech-republic.intevation.de}

echo "INFO: create server directories"
mkdir -p $DIRECTORY/server/bin/lib/own
mkdir $DIRECTORY/server/shapefiles
mkdir $DIRECTORY/client

echo "INFO: checkout sources"
echo " ... checkout $ARTIFACTS_HG_REPO"

rm -rf $ARTIFACTS_HG
hg clone $ARTIFACTS_HG_REPO $ARTIFACTS_HG
(cd $ARTIFACTS_HG && hg co $RELEASE)

echo " ... checkout $HTTPCLIIENT_HG_REPO"
rm -rf $HTTPCLIENT_HG
hg clone $HTTPCLIIENT_HG_REPO $HTTPCLIENT_HG
(cd $HTTPCLIENT_HG && hg co $RELEASE)

echo " ... checkout $FLYS_HG_REPO"
rm -rf $FLYS_HG
hg clone $FLYS_HG_REPO $FLYS_HG
(cd $FLYS_HG && hg co $RELEASE)

# adapt client configuration
echo "INFO: prepare configuration of web client"

sed -i -e "s@http://localhost:8181@http://localhost:$ARTIFACT_PORT@g" \
       -e "s@http://localhost:8888@http://localhost:$TOMCAT_PORT@g" \
    $FLYS_HG/flys-client/src/main/webapp/WEB-INF/web.xml

sed -i -e "s@/tmp/flys-client.log@/tmp/flys-client-${RELEASE}.log@g" \
    $FLYS_HG/flys-client/src/main/webapp/WEB-INF/log4j.properties

echo "INFO: download OpenLayers-2.11 for client"
curl -O http://openlayers.org/download/OpenLayers-2.11.tar.gz
tar xvfz OpenLayers-2.11.tar.gz
# TODO: Remove more superfluous OpenLayers stuff.
rm -rf OpenLayers-2.11/doc
rm -rf OpenLayers-2.11/tests
rm -rf OpenLayers-2.11/examples
mv OpenLayers-2.11 $FLYS_HG/flys-client/src/main/webapp/

# compile and build our code stuff
echo "INFO: compile and build sources"
mvn -f $ARTIFACTS_HG/pom.xml clean compile package install
mvn -f $FLYS_HG/flys-backend/pom.xml clean compile package install
mvn -f $FLYS_HG/flys-artifacts/pom.xml clean compile package dependency:copy-dependencies install
mvn -f $HTTPCLIENT_HG/pom.xml clean compile package install
mvn -f $FLYS_HG/flys-client/pom.xml clean compile package

## fetch the java stuff
echo "INFO: copy dependencies and libs"
cp $ARTIFACTS_HG/artifact-database/target/artifact-database-1.0-SNAPSHOT.jar $DIRECTORY/server/bin/lib/own/
cp $ARTIFACTS_HG/artifacts/target/artifacts-1.0-SNAPSHOT.jar $DIRECTORY/server/bin/lib/own/
cp $ARTIFACTS_HG/artifacts-common/target/artifacts-common-1.0-SNAPSHOT.jar $DIRECTORY/server/bin/lib/own/
cp $FLYS_HG/flys-backend/target/flys-backend-1.0-SNAPSHOT.jar $DIRECTORY/server/bin/lib/own/
cp $FLYS_HG/flys-artifacts/target/flys-artifacts-1.0-SNAPSHOT.jar $DIRECTORY/server/bin/lib/own/
cp $FLYS_HG/flys-client/target/FLYS-1.0-SNAPSHOT.war $DIRECTORY/client/flys-${RELEASE}.war
cp $FLYS_HG/flys-artifacts/target/dependency/* $DIRECTORY/server/bin/lib/

echo "INFO: copy scripts and libraries to target destination"
cp bin/run.sh $DIRECTORY/server/bin/
cp bin/wsplgen.exe $DIRECTORY/server/bin/
cp libs/* $DIRECTORY/server/bin/lib/

#echo "INFO: remove PostgreSQL and PostGIS libraries"
#rm $DIRECTORY/server/bin/lib/postg*
#rm $DIRECTORY/server/bin/lib/hibernate-spatial-postgis*

echo "INFO: remove Oralce libraries"
rm $DIRECTORY/server/bin/lib/hibernate-spatial-oracle-1.1.jar
rm $DIRECTORY/server/bin/lib/ojdbc*

# fetch the configuration stuff
echo "INFO: copy default configuration to target destination"
cp -R $FLYS_HG/flys-artifacts/doc/conf $DIRECTORY/server/

#cp confs/* $DIRECTORY/server/conf/
mkdir -o $DIRECTORY/server/conf

sed "s/8999/$ARTIFACT_PORT/g" \
    confs/rest-server.xml \
    > $DIRECTORY/server/conf/rest-server.xml

sed "s@http://czech-republic.intevation.de/@http://${MAPSERVER_URL}/@g" \
    "s@flys-default@flys-$RELEASE@g" \
    confs/floodmap.xml \
    > $DIRECTORY/server/conf/floodmap.xml

sed "s@/tmp/flys-server-default.log@/tmp/flys-server-${RELEASE}.log" \
    confs/log4j.properties \
    > $DIRECTORY/server/conf/log4j.properties

# TODO: Use templating here
cp confs/seddb-db.xml $DIRECTORY/server/conf/seddb-db.xml
cp confs/backend-db.xml $DIRECTORY/server/conf/backend-db.xml
cp confs/artifact-db.xml $DIRECTORY/server/conf/artifact-db.xml
cp confs/datacage-db.xml $DIRECTORY/server/conf/datacage-db.xml
cp confs/mapserver/fontset.txt $DIRECTORY/server/conf/mapserver/fontset.txt

cp $ARTIFACTS_HG/artifact-database/doc/schema-h2.sql h2/artifacts-h2.sql
cp $FLYS_HG/flys-artifacts/doc/conf/datacage.sql h2/datacage.sql

echo "INFO: create h2 database for artifacts and datacage"
h2/createArtifacts.sh
h2/createDatacage.sh

mv artifactsdb $DIRECTORY/server/
mv datacagedb $DIRECTORY/server/

echo "INFO: create tarball"
tar cvfz $DIRECTORY.tar.gz $DIRECTORY

echo "INFO: remove temporary files and directories"
rm -rf $ARTIFACTS_HG
rm -rf $HTTPCLIENT_HG
rm -rf $FLYS_HG
rm -rf $DIRECTORY
rm -rf OpenLayers.2.11.tar.gz
