#!/bin/bash
# Release script for Flys
#
# Authors:
# Andre Heinecke <aheinecke@intevation.de>
#
# Copyright:
# Copyright (C) 2013 Intevation GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

set -e
DEFAULT_WD=/tmp/flys-release
DEVELOPER=aheinecke

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"

REPOS="artifacts http-client flys"
# Do not use spaces in path
FLYS_SOURCE_DIR=/local-vol1/aheinecke/flys-release

SCRIPT_DIR=$(readlink -f `dirname $0`)
usage(){
    cat << EOF

usage: $0 [options] VERSION

Create a flys package

OPTIONS:
   -?, --help                      Show this message
   -w                              The working directory to use. (do not use spaces in path)
                                   Default: $DEFAULT_WD
   -t                              Tag the current default branch as "VERSION"
   -o, --oracle                    Release is for oracle.
   VERSION must be in the format MAYOR.MINOR.PATCH
EOF
exit 0
}
#   --backend-db-url                Url of database backend. Default: $BACKENDURL
#   --backend-db-pass               Backend db password. Default: $BACKENDPASS
#   --backend-db-port               Backend db port. Default: $BACKENDPORT
#   --backend-db-user               Backend db user. Default: $BACKENDUSER
#   --backend-db-backend            Backend db backend name. Default: $BACKENDBACK
#   --seddb-url                     Sediment db url. Default: $SEDDBURL
#   --seddb-port                    Sediment db port. Default: $SEDDBPORT
#   --seddb-user                    Sediment db user. Default: $SEDDBUSER
#   --seddb-pass                    Sediment db password. Default: $SEDDBPASS
#   --seddb-back                    Sediment db backend. Default: $SEDDBBACK
TOMCAT_PORT=${TOMCAT_PORT:-8282}
MAPSERVER_URL=${MAPSERVER_URL:-czech-republic.atlas.intevation.de}
WIKI_URL=${WIKI_URL:-https://flys-intern.intevation.de/Flys-3.0}

# Seddb Configuration
SEDDBURL=${SEDDBURL:-czech-republic.atlas.intevation.de}
SEDDBPORT=${SEDDBPORT:-1521}
SEDDBBACK=${SEDDBBACK:-XE}
SEDDBUSER=${SEDDBUSER:-seddb}
SEDDBPASS=${SEDDBPASS:-seddbpass}

# Backend configuration
BACKENDURL=${BACKENDURL:-czech-republic.atlas.intevation.de}
BACKENDPORT=${BACKENDPORT:-5432}
BACKENDBACK=${BACKENDBACK:-flys_2912}
BACKENDUSER=${BACKENDUSER:-flys_dami}
BACKENDPASS=${BACKENDPASS:-flys_dami}
INITSQLS=${INITSQLS:-}

OPTS=`getopt -o ?w:,t,o \
     -l help,oracle \
     -n $0 -- "$@"`

if [ $? != 0 ] ; then usage; fi
eval set -- "$OPTS"
while true ; do
  case "$1" in
    "-?"|"--help")
      usage;;
    "--")
      shift
      break;;
    "-w")
      WORK_DIR=$2
      shift 2;;
    "-o"|"--oracle")
      BUILD_ORACLE="TRUE"
      shift;;
    "-t")
      DO_TAG="TRUE"
      shift;;
    *)
      echo "Unknown Option $1"
      usage;;
  esac
done

