#!/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/framework"
HTTPCLIIENT_HG_REPO="http://wald.intevation.org/hg/dive4elements/http-client"
FLYS_HG_REPO="http://wald.intevation.org/hg/dive4elements/river"

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

ORACLE_HIBERNATE=$FLYS_SOURCE_DIR/hibernate-spatial-oracle-1.1.jar
ORACLE_JDBC=$FLYS_SOURCE_DIR/ojdbc5.jar

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 or default
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:-http://flys-devel.intevation.de}
WIKI_URL=${WIKI_URL:-https://flys3-devel.bafg.de/wiki}
# Prefix to FreeSans.ttf
FONT_PATH=${FONT_PATH:-/opt/flys}

# Seddb Configuration
SEDDBURL=${SEDDBURL:-czech-republic.atlas.intevation.de}
SEDDBPORT=${SEDDBPORT:-5432}
SEDDBBACK=${SEDDBBACK:-seddb}
SEDDBUSER=${SEDDBUSER:-flys}
SEDDBPASS=${SEDDBPASS:-flys}
SEDINITSQLS=${SEDINITSQLS:-}


# Backend configuration
BACKENDURL=${BACKENDURL:-czech-republic.atlas.intevation.de}
BACKENDPORT=${BACKENDPORT:-5432}
BACKENDBACK=${BACKENDBACK:-flys_devel}
BACKENDUSER=${BACKENDUSER:-flys_dami}
BACKENDPASS=${BACKENDPASS:-flys_dami}
INITSQLS=${INITSQLS:-}
DGM_PATH=${DGM_PATH:-/vol1/projects/Geospatial/flys-3.0/testdaten/dami_dgms/}
LOG_DIR=/var/log/flys

# Client configuration
AUTH=${AUTH:-GGinA}

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
if [ "$VERSION" = "default" ]; then
    ARTIFACT_PORT=${ARTIFACT_PORT:-29999}
else
    ARTIFACT_PORT=${ARTIFACT_PORT:-`echo 1$VERSION | sed 's/\.//g'`}
fi

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 framework
    hg clone $HTTPCLIIENT_HG_REPO http-client
    hg clone $FLYS_HG_REPO river
else
    echo "Updating sources / Reverting changes"
    cd $FLYS_SOURCE_DIR
    for repo in $REPOS; do
        cd $repo && hg pull && hg revert -a && hg up && 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
        OLD_REV=$(cat .hgtags | tail -1 | awk '{print $2}')
        hg tag $VERSION -m "Added tag $VERSION for changeset $CHANGESET"
        hg push ssh://$DEVELOPER@scm.wald.intevation.org/hg/dive4elements/$repo
        echo "Changelog for $repo" >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
        echo "#############################################################################" \
            >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
        hg log -r $VERSION:$OLD_REV --style changelog >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
        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"

#Needs to be done before the tomcat replacement below
sed -i -e "s@http://localhost:8080/d4e/flys/mapfish-print@https://localhost/flys-${VERSION}/flys/mapfish-print@g" \
    $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/web.xml

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/river/gwt-client/src/main/webapp/WEB-INF/web.xml

sed -i -e "s@<param-value>plain</param-value>@<param-value>$AUTH</param-value>@" \
    $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/web.xml

sed -i -e "s@https://flys3-devel.bafg.de/wiki@${WIKI_URL}@g" \
    $FLYS_SOURCE_DIR/river/gwt-client/src/main/java/org/dive4elements/river/client/client/config.xml

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

if [ -r "$FEATURES_XML" ]; then
    cp "$FEATURES_XML" $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/features.xml
fi

find $FLYS_SOURCE_DIR/river/ -name \*.properties -o -name \*.xsl |  \
    xargs sed -i "s@https://flys-intern.intevation.de@${WIKI_URL}@g";

if [ -r "$WMSSERVICES_XML" ]; then
    cp "$WMSSERVICES_XML" \
        $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/wms-services.xml
fi

if [ -r "$CONFIG_YAML" ]; then
    cp "$CONFIG_YAML" $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/config.yaml
fi

sed -i -e "s@/usr/share/fonts/truetype/freefont@${FONT_PATH}@g" \
    $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/config.yaml

if [ -r "$RIVERMAP_XML" ]; then
    cp "$RIVERMAP_XML" $FLYS_SOURCE_DIR/river/artifacts/doc/conf/rivermap.xml
fi

if [ -r "$FLOODMAP_XML" ]; then
    cp "$FLOODMAP_XML" $FLYS_SOURCE_DIR/river/artifacts/doc/conf/floodmap.xml
fi

if [ -r "$METADATA_XML" ]; then
    cp "$METADATA_XML" $FLYS_SOURCE_DIR/river/artifacts/doc/conf/meta-data.xml
fi

if [ -r "$MODULES_XML" ]; then
    cp "$MODULES_XML" $FLYS_SOURCE_DIR/river/artifacts/doc/conf/modules.xml
fi

if [ -r "$ZOOMSCALES_XML" ]; then
    cp "$ZOOMSCALES_XML" $FLYS_SOURCE_DIR/river/artifacts/doc/conf/zoom-scales.xml
fi

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/river/gwt-client/src/main/webapp/

echo "INFO: compile and build sources"
mvn -f $FLYS_SOURCE_DIR/framework/pom.xml clean compile package install
mvn -f $FLYS_SOURCE_DIR/river/backend/pom.xml clean compile package install
mvn -f $FLYS_SOURCE_DIR/river/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/river/gwt-client/pom.xml clean compile package


echo "INFO: copy dependencies and libs"
cp $FLYS_SOURCE_DIR/framework/artifact-database/target/artifact-database-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/framework/artifacts/target/artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/framework/artifacts-common/target/artifacts-common-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/river/backend/target/river-backend-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/river/artifacts/target/river-artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
cp $FLYS_SOURCE_DIR/river/gwt-client/target/gwt-client-1.0-SNAPSHOT.war $WORK_DIR/client/flys-${VERSION}.war
cp $FLYS_SOURCE_DIR/river/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}/libs/* $WORK_DIR/server/bin/lib/

echo "INFO: download WSPLGEN"
wget https://wald.intevation.org/frs/download.php/1496/wsplgen-linux-64bit-static.gz
gunzip wsplgen-linux-64bit-static.gz
echo "16d60047aa114d69e9435dbbe56b6981b4e1c445  wsplgen-linux-64bit-static" | \
    sha1sum -c
mv wsplgen-linux-64bit-static $WORK_DIR/server/bin/
chmod +x $WORK_DIR/server/bin/wsplgen-linux-64bit-static

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*
    if [ ! -f $ORACLE_JDBC ]; then
        echo "Could not find oracle jdbc in $ORACLE_JDBC"
        echo "Please make sure that the ORACLE_JDBC variable is set correctly"
        exit 1
    fi
    if [ ! -f $ORACLE_HIBERNATE ]; then
        echo "Could not find hibernate-spatial-oracle in $ORACLE_HIBERNATE"
        echo "Please make sure that the ORACLE_HIBERNATE variable is set correctly"
        exit 1
    fi
    cp $ORACLE_HIBERNATE $ORACLE_JDBC $WORK_DIR/server/bin/lib/
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/river/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/@${MAPSERVER_URL}/@g" \
    $WORK_DIR/server/conf/floodmap.xml

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

sed -i -e "s@/usr/share/fonts/truetype/freefont@${FONT_PATH}@g" \
    $WORK_DIR/server/conf/mapserver/fontset.txt

sed -i -e "s@/tmp/flys-rivers-wms.log@${LOG_DIR}/rivers-wms-${VERSION}.log@g" \
    $WORK_DIR/server/conf/mapserver/river-mapfile.vm

sed -i -e "s@/tmp/flys-user-wms.log@${LOG_DIR}/user-wms-${VERSION}.log@g" \
    $WORK_DIR/server/conf/mapserver/mapfile.vm

sed -i -e "s@/var/log/d4e-river/d4e-server.log@${LOG_DIR}/server-${VERSION}.log@" \
    $WORK_DIR/server/conf/log4j.properties

sed -i -e "s@<dgm-path>/path/to/rivers/</dgm-path>@<dgm-path>${DGM_PATH}</dgm-path>@g" \
       -e "s@<help-url>http://example.com</help-url>@<help-url>${WIKI_URL}</help-url>@g" \
    $WORK_DIR/server/conf/conf.xml


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

cat > $WORK_DIR/server/conf/seddb-db.xml << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<seddb-database>
    <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>
    <connection-init-sqls>$SEDINITSQLS</connection-init-sqls>
</seddb-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

cat > $WORK_DIR/server/conf/seddb-db.xml << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<seddb-database>
    <user>$SEDDBUSER</user>
    <password>$SEDDBPASS</password>
    <dialect>org.hibernate.dialect.PostgreSQLDialect</dialect>
    <driver>org.postgresql.Driver</driver>
    <url>jdbc:postgresql://$SEDDBURL:$SEDDBPORT/$SEDDBBACK</url>
</seddb-database>
EOF

fi

mkdir $WORK_DIR/artifactsdb
mkdir $WORK_DIR/datacagedb

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

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

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

echo "INFO: create tarball"
if [ -d "$WORK_DIR/flys-$VERSION" ]; then
    echo "INFO: removing old directory"
    rm -rf "$WORK_DIR/flys-$VERSION"
fi

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"
echo "Changelog: $WORK_DIR/changes_$OLD_REV-$VERSION.txt"
