#!/bin/bash
#
#########################################################################################
# Script: abs_encryption_key.sh
# Description: Enable Application and Report https communication
# 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
#########################################################################################

# Create Key
#chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#^&*()-_=+"
chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
ENCRYPTION_KEY=""
for (( i=0; i<32; i++ ))
do
  position=$((RANDOM % ${#chars}))
  ENCRYPTION_KEY="${ENCRYPTION_KEY}${chars:position:1}"
done

# Variables
TARGET="$(echo ~/.bashrc)"
LINE1="# ABS Encryption Key"
LINE2="export ABS_INSTANCE_ID="
LINE3="export ABS_INSTANCE_ID=\"${ENCRYPTION_KEY}\""

abs_encryption_key()
{
  # $1 - ABSM_SERVERNAME
  V_ABSM_SERVERNAME=$1

  # Add Lines to .bashrc
  if [ $ABSSOLUTENUMVERSION -ge 90902 ]
  then
	if [ "$ABS_ROOTSERVER" == "$V_ABSM_SERVERNAME" ] 
    then
      if grep -Fq "${LINE1}" ${TARGET}
      then 
	    echo "Encryption Line 1 already present on server: $V_ABSM_SERVERNAME"
      else
	    echo -e "" >> ${TARGET}
		echo -e ${LINE1} >> ${TARGET}
	  fi
	  if grep -Fq "${LINE2}" ${TARGET}
      then
	    echo "Encryption line 2 already present on server: $V_ABSM_SERVERNAME"
      else
        echo -e ${LINE3} >> ${TARGET}
		source ${TARGET}
		echo "ABS Encryption Key has been installed on server: $V_ABSM_SERVERNAME - Please restart session for the key to be effective."
		export ABSMESSAGE="     ABS Encryption Key has been installed on server: $V_ABSM_SERVERNAME - Please restart session for the key to be effective."; absmenu_log
	  fi
    else
	  ssh -n -q ${V_ABSM_SERVERNAME} " if grep -Fq '${LINE1}' ${TARGET} ; then ( echo "Encryption Line 1 already present on server: $V_ABSM_SERVERNAME" ) ; else ( echo -e "" >> ${TARGET} ; echo -e '${LINE1}' >> ${TARGET} ) ; fi "
	  ssh -n -q ${V_ABSM_SERVERNAME} " if grep -Fq '${LINE2}' ${TARGET} ; then ( echo "Encryption line 2 already present on server: $V_ABSM_SERVERNAME" ) ; else ( echo -e '${LINE3}' >> ${TARGET} ; source ${TARGET} ; echo "ABS Encryption Key has been installed on server: $V_ABSM_SERVERNAME - Please restart session for the key to be effective." ) ; fi "
	fi
  else
    echo "Encryption key is not supported prior to ABSSolute v9.09.02"
    export ABSMESSAGE="     Encryption is not supported prior to ABSSolute v9.09.02"; absmenu_log
  fi
}

# Main
# $1 = environment
# 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

grep '^ *'$1"[[:blank:]]" $ABSMENUGLOBALCONFIG | sort -k1,1 -k2,2 | awk ' { print $2 } ' | uniq | while read ABSM_SERVERNAME
do
  abs_encryption_key $ABSM_SERVERNAME
done
