#!/bin/sh

#
# Script used for deploying DM webapp
# Deployment configuration can be set in etc/$DM_DB_NAME.deploy.conf file
#
# Usage:
#
# $0 [DM_DB_NAME]
#

MY_DIR=`dirname $0` && cd $MY_DIR && MY_DIR=`pwd`
if [ -z "${DM_ROOT_DIR}" ]; then
    DM_ROOT_DIR=$MY_DIR/..
fi
DM_ENV_FILE=${DM_ROOT_DIR}/setup.sh
if [ ! -f ${DM_ENV_FILE} ]; then
    echo "Environment file ${DM_ENV_FILE} does not exist." 
    exit 2
fi
. ${DM_ENV_FILE} > /dev/null

# Use first argument as db name, if provided
DM_DB_NAME=${DM_DB_NAME:=dm}
if [ ! -z "$1" ]; then
    DM_DB_NAME=$1
fi
echo "Using DB name: $DM_DB_NAME"

# Look for deployment file in etc directory, and use it to override
# default entries
deployConfigFile=$DM_ROOT_DIR/etc/${DM_DB_NAME}.deploy.conf
if [ -f $deployConfigFile ]; then
    echo "Using deployment config file: $deployConfigFile"
    . $deployConfigFile
else
    echo "Deployment config file $deployConfigFile not found, using defaults"
fi

DM_HOST_ARCH=`uname | tr [A-Z] [a-z]`-`uname -m`
DM_CONTEXT_ROOT=${DM_CONTEXT_ROOT:=dm}
GLASSFISH_DIR=$DM_SUPPORT_DIR/glassfish/$DM_HOST_ARCH
DM_DEPLOY_DIR=$GLASSFISH_DIR/glassfish/domains/domain1/autodeploy
DM_DIST_DIR=$DM_ROOT_DIR/src/java/DmWebPortal/dist
DM_BUILD_WAR_FILE=DmWebPortal.war
DM_WAR_FILE=$DM_CONTEXT_ROOT.war
JAVA_HOME=$DM_SUPPORT_DIR/java/$DM_HOST_ARCH

if [ ! -f $DM_DIST_DIR/$DM_BUILD_WAR_FILE ]; then
    echo "$DM_BUILD_WAR_FILE not found in $DM_DIST_DIR."
    exit 1
fi

# Modify war file for proper context/persistence settings and
# repackage it into new war 
echo "Repackaging war file for context root $DM_CONTEXT_ROOT"
cd $DM_DIST_DIR
rm -rf $DM_CONTEXT_ROOT
mkdir -p $DM_CONTEXT_ROOT
cd $DM_CONTEXT_ROOT
jar xf ../$DM_BUILD_WAR_FILE

configFile=WEB-INF/glassfish-web.xml
cmd="cat $configFile | sed 's?<context-root.*?<context-root>${DM_CONTEXT_ROOT}</context-root>?g' > $configFile.2 && mv $configFile.2 $configFile"
eval $cmd

configFile=WEB-INF/classes/META-INF/persistence.xml
cmd="cat $configFile | sed 's?<jta-data-source.*?<jta-data-source>${DM_DB_NAME}_DataSource</jta-data-source>?g' > $configFile.2 && mv $configFile.2 $configFile"
eval $cmd

configFile=WEB-INF/classes/dm.portal.properties
cmd="cat $configFile | sed 's?dm.dsWebService.url=.*?dm.dsWebService.url=https://${DM_DS_WEB_SERVICE_HOST}:${DM_DS_WEB_SERVICE_PORT}/dm?g' > $configFile.2 && mv $configFile.2 $configFile"
eval $cmd
> 
configFile=WEB-INF/classes/resources.properties
cmd="cat $configFile | sed 's?DmSoftwareVersion=.*?DmSoftwareVersion=${DM_SOFTWARE_VERSION}?g' | sed 's?DM_DATE?$DM_DATE?g' > $configFile.2 && mv $configFile.2 $configFile"
eval $cmd

jar cf ../$DM_WAR_FILE *

export AS_JAVA=$JAVA_HOME
ASADMIN_CMD=$GLASSFISH_DIR/bin/asadmin

# copy war file
echo "Copying war file $DM_DIST_DIR/$DM_WAR_FILE to $DM_DEPLOY_DIR"
rm -f $DM_DEPLOY_DIR/${DM_WAR_FILE}_*
cp $DM_DIST_DIR/$DM_WAR_FILE $DM_DEPLOY_DIR

# wait on deployment
echo "Waiting on war deployment..."
WAIT_TIME=60
cd $DM_DEPLOY_DIR
t=0
while [ $t -lt $WAIT_TIME ]; do
    sleep 1
    deploymentStatus=`ls -c1 ${DM_WAR_FILE}_* 2> /dev/null | sed 's?.*war_??g'`
    if [ ! -z "$deploymentStatus" ]; then
        break
    fi
    t=`expr $t + 1`
done
echo "Deployment Status: $deploymentStatus"


