#!/bin/bash
#
#########################################################################################
# Script: 		abs_deploy_certificates.sh
# Description: 	Deploy Application and Report Server Certificates Online
#               No Service restart is needed
# Author: 		ABS Laundry Business Solutions
# Version: 		9.09.02
#### Changelog of script ################################################################
# Date        Author    Version 	Change
# 26-May-2025 RAY	    9.09.02 	Enable Application Server https
# 01-Jun-2025 RAY       9.09.02     ABSSolute Server - Standardization & hot redeploy - TS-839
#########################################################################################

deploy_app_certs()
{
  ABSM_ENVIRONMENT="${1}"
  ABSM_SERVERNAME="${2}"  
  ABSM_SERVICE="${3}"
  ABSM_TYPE="${4}"
  ABSM_SLOT="${5}"
  ABSM_LOCAL_HOME="${6}"
  ABSM_REMOTE_HOME="${7}"
  ABSM_USER="${8}"
  ABSM_PWD="${9}"
  ABSM_ORATNS="${10}"
  ABSM_ORAHOME="${11}"
  ABSM_DBSERVERNAME="${12}"
  ABSM_DBDRIVERTYPE="${13}"
  ABSM_DBPORTNUMBER="${14}"
  ABSM_HEAPSPACE="${15}"
  ABSM_STATIONNR="${16}"
  ABSM_GFUSER="${17}"
  ABSM_GFPASWRD="${18}"
  ABSM_GFDOMAIN="${19}"
  ABSM_WEBCONTEXT="${20}"
  ABSM_WEBNAME="${21}"
  ABSM_WEBLIB="${22}"
  ABSM_CONNAPPSRV="${23}"
  ABSM_CONNAPPSLOT="${24}"
  ABSM_CONNREPSRV="${25}"
  ABSM_CONNREPSLOT="${26}"
  ABSM_USENATIVE="${27}"
  ABSM_EXCLUDECUSTREP="${28}"
  ABSM_WEB_VERSION="${29}"
  ABSM_CLIENTDISTR_SRV="${30}"
  ABSM_CLIENTDISTR_DIR="${31}"
  ABSM_USER2="${32}"
  ABSM_PWD2="${33}"
  ABSM_STATIONNR2="${34}"
  ABSM_INET_KEY="${35}"

  APP_PROPERTIES_FILE="$ABSM_LOCAL_HOME/Server/Config/application.properties"
  
  if [ $ABSSOLUTENUMVERSION -ge 90902 ]
  then
	if [ "$ABS_ROOTSERVER" == "$ABSM_SERVERNAME" ] 
    then
	  if [ -e "$APP_PROPERTIES_FILE" ]
	  then
	    cp -f $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.p12 $ABSM_LOCAL_HOME/Server/Config/
	  else
	    echo "Property file $ABSM_SERVERNAME:$APP_PROPERTIES_FILE not found - please verify if slot exits."
		EXITFLAG=1
	  fi
	else
	  if ssh -n -q $ABSM_SERVERNAME "test -e $APP_PROPERTIES_FILE"
	  then
	    scp -q $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.p12 $ABSM_REMOTE_HOME/Server/Config/
	  else
	    echo "Property file $ABSM_SERVERNAME:$APP_PROPERTIES_FILE not found - please verify if slot exits."
		EXITFLAG=1
	  fi
	fi
  fi
}

