#!/bin/bash
# Copyright (c) 2025 Broadcom Inc. and its subsidiaries. All Rights Reserved.
# -----------------------------------------------------------------------------
# LAYER 7 TECHNOLOGIES
# November 2008
#
# Launches the SSG Restore Utility
# -----------------------------------------------------------------------------
#

# Saves current directory as base dir for all relative paths.
REL_BASE_DIR=`pwd`

# Assumes this script file is in the SSG_HOME/config/backup and deduce SSG_HOME from there.
pushd `dirname $0` > /dev/null
RESTORE_HOME=`pwd`
cd ../..
SSG_HOME=`pwd`
popd > /dev/null

#Set environment variables

if [ -z "${SSGNODE}" ] ; then
    SSGNODE="default"
fi

GATEWAY_PID="${SSG_HOME}/node/${SSGNODE}/var/ssg.pid"

# Helper functions
pid_exited() {
  if [ -f "${GATEWAY_PID}" ]  && [ -d "/proc/$(< ${GATEWAY_PID})" ] ; then
    return 1;
  else
    return 0;
  fi
}

# Checks for the gateway pid file.
checkRunningLocalGateway() {
    if [ ! -z "${GATEWAY_PID}" ]; then
        if ! pid_exited ; then
            echo "Local gateway may be running.  Please ensure the local gateway is not running before proceeding."
            exit 33
        fi
    fi
}

#Backup script is removing read permissions for groups and others, hence revert these folders back to their default permissions
restoreFolderPermissions() {
    if [ -d "${SSG_HOME}/runtime/modules/lib/" ] ; then
        chmod -R 444 ${SSG_HOME}/runtime/modules/lib/*
    fi
    if [ -d "${SSG_HOME}/runtime/modules/assertions/" ] ; then 
        chmod -R 444 ${SSG_HOME}/runtime/modules/assertions/*
    fi
    if [ -d "${SSG_HOME}/config/backup/cfg/configfiles/" ] ; then    
        chmod -R go+r ${SSG_HOME}/config/backup/cfg/configfiles/*
    fi
    if [ -d "${SSG_HOME}/runtime/lib/ext/" ] ; then
        chmod -R 444 ${SSG_HOME}/runtime/lib/ext/*
    fi
    if [ -d "${SSG_HOME}/node/default/etc/conf/" ] ; then
        chmod -R 640 ${SSG_HOME}/node/default/etc/conf/*
		chmod -R 664 ${SSG_HOME}/node/default/etc/conf/node.properties
    fi
}
# This will set the location of the jdk into SSG_JAVA_HOME.
. ${SSG_HOME}/runtime/etc/profile

# The backup Java app must be launched from the backup home folder.
cd ${RESTORE_HOME}

#
if [ "$1" == "cfgdeamon" ]; then
    ${SSG_JAVA_HOME}/bin/java \
        -Dcom.l7tech.server.home=${SSG_HOME} \
        -Dcom.l7tech.server.backuprestore.basedir=${REL_BASE_DIR} \
        -jar ${RESTORE_HOME}/SSGBackupUtility.jar cfgdeamon
    chmod 644 ./logs/ssgrestore_*.log > /dev/null 2>&1
    chown layer7:layer7 ./logs/ssgrestore_*.log > /dev/null 2>&1
    exit
fi

checkRunningLocalGateway

# This must be run as layer7
if [ $UID -eq 0 ]; then
    ESCAPED_ARGS=""
    for ARG in "$@"; do
        ESCAPED_ARGS="$ESCAPED_ARGS $(printf '%q' ${ARG})"
    done

    # invoke backup as layer7
    su layer7 -c "${SSG_JAVA_HOME}/bin/java -Xmx256m \
        -Dcom.l7tech.server.home=${SSG_HOME} \
        -Dcom.l7tech.server.backuprestore.basedir=${REL_BASE_DIR} \
        -jar ${RESTORE_HOME}/SSGBackupUtility.jar restore ${ESCAPED_ARGS}"
        restoreFolderPermissions
elif [ "$USER" == "layer7" ]; then
    ${SSG_JAVA_HOME}/bin/java -Xmx256m \
        -Dcom.l7tech.server.home=${SSG_HOME} \
        -Dcom.l7tech.server.backuprestore.basedir=${REL_BASE_DIR} \
        -jar ${RESTORE_HOME}/SSGBackupUtility.jar restore "$@"
        restoreFolderPermissions
else
    echo "Must be root or layer7 to invoke ssgrestore.sh"
fi
