#!/usr/bin/env bash
FNAME='[\E[37;44m'"\033[1mEITS SOFTWARE UPDATER\033[0m]"


if [ "update" = "$1" ];then
    SVN_UPDATE=1
    echo -e $FNAME " All projects will be updated from version control"
else
    SVN_UPDATE=0
fi

EITS_APP_HOME=/Applications/EITS


# Build and Install VARS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# VARS Needs to be installed into 2 places. The web start apps go on a 
# webserver. A standalone app is also bilt so that it can be called by
# a job scheduler.
echo -e $FNAME " Building VARS"
VARS_HOME=$HOME/workspace/vars
VARS_WEBSTART_HOME=/Library/WebServer/Documents/webstart
VARS_APP=VARS-Standalone
VARS_DEPLOY=$EITS_APP_HOME/$VARS_APP

# Update VARS source and build VARS
cd $VARS_HOME
if [ "$SVN_UPDATE" = "1" ];then
    svn update
fi
mvn clean install -Denvironment=eits -Dmaven.test.skip=true

# Install the standalone app for scripting
echo -e $FNAME " Installing VARS to " $VARS_DEPLOY
cp $VARS_HOME/vars-deploy/vars-app/target/*.zip $EITS_APP_HOME
cd $EITS_APP_HOME
rm -rf $VARS_APP
unzip vars*.zip -d $VARS_APP
rm vars*.zip
cd $VARS_DEPLOY
rm -rf database
cd $VARS_DEPLOY/bin
chmod u+x explode_config
explode_config

# Install the web start apps
echo -e $FNAME " Installing VARS Web Start Apps to " $VARS_WEBSTART_HOME
cp -r $VARS_HOME/vars-deploy/vars-jnlp/target/site/* $VARS_WEBSTART_HOME

# Cleanup
cd $VARS_HOME
mvn clean


# Build and Install AVED-DB ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The AVEDDB libraries are built and installed into the Maven repository 
# only. EITS-DB build will grab this jar and use it. We really don't always
# need to do this.
#
echo -e $FNAME " Building AVED-DB"
AVEDDB_HOME=$HOME/workspace/eits/aved-db
cd $AVEDDB_HOME
if [ "$SVN_UPDATE" = "1" ];then
    svn update
fi
mvn clean install -Dmaven.test.skip=true

# Cleanup
mvn clean


# Build and Install EITS-EAL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The EITS-EAL libraries are bult and installed into the Maven repository 
# only. EITS-DB build will grab this jar and use it. We really don't always
# need to do this.
#
echo -e $FNAME " Building EITS-EAL"
EITSEAL_HOME=$HOME/workspace/eits/eits-eal
cd $EITSEAL_HOME
if [ "$SVN_UPDATE" = "1" ];then
    svn update
fi
mvn clean install -Dmaven.test.skip=true -P eits

# Cleanup
mvn clean


# Build and install EITS-DB ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# EITS-DB runs support scripts, such as AVED data loaders and anxillary data
# mergin, used for working with EITS data. It will be built using the 
# AVED and VARS libraries available in the maven repository
echo -e $FNAME "Building EITS-DB"
EITSDB_HOME=$HOME/workspace/eits/eits-db
EITSDB_APP=EITS-Standalone
EITSDB_DEPLOY=$EITS_APP_HOME/$EITSDB_APP
cd $EITSDB_HOME
if [ "$SVN_UPDATE" = "1" ];then
    svn update
fi

ant clean init

echo -e $FNAME " Installing EITS-DB to " $EITSDB_DEPLOY
rm -rf $EITSDB_DEPLOY
mkdir $EITSDB_DEPLOY
cp -rf * $EITSDB_DEPLOY