deploy_rep_certs()
{
  ABSM_ENVIRONMENT="${1}"
  ABSM_SERVERNAME="${2}"  
  ABSM_SERVICE="${3}"
  ABSM_TYPE="${4}"
  ABSM_SLOT="${5}"
  ABSM_LOCAL_HOME="${6}"
  ABSM_REMOTE_HOME="${7}"
  ABSM_USER="${8}"
  ABSM_PWD="${9}"
  ABSM_ORATNS="${10}"
  ABSM_ORAHOME="${11}"
  ABSM_DBSERVERNAME="${12}"
  ABSM_DBDRIVERTYPE="${13}"
  ABSM_DBPORTNUMBER="${14}"
  ABSM_HEAPSPACE="${15}"
  ABSM_STATIONNR="${16}"
  ABSM_GFUSER="${17}"
  ABSM_GFPASWRD="${18}"
  ABSM_GFDOMAIN="${19}"
  ABSM_WEBCONTEXT="${20}"
  ABSM_WEBNAME="${21}"
  ABSM_WEBLIB="${22}"
  ABSM_CONNAPPSRV="${23}"
  ABSM_CONNAPPSLOT="${24}"
  ABSM_CONNREPSRV="${25}"
  ABSM_CONNREPSLOT="${26}"
  ABSM_USENATIVE="${27}"
  ABSM_EXCLUDECUSTREP="${28}"
  ABSM_WEB_VERSION="${29}"
  ABSM_CLIENTDISTR_SRV="${30}"
  ABSM_CLIENTDISTR_DIR="${31}"
  ABSM_USER2="${32}"
  ABSM_PWD2="${33}"
  ABSM_STATIONNR2="${34}"
  ABSM_INET_KEY="${35}"

  CRYSTALCLEAR_PROPERTIES_FILE="$ABSM_LOCAL_HOME/Reportserver/Config/crystalclear.properties"
  
  if [ $ABSSOLUTENUMVERSION -ge 90902 ]
  then
	if [ "$ABS_ROOTSERVER" == "$ABSM_SERVERNAME" ] 
    then
	  if [ -e "$CRYSTALCLEAR_PROPERTIES_FILE" ]
	  then
	    cp -f $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.crt $ABSM_LOCAL_HOME/Reportserver/Config/
		cp -f $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.key $ABSM_LOCAL_HOME/Reportserver/Config/
	  else
	    echo "Property file $ABSM_SERVERNAME:$CRYSTALCLEAR_PROPERTIES_FILE not found - please verify if slot exits."
		EXITFLAG=1
	  fi
	else
	  if ssh -n -q $ABSM_SERVERNAME "test -e $CRYSTALCLEAR_PROPERTIES_FILE"
	  then
	    scp -q $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.crt $ABSM_REMOTE_HOME/Reportserver/Config/
		scp -q $ABSMENUREPOSITORYLOCATION/$ABSM_ENVIRONMENT/Future/CERTIFICATES/*.key $ABSM_REMOTE_HOME/Reportserver/Config/
	  else
	    echo "Property file $ABSM_SERVERNAME:$CRYSTALCLEAR_PROPERTIES_FILE not found - please verify if slot exits."
		EXITFLAG=1
	  fi
	fi
  fi
}

# Main
# $1 = environment
EXITFLAG=0
echo "Initiating Server Certificates update process to $1 environment."

grep '^ *'$1"[[:blank:]]" $ABSMENUGLOBALCONFIG | sort -k2,2 -k5,5 -k4,4 | while read ABSM_ENVIRONMENT ABSM_SERVERNAME ABSM_SERVICE ABSM_TYPE ABSM_SLOT ABSM_LOCAL_HOME ABSM_REMOTE_HOME ABSM_USER ABSM_PWD ABSM_ORATNS ABSM_ORAHOME ABSM_DBSERVERNAME ABSM_DBDRIVERTYPE ABSM_DBPORTNUMBER ABSM_HEAPSPACE ABSM_STATIONNR ABSM_GFUSER ABSM_GFPASWRD ABSM_GFDOMAIN ABSM_WEBCONTEXT ABSM_WEBNAME ABSM_WEBLIB ABSM_CONNAPPSRV ABSM_CONNAPPSLOT ABSM_CONNREPSRV ABSM_CONNREPSLOT ABSM_USENATIVE ABSM_EXCLUDECUSTREP ABSM_WEB_VERSION ABSM_CLIENTDISTR_SRV ABSM_CLIENTDISTR_DIR ABSM_USER2 ABSM_PWD2 ABSM_STATIONNR2 ABSM_INET_KEY
do
  [ ${EXITFLAG} == 1 ] && break
  if [ "$ABSM_SERVICE" == "https" ]
  then
    case "$ABSM_TYPE" in
      app)  deploy_app_certs $ABSM_ENVIRONMENT $ABSM_SERVERNAME $ABSM_SERVICE $ABSM_TYPE $ABSM_SLOT $ABSM_LOCAL_HOME $ABSM_REMOTE_HOME $ABSM_USER $ABSM_PWD $ABSM_ORATNS $ABSM_ORAHOME $ABSM_DBSERVERNAME $ABSM_DBDRIVERTYPE $ABSM_DBPORTNUMBER $ABSM_HEAPSPACE $ABSM_STATIONNR $ABSM_GFUSER $ABSM_GFPASWRD $ABSM_GFDOMAIN $ABSM_WEBCONTEXT $ABSM_WEBNAME $ABSM_WEBLIB $ABSM_CONNAPPSRV $ABSM_CONNAPPSLOT $ABSM_CONNREPSRV $ABSM_CONNREPSLOT $ABSM_USENATIVE $ABSM_EXCLUDECUSTREP $ABSM_WEB_VERSION $ABSM_CLIENTDISTR_SRV $ABSM_CLIENTDISTR_DIR $ABSM_USER2 $ABSM_PWD2 $ABSM_STATIONNR2 $ABSM_INET_KEY
	        ;;
      rep)  deploy_rep_certs $ABSM_ENVIRONMENT $ABSM_SERVERNAME $ABSM_SERVICE $ABSM_TYPE $ABSM_SLOT $ABSM_LOCAL_HOME $ABSM_REMOTE_HOME $ABSM_USER $ABSM_PWD $ABSM_ORATNS $ABSM_ORAHOME $ABSM_DBSERVERNAME $ABSM_DBDRIVERTYPE $ABSM_DBPORTNUMBER $ABSM_HEAPSPACE $ABSM_STATIONNR $ABSM_GFUSER $ABSM_GFPASWRD $ABSM_GFDOMAIN $ABSM_WEBCONTEXT $ABSM_WEBNAME $ABSM_WEBLIB $ABSM_CONNAPPSRV $ABSM_CONNAPPSLOT $ABSM_CONNREPSRV $ABSM_CONNREPSLOT $ABSM_USENATIVE $ABSM_EXCLUDECUSTREP $ABSM_WEB_VERSION $ABSM_CLIENTDISTR_SRV $ABSM_CLIENTDISTR_DIR $ABSM_USER2 $ABSM_PWD2 $ABSM_STATIONNR2 $ABSM_INET_KEY
			;;
		*)  export ABSMESSAGE="     Invalid Servive Type make_http_https.sh"; absmenu_log
		    ;;
    esac
  else
    export ABSMESSAGE="     Supports only https - abs_deploy_certificates.sh"; absmenu_log
  fi
done

[ ${EXITFLAG} == 0 ] && echo "Server Certificates have been updated online successfully."