#!/bin/bash
#
#########################################################################################
# Script: 		restore_current_environment.sh
# Description: 	Restore selected environment from latest backup 
# Author: 		ABS Laundry Business Solutions
# Version: 		9.08.00
#### Changelog of script ################################################################
# Date        Author    Version 	Change				                             
# 19-jun-2011 JMI		1.3     	Initial setup
#									Binaries from repository/current will be included in incremental backup
# 20-Mar-2024 RAY       6.0         Support for Glassfish4, Payara5, Tomee8, Tomee91 and Tomcat90
# 03-Apr-2024 RAY       9.08.00		Renamed version
# 25-Apr-2024 RAY       9.08.00     Added Support for Tomcat9 - ABS-164468
# 01-Jul-2024 RAY       9.08.01     Added Support for Tomcat9 and Tomcat10 - ABS-165658
# 21-Aug-2024 RAY       9.08.01     TI changes for replacing TomEE with Tomcat in 9.08.00 - ABS-167946
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Java21 for ABSSolute 9.09.00 - ABS-170835
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Plugins folder - INSTALLER-176
# 04-Nov-2024 RAY		9.09.00 	Absmenu update supporting Application Server new Classpath location - ABS-170842
# 01-Jun-2025 RAY       9.09.02         ABSSolute Server - Standardization & hot redeploy - TS-839
###################################################################################

restore_abs_component()
{
    case "$1" in
        application)    ABSAPPTYPE="Server";;
        report)         ABSAPPTYPE="Reportserver";;
        web)            ABSAPPTYPE="Web";;
        license)        ABSAPPTYPE="LICENSE";;
        customreports)  ABSAPPTYPE="CUSTOMREPORTS";;
        fonts)          ABSAPPTYPE="FONTS";;
		certificates)   ABSAPPTYPE="CERTIFICATES";;
    esac
    echo "Restore binaries for $1 server instance"
    export ABSMESSAGE="     Restore binaries for $1 server instance"; absmenu_log
    if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
    then
        rsync -tav --delete -e "ssh -q" $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup/$ABSAPPTYPE $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future
    else
        ssh -n -q $ABSMENUBACKUPSERVER "rsync -tav -e \"ssh -q\" --delete $ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup/$ABSAPPTYPE $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future"
    fi
}

ABSMENU_CURRENTDATEANDTIME=$(date +%Y_%b%d_%H%M%S)
ABSMENU_CURRENTBACKUPLOCATION=$ABSMENU_CURRENT_ENV_LICCODE"_"$ABSMENU_CURRENT_ENV_VERSION"_"$ABSMENU_CURRENTDATEANDTIME

export ABSMESSAGE="Restore $ABSMENUBACKUPSERVER:$ABSMENUBACKUPLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/last_backup to $ABSMENUREPOSITORYSERVER:$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future"
absmenu_log

# check if repository future locations for environment exists. If not then create 
if [ "$ABS_ROOTSERVER" == "$ABSMENUBACKUPSERVER" ] 
then
    [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future" ] && mkdir -p $ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future
else
    ssh -q $ABSMENUREPOSITORYSERVER "if [ ! -d "$ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future" ]; then mkdir -p $ABSMENUREPOSITORYLOCATION/$ABSMENU_CURRENT_ENVIRONMENT/Future; fi;"
fi
                                            
# restore app, rep and web server instance binaries from last backup to repository/Future
restore_abs_component application
restore_abs_component report
restore_abs_component web
restore_abs_component license
restore_abs_component customreports
restore_abs_component fonts
restore_abs_component certificates