#!/bin/bash

if [ $EUID -eq 0 ]; then
   echo "This script must not be run as root." 1>&2
   exit 1
fi

GLASSFISH_HOME=/opt/glassfish
ADMIN_TOOL=$GLASSFISH_HOME/bin/asadmin
DEPLOYMENR_HOME=/tmp
dbpasswd=`/opt/phoenix/bin/phLicenseTool --showDatabasePassword`;
notfound=0
if [ "$dbpasswd" != "" ] ; then
   echo "get db password"
   echo "AS_ADMIN_PASSWORD="$dbpasswd > $DEPLOYMENR_HOME/glassfish-pwd.txt
else
   echo " cannot get db password"
   notfound=1
   /bin/cp -f /opt/glassfish/domains/domain1/config/admin-keyfile /opt/glassfish/domains/domain1/config/admin-keyfile.orig
   /bin/cp -f /opt/phoenix/deployment/jumpbox/gf_admin-keyfile /opt/glassfish/domains/domain1/config/admin-keyfile
   echo "AS_ADMIN_PASSWORD=changeit" > $DEPLOYMENR_HOME/glassfish-pwd.txt
   $ADMIN_TOOL stop-domain domain1
   $ADMIN_TOOL start-domain domain1
fi;


# white label backup
BACKUP_DIR=/tmp/wlbackup
WL_DIR=/opt/glassfish/domains/domain1/applications/phoenix/phoenix-web-1.0_war ;
WL_BKUP_IMAGES=/tmp/wlbackup/images ;
WL_BKUP_HEADER=/tmp/wlbackup/header ;

if [ ! -d ${WL_BKUP_IMAGES} ] ; then
        /bin/mkdir -p ${WL_BKUP_IMAGES} ;
fi ;

if [ ! -d ${WL_BKUP_HEADER} ] ; then
        /bin/mkdir -p ${WL_BKUP_HEADER} ;
fi ;

/bin/cp -f ${WL_DIR}/images/wl_favicon.ico ${WL_BKUP_IMAGES} &>/dev/null ;
/bin/cp -f ${WL_DIR}/images/wl_login.png ${WL_BKUP_IMAGES} &>/dev/null ;
/bin/cp -f ${WL_DIR}/images/wl_upload.png ${WL_BKUP_IMAGES} &>/dev/null ;
/bin/cp -f ${WL_DIR}/images/login.png ${WL_BKUP_IMAGES} &>/dev/null ;
/bin/cp -f ${WL_DIR}/images/upload.png ${WL_BKUP_IMAGES} &>/dev/null ;
/bin/cp -f ${WL_DIR}/resources/header/login.png ${WL_BKUP_HEADER} &>/dev/null ;
/bin/cp -f ${WL_DIR}/resources/header/wl_login.png ${WL_BKUP_HEADER} &>/dev/null ;
/bin/cp -f ${WL_DIR}/resources/header/logo.png ${WL_BKUP_HEADER} &>/dev/null ;
/bin/cp -f ${WL_DIR}/favicon.ico ${BACKUP_DIR} &>/dev/null ;
/bin/cp -f /opt/phoenix/config/companylogo.svg ${BACKUP_DIR} &>/dev/null ;
/bin/cp -f /opt/phoenix/config/phoenix_config.txt ${BACKUP_DIR} &>/dev/null ;

echo Undeploy current phoenix application
$ADMIN_TOOL --user admin --passwordfile $DEPLOYMENR_HOME/glassfish-pwd.txt undeploy phoenix 
if [ $? == 0 ] ;then
   echo "Updeploy $1 successfully done"
fi;


if [ -d /opt/glassfish/domains/domain1/applications/phoenix ] ; then
  echo "Remove previous phoenix.";
  rm -rf /opt/glassfish/domains/domain1/applications/phoenix;
fi;

echo Stop server ...
$ADMIN_TOOL stop-domain domain1
echo Clean session persistent
#rm -rf $GLASSFISH_HOME/domains/domain1/session-store/ejb/j2ee-apps/phoenix_*
rm -rf /opt/glassfish/domains/domain1/generated
rm -rf $GLASSFISH_HOME/domains/domain1/autodeploy/phoenix*
echo Start server ...
$ADMIN_TOOL start-domain domain1
echo Deploy $1
$ADMIN_TOOL --user admin --passwordfile $DEPLOYMENR_HOME/glassfish-pwd.txt deploy --name phoenix $1
if [ $? == 0 ] ;then
   echo "Deploy $1 successfully done."
   rm -f $DEPLOYMENR_HOME/glassfish-pwd.txt
fi;

if [ $notfound -eq 1 ]; then
  /bin/cp -f /opt/glassfish/domains/domain1/config/admin-keyfile.orig /opt/glassfish/domains/domain1/config/admin-keyfile
  $ADMIN_TOOL stop-domain domain1
  echo Clean session persistent
  rm -rf /opt/glassfish/domains/domain1/generated
  $ADMIN_TOOL start-domain domain1
fi
# white label restore
/bin/cp -f ${WL_BKUP_IMAGES}/wl_favicon.ico ${WL_DIR}/images/wl_favicon.ico &>/dev/null ;
/bin/cp -f ${WL_BKUP_IMAGES}/wl_login.png ${WL_DIR}/images/wl_login.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_IMAGES}/wl_upload.png ${WL_DIR}/images/wl_upload.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_IMAGES}/login.png ${WL_DIR}/images/login.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_IMAGES}/upload.png ${WL_DIR}/images/upload.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_HEADER}/login.png ${WL_DIR}/resources/header/login.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_HEADER}/wl_login.png ${WL_DIR}/resources/header/wl_login.png &>/dev/null ;
/bin/cp -f ${WL_BKUP_HEADER}/logo.png ${WL_DIR}/resources/header/logo.png &>/dev/null ;
/bin/cp -f ${BACKUP_DIR}/favicon.ico  ${WL_DIR}/favicon.ico &>/dev/null 
/bin/cp -f ${BACKUP_DIR}/companylogo.svg /opt/phoenix/config/companylogo.svg &>/dev/null ;

PH_CONFIG=/opt/phoenix/config/phoenix_config.txt ;
if grep -q 'WL\=1' ${BACKUP_DIR}/phoenix_config.txt; then
  echo $"" >> $PH_CONFIG ;
  echo [GLOBAL] >> $PH_CONFIG ;
  echo WL=1 >> $PH_CONFIG ;
  echo [END] >> $PH_CONFIG ;
fi

echo new phoenix application is ready at $(date).