if [ $# != 1 ]; then
    usage
fi

VERSION=$1
ARTIFACT_PORT=${ARTIFACT_PORT:-`echo 1$VERSION | sed 's/\.//g'`}

if [ -z $WORK_DIR ]; then
  WORK_DIR=$DEFAULT_WD
fi

mkdir -p $WORK_DIR

if [ ! -d $FLYS_SOURCE_DIR ]; then
    mkdir -p $FLYS_SOURCE_DIR
    echo "Cloning sources"
    cd $FLYS_SOURCE_DIR
    hg clone $ARTIFACTS_HG_REPO artifacts
    hg clone $HTTPCLIIENT_HG_REPO http-client
    hg clone $FLYS_HG_REPO flys
else
    echo "Updating sources / Reverting changes"
    cd $FLYS_SOURCE_DIR
    for repo in $REPOS; do
        cd $repo && hg pull && hg up && hg revert -a && cd $FLYS_SOURCE_DIR
    done;
fi

if [ "$DO_TAG" = "TRUE" ]; then
    echo "Tagging version $VERSION"
    for repo in $REPOS; do
        cd $repo
        CHANGESET=$(hg log -l1 |head -1 | awk -F: '{print $3}')
        echo ""
        echo "Do you really want to tag $repo rev: $CHANGESET as Version $VERSION?"
        echo "press enter to continue or CTRL+C to abort."
        echo ""
        hg log -l1
        read
        hg tag $VERSION -m "Added tag $VERSION for changeset $CHANGESET"
        hg push ssh://$DEVELOPER@scm.wald.intevation.org/hg/dive4elements/$repo
        cd $FLYS_SOURCE_DIR
    done;
fi

# Update to current version
for repo in $REPOS; do
    cd $repo
    hg up $VERSION
    cd $FLYS_SOURCE_DIR
done

rm -rf "$WORK_DIR/server" "$WORK_DIR/client"
cd $WORK_DIR
mkdir -p "$WORK_DIR/server/bin/lib/own"
mkdir "$WORK_DIR/server/shapefiles"
mkdir "$WORK_DIR/client"

echo "[INFO]: Preparing configuration of web client"
echo "[INFO]: Tomcat Port: $TOMCAT_PORT"
echo "[INFO]: Artifact Port: $ARTIFACT_PORT"
sed -i -e "s@http://localhost:8181@http://localhost:$ARTIFACT_PORT@g" \
       -e "s@http://localhost:8888@http://localhost:$TOMCAT_PORT@g" \
    $FLYS_SOURCE_DIR/flys/flys-client/src/main/webapp/WEB-INF/web.xml

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

find $FLYS_SOURCE_DIR/flys/flys-artifacts/src/main/resources/ -name messages_\*.properties \
    -exec sed -i "s@https://flys-intern.intevation.de/Flys-3.0@"'${WIKI_URL}'"@g" {} \;

if [ ! -f $FLYS_SOURCE_DIR/OpenLayers-2.11.tar.gz ]; then
    echo "INFO: download OpenLayers-2.11 for client"
    cd $FLYS_SOURCE_DIR
    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
    cd $WORK_DIR
fi
cp -r $FLYS_SOURCE_DIR/OpenLayers-2.11 $FLYS_SOURCE_DIR/flys/flys-client/src/main/webapp/

echo "INFO: compile and build sources"
mvn -f $FLYS_SOURCE_DIR/artifacts/pom.xml clean compile package install
mvn -f $FLYS_SOURCE_DIR/flys/flys-backend/pom.xml clean compile package install
mvn -f $FLYS_SOURCE_DIR/flys/flys-artifacts/pom.xml clean compile package dependency:copy-dependencies install
mvn -f $FLYS_SOURCE_DIR/http-client/pom.xml clean compile package install
mvn -f $FLYS_SOURCE_DIR/flys/flys-client/pom.xml clean compile package


echo "INFO: copy dependencies and libs"
cp $FLYS_SOURCE_DIR/artifacts/artifact-database/target/artifact-database-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/artifacts/artifacts/target/artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/artifacts/artifacts-common/target/artifacts-common-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/flys/flys-backend/target/flys-backend-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/flys/flys-artifacts/target/flys-artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/flys/flys-client/target/FLYS-1.0-SNAPSHOT.war $WORK_DIR/client/flys-${VERSION}.war
cp $FLYS_SOURCE_DIR/flys/flys-artifacts/target/dependency/* $WORK_DIR/server/bin/lib/

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


if [ "$BUILD_ORACLE" = "TRUE" ]; then
    echo "INFO: remove PostgreSQL and PostGIS libraries"
    rm $WORK_DIR/server/bin/lib/postg*
    rm $WORK_DIR/server/bin/lib/hibernate-spatial-postgis*
else
    echo "INFO: remove Oralce libraries"
    rm -f $WORK_DIR/server/bin/lib/hibernate-spatial-oracle-1.1.jar
    rm -f $WORK_DIR/server/bin/lib/ojdbc*
fi

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

sed -i "s/8181/$ARTIFACT_PORT/g" \
    $WORK_DIR/server/conf/rest-server.xml

sed -i -e "s@http://example.com/@http://${MAPSERVER_URL}/@g" \
    $WORK_DIR/server/conf/floodmap.xml

sed -i -e "s@http://example.com/@http://${MAPSERVER_URL}/@g" \
    $WORK_DIR/server/conf/rivermap.xml

sed "s@/tmp/flys-server-default.log@/var/log/flys/server-${VERSION}.log@" \
    $SCRIPT_DIR/confs/log4j.properties \
    > $WORK_DIR/server/conf/log4j.properties

cat > $WORK_DIR/server/conf/seddb-db.xml << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<seddb-database>
    <!-- This is the default SedDB db configuration. -->
    <user>$SEDDBUSER</user>
    <password>$SEDDBPASS</password>
    <dialect>org.hibernate.dialect.Oracle9iDialect</dialect>
    <driver>oracle.jdbc.driver.OracleDriver</driver>
    <url>jdbc:oracle:thin:@//$SEDDBURL:$SEDDBPORT/$SEDDBBACK </url>
</seddb-database>
EOF

if [ "$BUILD_ORACLE" = "TRUE" ]; then
    # Oracle backend configuration
    cat > $WORK_DIR/server/conf/backend-db.xml << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<backend-database>
    <user>$BACKENDUSER</user>
    <password>$BACKENDPASS</password>
    <dialect>org.hibernatespatial.oracle.OracleSpatial10gDialect</dialect>
    <driver>oracle.jdbc.driver.OracleDriver</driver>
    <url>jdbc:oracle:thin:@//$BACKENDURL:$BACKENDPORT/$BACKENDBACK</url>
    <connection-init-sqls>$INITSQLS</connection-init-sqls>
</backend-database>
EOF
else
    #Postgresql backend configuration
    cat > $WORK_DIR/server/conf/backend-db.xml << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<backend-database>
    <user>$BACKENDUSER</user>
    <password>$BACKENDPASS</password>
    <dialect>org.hibernate.dialect.PostgreSQLDialect</dialect>
    <driver>org.postgresql.Driver</driver>
    <url>jdbc:postgresql://$BACKENDURL:$BACKENDPORT/$BACKENDBACK</url>
    <connection-init-sqls>$INITSQLS</connection-init-sqls>
</backend-database>
EOF
fi

mkdir $WORK_DIR/artifactsdb
mkdir $WORK_DIR/datacagedb

cp $FLYS_SOURCE_DIR/artifacts/artifact-database/doc/schema-h2.sql $WORK_DIR/artifactsdb/artifacts-h2.sql
cp $FLYS_SOURCE_DIR/flys/flys-artifacts/doc/conf/datacage.sql $WORK_DIR/datacagedb/datacage.sql

echo "INFO: create h2 database for artifacts and datacage"
$SCRIPT_DIR/h2/createArtifacts.sh $WORK_DIR/artifactsdb/artifacts-h2.sql
$SCRIPT_DIR/h2/createDatacage.sh $WORK_DIR/datacagedb/datacage.sql

mv $WORK_DIR/artifactsdb $WORK_DIR/server/
mv $WORK_DIR/datacagedb $WORK_DIR/server/

echo "INFO: create tarball"
mkdir $WORK_DIR/flys-$VERSION
mv $WORK_DIR/server $WORK_DIR/client $WORK_DIR/flys-$VERSION
cd $WORK_DIR
tar cfz flys-$VERSION.tar.gz flys-$VERSION
echo "INFO: cleanup"
#rm -r $WORK_DIR/flys-$VERSION

echo "DONE: $WORK_DIR/flys-$VERSION.tar.gz"
