teichmann@4484: #!/bin/bash
tom@8493: # Release script for Dive4Elements River
aheinecke@5251: #
aheinecke@5251: # Authors:
aheinecke@5251: # Andre Heinecke <aheinecke@intevation.de>
aheinecke@5251: #
tom@8493: # Copyright (C) 2011 - 2014 by Bundesanstalt für Gewässerkunde
tom@8493: # Software engineering by Intevation GmbH
aheinecke@5251: #
tom@8493: # This file is Free Software under the GNU AGPL (>=v3)
tom@8493: # and comes with ABSOLUTELY NO WARRANTY! Check out the
tom@8493: # documentation coming with Dive4Elements River for details.
ingo@4065: 
aheinecke@5220: set -e
andre@8080: LC_ALL=en_US.UTF-8
aheinecke@5251: DEFAULT_WD=/tmp/flys-release
aheinecke@5251: DEVELOPER=aheinecke
aheinecke@5220: 
tom@8499: ARTIFACTS_HG_REPO="https://wald.intevation.org/hg/dive4elements/framework"
tom@8499: HTTPCLIIENT_HG_REPO="https://wald.intevation.org/hg/dive4elements/http-client"
tom@8499: FLYS_HG_REPO="https://wald.intevation.org/hg/dive4elements/river"
ingo@4065: 
aheinecke@5900: REPOS="river http-client framework"
aheinecke@5251: # Do not use spaces in path
aheinecke@5251: FLYS_SOURCE_DIR=/local-vol1/aheinecke/flys-release
ingo@4065: 
aheinecke@5251: SCRIPT_DIR=$(readlink -f `dirname $0`)
aheinecke@5251: usage(){
aheinecke@5251:     cat << EOF
ingo@4065: 
aheinecke@5251: usage: $0 [options] VERSION
teichmann@4484: 
tom@8501: Create a D4E River package
aheinecke@5251: 
aheinecke@5251: OPTIONS:
tom@8494:    -?, --help          Show this message
tom@8494:    -w                  The working directory to use (do not use spaces in path)
tom@8494:                        Default: $DEFAULT_WD
tom@8494:    -t                  Tag the current default branch as "VERSION"
tom@8494:    -o, --oracle        Release is for oracle.
tom@8494:                        Suitable jar-files for JDBC driver and
tom@8494:                        hibernate-spatial have to be given with the
tom@8494:                        environment variables ORACLE_JDBC and ORACLE_HIBERNATE
aheinecke@5493:    VERSION must be in the format MAYOR.MINOR.PATCH or default
aheinecke@5251: EOF
aheinecke@5251: exit 0
aheinecke@5251: }
aheinecke@5251: #   --backend-db-url                Url of database backend. Default: $BACKENDURL
aheinecke@5251: #   --backend-db-pass               Backend db password. Default: $BACKENDPASS
aheinecke@5251: #   --backend-db-port               Backend db port. Default: $BACKENDPORT
aheinecke@5251: #   --backend-db-user               Backend db user. Default: $BACKENDUSER
aheinecke@5251: #   --backend-db-backend            Backend db backend name. Default: $BACKENDBACK
aheinecke@5251: #   --seddb-url                     Sediment db url. Default: $SEDDBURL
aheinecke@5251: #   --seddb-port                    Sediment db port. Default: $SEDDBPORT
aheinecke@5251: #   --seddb-user                    Sediment db user. Default: $SEDDBUSER
aheinecke@5251: #   --seddb-pass                    Sediment db password. Default: $SEDDBPASS
aheinecke@5251: #   --seddb-back                    Sediment db backend. Default: $SEDDBBACK
teichmann@4494: 
tom@8501: # customizable variables
rrenkert@7813: ELBE_MODEL_UUID=${ELBE_MODEL_UUID:-1a4825f6-925f-11e3-8165-001f29e71d12}
tom@7775: 
aheinecke@5251: OPTS=`getopt -o ?w:,t,o \
aheinecke@5251:      -l help,oracle \
aheinecke@5251:      -n $0 -- "$@"`
ingo@4065: 
aheinecke@5251: if [ $? != 0 ] ; then usage; fi
aheinecke@5251: eval set -- "$OPTS"
aheinecke@5251: while true ; do
aheinecke@5251:   case "$1" in
aheinecke@5251:     "-?"|"--help")
aheinecke@5251:       usage;;
aheinecke@5251:     "--")
aheinecke@5251:       shift
aheinecke@5251:       break;;
aheinecke@5251:     "-w")
aheinecke@5251:       WORK_DIR=$2
aheinecke@5251:       shift 2;;
aheinecke@5251:     "-o"|"--oracle")
aheinecke@5251:       BUILD_ORACLE="TRUE"
aheinecke@5251:       shift;;
aheinecke@5251:     "-t")
aheinecke@5251:       DO_TAG="TRUE"
aheinecke@5251:       shift;;
aheinecke@5251:     *)
aheinecke@5251:       echo "Unknown Option $1"
aheinecke@5251:       usage;;
aheinecke@5251:   esac
aheinecke@5251: done
teichmann@4484: 
aheinecke@5251: if [ $# != 1 ]; then
aheinecke@5251:     usage
aheinecke@5251: fi
aheinecke@5251: 
aheinecke@5251: VERSION=$1
aheinecke@5251: 
aheinecke@5251: if [ -z $WORK_DIR ]; then
aheinecke@5251:   WORK_DIR=$DEFAULT_WD
aheinecke@5251: fi
aheinecke@5251: 
aheinecke@5251: mkdir -p $WORK_DIR
aheinecke@5251: 
aheinecke@5251: if [ ! -d $FLYS_SOURCE_DIR ]; then
aheinecke@5251:     mkdir -p $FLYS_SOURCE_DIR
aheinecke@5251:     echo "Cloning sources"
aheinecke@5251:     cd $FLYS_SOURCE_DIR
aheinecke@5900:     hg clone $ARTIFACTS_HG_REPO framework
aheinecke@5251:     hg clone $HTTPCLIIENT_HG_REPO http-client
aheinecke@5900:     hg clone $FLYS_HG_REPO river
aheinecke@5251: else
aheinecke@5251:     echo "Updating sources / Reverting changes"
aheinecke@5251:     cd $FLYS_SOURCE_DIR
aheinecke@5251:     for repo in $REPOS; do
aheinecke@5383:         cd $repo && hg pull && hg revert -a && hg up && cd $FLYS_SOURCE_DIR
aheinecke@5251:     done;
aheinecke@5251: fi
aheinecke@5251: 
aheinecke@5251: if [ "$DO_TAG" = "TRUE" ]; then
aheinecke@5251:     echo "Tagging version $VERSION"
aheinecke@5251:     for repo in $REPOS; do
aheinecke@5251:         cd $repo
aheinecke@5251:         CHANGESET=$(hg log -l1 |head -1 | awk -F: '{print $3}')
aheinecke@5251:         echo ""
aheinecke@5251:         echo "Do you really want to tag $repo rev: $CHANGESET as Version $VERSION?"
aheinecke@5251:         echo "press enter to continue or CTRL+C to abort."
aheinecke@5251:         echo ""
aheinecke@5251:         hg log -l1
aheinecke@5251:         read
aheinecke@5320:         OLD_REV=$(cat .hgtags | tail -1 | awk '{print $2}')
aheinecke@5251:         hg tag $VERSION -m "Added tag $VERSION for changeset $CHANGESET"
aheinecke@5251:         hg push ssh://$DEVELOPER@scm.wald.intevation.org/hg/dive4elements/$repo
aheinecke@5320:         echo "Changelog for $repo" >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
aheinecke@5320:         echo "#############################################################################" \
aheinecke@5320:             >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
aheinecke@5320:         hg log -r $VERSION:$OLD_REV --style changelog >> $WORK_DIR/changes_$OLD_REV-$VERSION.txt
aheinecke@5251:         cd $FLYS_SOURCE_DIR
aheinecke@5251:     done;
aheinecke@5251: fi
aheinecke@5251: 
aheinecke@5251: # Update to current version
aheinecke@5251: for repo in $REPOS; do
aheinecke@5251:     cd $repo
tom@7996:     if [ -z "`hg tags | sed -n "/$VERSION/p"`" -a -z "`hg branches | sed -n "/$VERSION/p"`" ]
tom@7996:     then
tom@8501:         echo "WARNING: $VERSION not found in $repo! Updating to default."
tom@7996:         hg up default
tom@7996:     else
tom@7996:         hg up "$VERSION"
tom@7996:     fi
aheinecke@5251:     cd $FLYS_SOURCE_DIR
aheinecke@5251: done
aheinecke@5251: 
aheinecke@5251: rm -rf "$WORK_DIR/server" "$WORK_DIR/client"
aheinecke@5251: cd $WORK_DIR
aheinecke@5251: mkdir -p "$WORK_DIR/server/bin/lib/own"
aheinecke@5251: mkdir "$WORK_DIR/server/shapefiles"
aheinecke@5251: mkdir "$WORK_DIR/client"
aheinecke@5251: 
tom@8501: 
tom@8501: echo "INFO: Preparing configuration of web client"
aheinecke@5692: 
aheinecke@5692: #Needs to be done before the tomcat replacement below
rrenkert@7813: sed -i -e "s@1a4825f6-925f-11e3-8165-001f29e71d12@${ELBE_MODEL_UUID}@g" \
rrenkert@7813:     $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/images/FLYS_Karte_interactive.html
rrenkert@7813: 
tom@8501: if [ -d $WEBINF ]; then
tom@8501:     echo "INFO: copy custom client configuration to target destination"
tom@8501:     WEBINF_DIR=$FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/WEB-INF/
tom@8501:     cp -R $WEBINF/* $WEBINF_DIR
tom@8501:     for file in `find $WEBINF_DIR/ -type f`; do
tom@8501:         sed -i -e "s@D4E_VERSION@${VERSION}@g" $file
tom@8501:     done
tom@7801: fi
tom@7801: 
tom@8384: 
aheinecke@5251: if [ ! -f $FLYS_SOURCE_DIR/OpenLayers-2.11.tar.gz ]; then
aheinecke@5251:     echo "INFO: download OpenLayers-2.11 for client"
aheinecke@5251:     cd $FLYS_SOURCE_DIR
aheinecke@5251:     curl -O http://openlayers.org/download/OpenLayers-2.11.tar.gz
aheinecke@5251:     tar xvfz OpenLayers-2.11.tar.gz
aheinecke@5251:     # TODO: Remove more superfluous OpenLayers stuff.
aheinecke@5251:     rm -rf OpenLayers-2.11/doc
aheinecke@5251:     rm -rf OpenLayers-2.11/tests
aheinecke@5251:     rm -rf OpenLayers-2.11/examples
aheinecke@5251:     cd $WORK_DIR
aheinecke@5251: fi
aheinecke@5900: cp -r $FLYS_SOURCE_DIR/OpenLayers-2.11 $FLYS_SOURCE_DIR/river/gwt-client/src/main/webapp/
ingo@4065: 
rrenkert@7939: echo "INFO: Downloading smartGWT"
tom@8245: mvn com.isomorphic:isc-maven-plugin:install -Dproduct=SMARTGWT -Dlicense=LGPL \
tom@8500:     -DbuildNumber=4.1p -DbuildDate=2014-12-13 -Dworkdir=$WORK_DIR
rrenkert@7939: 
ingo@4065: echo "INFO: compile and build sources"
aheinecke@5900: mvn -f $FLYS_SOURCE_DIR/framework/pom.xml clean compile package install
aheinecke@5900: mvn -f $FLYS_SOURCE_DIR/river/backend/pom.xml clean compile package install
aheinecke@5900: mvn -f $FLYS_SOURCE_DIR/river/artifacts/pom.xml clean compile package dependency:copy-dependencies install
aheinecke@5251: mvn -f $FLYS_SOURCE_DIR/http-client/pom.xml clean compile package install
aheinecke@5900: mvn -f $FLYS_SOURCE_DIR/river/gwt-client/pom.xml clean compile package
ingo@4065: 
tom@7951: # remove unused Version of JFreeChart (it's a dependency of JasperReports
tom@7951: # that prevents diagram generation in D4E river)
tom@7951: rm $FLYS_SOURCE_DIR/river/artifacts/target/dependency/jfreechart-1.0.12.jar
aheinecke@5251: 
ingo@4065: echo "INFO: copy dependencies and libs"
aheinecke@5900: cp $FLYS_SOURCE_DIR/framework/artifact-database/target/artifact-database-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
aheinecke@5900: cp $FLYS_SOURCE_DIR/framework/artifacts/target/artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
aheinecke@5900: cp $FLYS_SOURCE_DIR/framework/artifacts-common/target/artifacts-common-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
aheinecke@5900: cp $FLYS_SOURCE_DIR/river/backend/target/river-backend-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
aheinecke@5900: cp $FLYS_SOURCE_DIR/river/artifacts/target/river-artifacts-1.0-SNAPSHOT.jar $WORK_DIR/server/bin/lib/own/
aheinecke@5900: cp $FLYS_SOURCE_DIR/river/gwt-client/target/gwt-client-1.0-SNAPSHOT.war $WORK_DIR/client/flys-${VERSION}.war
aheinecke@5900: cp $FLYS_SOURCE_DIR/river/artifacts/target/dependency/* $WORK_DIR/server/bin/lib/
ingo@4065: 
ingo@4065: echo "INFO: copy scripts and libraries to target destination"
aheinecke@5251: cp ${SCRIPT_DIR}/bin/run.sh $WORK_DIR/server/bin/
aheinecke@5251: cp ${SCRIPT_DIR}/libs/* $WORK_DIR/server/bin/lib/
ingo@4065: 
tom@7797: echo "INFO: download WSPLGEN"
tom@7797: wget https://wald.intevation.org/frs/download.php/1496/wsplgen-linux-64bit-static.gz
tom@7797: gunzip wsplgen-linux-64bit-static.gz
tom@7797: echo "16d60047aa114d69e9435dbbe56b6981b4e1c445  wsplgen-linux-64bit-static" | \
tom@7797:     sha1sum -c
tom@7816: mv wsplgen-linux-64bit-static $WORK_DIR/server/bin/wsplgen
tom@7816: chmod +x $WORK_DIR/server/bin/wsplgen
ingo@4065: 
aheinecke@5251: if [ "$BUILD_ORACLE" = "TRUE" ]; then
aheinecke@5251:     echo "INFO: remove PostgreSQL and PostGIS libraries"
aheinecke@5251:     rm $WORK_DIR/server/bin/lib/postg*
aheinecke@5251:     rm $WORK_DIR/server/bin/lib/hibernate-spatial-postgis*
aheinecke@5320:     if [ ! -f $ORACLE_JDBC ]; then
aheinecke@5320:         echo "Could not find oracle jdbc in $ORACLE_JDBC"
aheinecke@5320:         echo "Please make sure that the ORACLE_JDBC variable is set correctly"
aheinecke@5320:         exit 1
aheinecke@5320:     fi
aheinecke@5320:     if [ ! -f $ORACLE_HIBERNATE ]; then
aheinecke@5320:         echo "Could not find hibernate-spatial-oracle in $ORACLE_HIBERNATE"
aheinecke@5320:         echo "Please make sure that the ORACLE_HIBERNATE variable is set correctly"
aheinecke@5320:         exit 1
aheinecke@5320:     fi
aheinecke@5320:     cp $ORACLE_HIBERNATE $ORACLE_JDBC $WORK_DIR/server/bin/lib/
aheinecke@5251: else
tom@8501:     echo "INFO: remove Oracle libraries"
aheinecke@5251:     rm -f $WORK_DIR/server/bin/lib/hibernate-spatial-oracle-1.1.jar
aheinecke@5251:     rm -f $WORK_DIR/server/bin/lib/ojdbc*
aheinecke@5251: fi
ingo@4065: 
tom@8495: 
tom@8501: echo "INFO: copy default server configuration to target destination"
aheinecke@5900: cp -R $FLYS_SOURCE_DIR/river/artifacts/doc/conf $WORK_DIR/server/
teichmann@4484: 
tom@8495: if [ -n $DGM_PATH ]; then
tom@8495:     sed -i -e "s@<dgm-path>/path/to/rivers/</dgm-path>@<dgm-path>${DGM_PATH}</dgm-path>@g" \
tom@8495:         $WORK_DIR/server/conf/conf.xml
tom@8495: fi
aheinecke@5530: 
tom@8495: if [ -n $WIKI_URL ]; then
tom@8495:     sed -i -e "s@<help-url>http://example.com</help-url>@<help-url>${WIKI_URL}</help-url>@g" \
tom@8495:         $WORK_DIR/server/conf/conf.xml
tom@8495: fi
aheinecke@5268: 
tom@8495: if [ -d $SERVER_CONF ]; then
tom@8501:     echo "INFO: copy custom server configuration to target destination"
tom@8495:     cp -R $SERVER_CONF/* $WORK_DIR/server/conf/
tom@8495:     for file in `find $WORK_DIR/server/conf -type f`; do
tom@8495:         sed -i -e "s@D4E_VERSION@${VERSION}@g" $file
tom@8495:     done
tom@8495: fi
aheinecke@5602: 
aheinecke@5251: 
tom@8495: echo "INFO: create h2 database for artifacts and datacage"
aheinecke@5251: mkdir $WORK_DIR/artifactsdb
aheinecke@5251: mkdir $WORK_DIR/datacagedb
aheinecke@5251: 
aheinecke@5900: cp $FLYS_SOURCE_DIR/framework/artifact-database/doc/schema-h2.sql $WORK_DIR/artifactsdb/artifacts-h2.sql
aheinecke@5900: cp $FLYS_SOURCE_DIR/river/artifacts/doc/conf/datacage.sql $WORK_DIR/datacagedb/datacage.sql
ingo@4065: 
aheinecke@7771: cd $WORK_DIR
aheinecke@5251: $SCRIPT_DIR/h2/createArtifacts.sh $WORK_DIR/artifactsdb/artifacts-h2.sql
aheinecke@5251: $SCRIPT_DIR/h2/createDatacage.sh $WORK_DIR/datacagedb/datacage.sql
aheinecke@7771: cd -
ingo@4065: 
aheinecke@5251: mv $WORK_DIR/artifactsdb $WORK_DIR/server/
aheinecke@5251: mv $WORK_DIR/datacagedb $WORK_DIR/server/
ingo@4065: 
tom@8495: if [ -f $INSTALL ]; then
tom@8464:     echo "INFO: prepare custom installation script"
tom@8464:     cp $INSTALL $WORK_DIR
tom@8495:     sed -i "s/D4E_VERSION/$VERSION/g" $WORK_DIR/`basename $INSTALL`
tom@8464: fi
tom@8464: 
ingo@4065: echo "INFO: create tarball"
aheinecke@6612: if [ -d "$WORK_DIR/flys-$VERSION" ]; then
aheinecke@6612:     echo "INFO: removing old directory"
aheinecke@6612:     rm -rf "$WORK_DIR/flys-$VERSION"
aheinecke@6612: fi
aheinecke@6612: 
aheinecke@5251: mkdir $WORK_DIR/flys-$VERSION
aheinecke@5251: mv $WORK_DIR/server $WORK_DIR/client $WORK_DIR/flys-$VERSION
tom@8464: if [ -n $INSTALL ]; then
tom@8464:     mv $WORK_DIR/`basename $INSTALL` $WORK_DIR/flys-$VERSION
tom@8464: fi
aheinecke@5251: cd $WORK_DIR
aheinecke@5251: tar cfz flys-$VERSION.tar.gz flys-$VERSION
tom@7948: #echo "INFO: cleanup"
aheinecke@5251: #rm -r $WORK_DIR/flys-$VERSION
ingo@4065: 
aheinecke@5251: echo "DONE: $WORK_DIR/flys-$VERSION.tar.gz"
aheinecke@5320: echo "Changelog: $WORK_DIR/changes_$OLD_REV-$VERSION.txt"