#!/bin/bash
#
#
#-----------------------------------------------------------------------------
# Licensed materials - Property of IBM
# 5725-G39 Copyright IBM Corp. 2022
#
# All rights reserved
#
# US Government users restricted rights  -  Use, duplication or
# disclosure restricted by GSA ADP schedule contract with IBM Corp.
#
# IBM Z Development and Test Environment (ZD&T)
#-----------------------------------------------------------------------------
# Prepare the initial IPL parm for ZD&T m/c
#
#
# Here is the process to execute the step.
#
#
#
# Script called by:
#
#               zdt_config_ipl <JES2> <CICS61> <CICS56> <DB13> <DB12> <ZOS> <WAS> <IMS15> <IZU> <UCD> <ZCON> <ZOSX> <RTC> <NFS> <HTTP> <CSQ9> <RSEAPI> <DBG> <DBB>
#                              ==> JES2 -- Provide this parm if, you would like to start the JES2 as WARM otherwise don't provide the value.
#                              ==> CICS56/CICS61 -- If you would like to IPL CICS6.1 or CICS 5.6 with ZOS. 
#                              ==> DB12/DB13  -- If you would like to IPL DB2 V12 or V13 or both with ZOS.
#                              ==> ZOS  -- If you would like to only start base ZOS.
#                              ==> WAS  -- If you would like to enable Web sphere with base zos.
#                              ==> IMS15  -- If you would like to enable IMS V 15 with base zos.
#                                  
# This function calls:
#
#               no one else
#
#
###########################################################################################################################################################
#### Below function is to find the ZOS section and comment or un-comment ##################################################################################
###########################################################################################################################################################

parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
#Redirect the script output to its same name log
# exec > /var/log/zdt/$(basename $0).log 2>&1
echo "[`date '+%Y-%m-%d %H:%M:%S %Z'`] executing command: $0 $*"

Replace_JES2 () {

JES2_parm=$1
echo "ZDTC10_0200i *********************************JES2 change starts ***********************************"
echo "ZDTC10_0201i ## IEASYMAU change starts ##"

COMMND_parmname_found=`pdsUtil $RES_path/$RES_filename $PARMLIB_name/$COMMND_parmname --extract /$tmpdir/$COMMND_parmname.txt `
pdsUtil_rc=$?

if [ $pdsUtil_rc -ne 0 ]; then
	echo "ZDTC10_0210e pdsUtil extract failed for "$PARMLIB_name $COMMND_parmname 
	exit 8
fi
if [ -n "$COMMND_parmname_found" ] ; then
      echo "ZDTC10_0210i  PDS member $COMMND_parmname from DSN=$PARMLIB_name found"
   else
      echo "ZDTC10_0210e  Unable to get PDS member $COMMND_parmname from DSN=$PARMLIB_name"
      exit 8
fi
 
PARM_val=`cat /$tmpdir/$COMMND_parmname.txt | grep JES2STR_= | cut -d'(' -f3 | cut -d',' -f1`

if [ -n "$PARM_val" ] ; then
   if [ $PARM_val = $JES2_parm ]; then
   echo "ZDTC10_0220i No change require in $COMMND_parmname. Its already $JES2_parm"
   return
   else
   echo "ZDTC10_0230i changing from $PARM_val to $JES2_parm in $COMMND_parmname"
   fi
fi

sed -i "s,$PARM_val,$JES2_parm," /$tmpdir/$COMMND_parmname.txt

CHANGED_val=` cat /$tmpdir/$COMMND_parmname.txt | grep $JES2_parm`

if [ -n "$CHANGED_val" ] ; then
    echo "ZDTC10_0240i  Parm $PARM_val is changed to $JES2_parm in $COMMND_parmname"
else
    echo "ZDTC10_0240e Unable to change the parm from $PARM_val to $JES2_parm"

fi

PDS_changed=`pdsUtil $RES_path/$RES_filename $PARMLIB_name/$COMMND_parmname --overlay /$tmpdir/$COMMND_parmname.txt`
pdsUtil_rc=$?
if [ $pdsUtil_rc -ne 0 ]; then
	echo "ZDTC10_0210e pdsUtil overlay failed for "$PARMLIB_name $COMMND_parmname 
        exit 8
fi

##PDS_changed=`pdsUtil $RES_path/$RES_filename $PARMLIB_name/$COMMND_parmname --extract /$tmpdir/$COMMND_parmname.txt| grep $COMMND_parmname | cut -d':' -f2  `

if [ -n "$PDS_changed" ] ; then
   
##   CHANGED_PDS_val=`cat /$tmpdir/$COMMND_parmname.txt | grep PARM= | cut -d''\' -f3 | cut -d',' -f1`
##   if [ $CHANGED_PDS_val = $JES2_parm ] ; then
      echo "ZDTC10_0250i $PARMLIB_name/$COMMND_parmname is updated and value changed to $CHANGED_PDS_val"
else
      echo "ZDTC10_0250e unable to change $PARMLIB_name/$COMMND_parmname"
      exit 8
##   fi

fi

echo "ZDTC10_0299i *********************************** Change for JES2 ends ******************************************"
}

#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ END of Replace_JES2 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#


Replace_Parm() {

Cond_Replace="NO"
count=0
Start_line=0
End_line=0
Parmname="$2"
Parmname_found=""
echo "ZDTC10_101i  $RES_path/$RES_filename $PARMLIB_name/$Parmname"


Parmname_found=`pdsUtil $RES_path/$RES_filename $PARMLIB_name/$Parmname --extract /$tmpdir/$Parmname.txt| grep $Parmname | cut -d':' -f2  `
pdsUtil_rc=$?

if [ $pdsUtil_rc -ne 0 ]; then
        echo "ZDTC10_0210e pdsUtil extract failed for "$PARMLIB_name $Parmname 
        exit 8
fi


echo "ZDTC10_102i $Parmname_found"

if [ -n "$Parmname_found" ] ; then
      echo "ZDTC10_0103i  PDS member $Parmname from DSN=$PARMLIB_name found"
   else
      echo "ZDTC10_0104e  Unable to get PDS member $Parmname from DSN=$PARMLIB_name"
      exit 8
fi

while IFS='' read -r line || [[ -n "$line" ]]; do

count=$(( $count + 1 ))

#to check the start of Parm type
    if [[ $line == *"$3"* ]]; then
       Cond_Replace="YES"
       Start_line=$(( $count + 1 ))
	   echo "ZDTC10_0105i $2 Found at line :  $Start_line"
    fi
	
#To check the end of parm type
    if [[ $line == *"$4"* ]]; then
       Cond_Replace="NO"
       End_line=$(( $count - 1 ))
       echo "ZDTC10_0106i $2 end found at line: $End_line"
       break
    fi

done < "/$tmpdir/$Parmname.txt"



if [[ ${1} = "comment"  ]]; then
	if [[ (${2} = "LPALSTAU") ]]; then
		sed -i  ''"$Start_line"','"$End_line"'{s/^./*/}' "/$tmpdir/$Parmname.txt"
	else
		sed -i ''"$Start_line"','"$End_line"'{s/./\*/70;s/./\//71}' "/$tmpdir/$Parmname.txt"
		sed -i ''"$Start_line"','"$End_line"'{s/^../\/'"\*"'/}' "/$tmpdir/$Parmname.txt"
   fi
else
	if [[ ${2} = "LPALSTAU" ]]; then
		sed  -i ''"$Start_line"','"$End_line"'{s/^./'" "'/}' "/$tmpdir/$Parmname.txt"
	else
		sed -i ''"$Start_line"','"$End_line"'{s/^../'"  "'/}' "/$tmpdir/$Parmname.txt" 
		sed -i ''"$Start_line"','"$End_line"'{s/./\'" "'/70;s/./\'" "'/71}' "/$tmpdir/$Parmname.txt"
	fi
fi


Parmname_found=""

Parmname_found=`pdsUtil $RES_path/$RES_filename $PARMLIB_name/$Parmname --overlay /$tmpdir/$Parmname.txt | grep $Parmname | cut -d':' -f2  `
pdsUtil_rc=$?

if [ $pdsUtil_rc -ne 0 ]; then
        echo "ZDTC10_0210e pdsUtil overlay failed for "$PARMLIB_name $Parmname 
        exit 8
fi

if [ -n "$Parmname_found" ] ; then
      echo "ZDTC10_0107i  PDS member $Parmname from DSN=$PARMLIB_name updated"
   else
      echo "ZDTC10_0108e  Unable to update PDS member $Parmname from DSN=$PARMLIB_name"
      exit 8
fi

echo "ZDTC10_0109i ## Change $2 ends ##"

}


Replace_Start(){
        echo "ZDTC10_0110i Calling Replace Start for $COMMND_parmname $1"
	sed -i "{s/$1='NOS'/$1='IPL'/g}" "/$tmpdir/$COMMND_parmname.txt"

}


Disable_Start(){
        echo "ZDTC10_0111i Calling Disable Start for $COMMND_parmname $1"
        sed -i "{s/$1='IPL'/$1='NOS'/g}" "/$tmpdir/$COMMND_parmname.txt"

}

#######################################################################################################################################################################################
#######################################################################################################################################################################################
#################################################### START OF PROGRAM #################################################################################################################
#######################################################################################################################################################################################
#######################################################################################################################################################################################


# Assigning values to different parms
# Dont change it until very necessary. If you are working on other version of ADCD, replace accordingly
#

RES_filename=B3CFG1
COMMND_parmname=IEASYMAU
LIB_path=FEU.Z31B.PARMLIB

echo "***********************************Starting the process*********************************"


#
# checking arguments
#
RES_path=$1

if [ -z "$RES_path" ]; then
	echo "ZDTI00_0001e  Specify volumes_path"
	exit 12
else
    echo "ZDTI00_0000i res_path is $RES_path."
fi

VERSION=$2

if [ -z "$VERSION" ]; then
	echo "ZDTI00_0001e  Specify zdt_version"
	exit 12
else
    echo "ZDTI00_0000i zdt_version is $VERSION."
fi

DEPLOYMENTDIR=$3
if [ -z "$DEPLOYMENTDIR" ]; then
	echo "ZDTI00_0001e Specify deployment directory"
	exit 12
fi

# run zdtenv
. ~/.zdtenv_$VERSION
. $DEPLOYMENTDIR/.zdtenv_$VERSION
. ~/.userenv_$VERSION
. $DEPLOYMENTDIR/.userenv_$VERSION
echo "--- After sourcing env file --- path is: $PATH ---"

shift 3

for args in "$@"
do
args=${args^^}
   echo "ZDTC10_0001i Argument supplied: " $args
  case $args in
     DBG)
        DBGMGR_Parm=Y
        ;;
     DBB)
	DBB_Parm=Y
	;;
     RSEAPI)
        RSEAPI_Parm=Y
        ;;
     DBB) 
        DBB_Parm=Y
        ;;
     IMS15)
        IMS15_Parm=Y
        ;;
     ZOS)
        ZOS_Parm=Y
        ;;
     JES2)
         JES2_Parm=Y
        ;;
     WAS)
         WAS_Parm=Y
        ;;
     CICS61)
	 CICS61_Parm=Y
        ;;
     CICS56)
	 CICS56_Parm=Y
        ;;
     DB13)
         DB13_Parm=Y
        ;;
     DB12)
	 DB12_Parm=Y
	;;
     RTC)
        RTC_Parm=Y
        ;;
     IZU)
	IZU_Parm=Y
	;;
     UCD)
	UCD_Parm=Y
	;;
     ZCON)
	ZCON_Parm=Y
	;;
     ZOSX)
	ZOSX_Parm=Y
	;;
     HTTP)
        HTTP_Parm=Y
        ;;
     CSQ9)
        CSQ9_Parm=Y
        ;;
     NFS)
        NFS_Parm=Y
        ;;


     \?)
        echo "ZDTC10_0001e  Invalid paramaters: "$args
        echo "zdt_config_ipl <JES2> <CICS61> <CICS56> <DB13> <DB12> <ZOS> <WAS> <IMS15> <IZU> <UCD> <ZCON> <ZOSX> <HTTP> <CSQ9> <NFS>"

        echo "  "                                
        echo " ZOS parameter is mandatory. Otherwise ipl won't happen"
        echo "            e.g.     ./zdt_config_ipl ZOS JES2 CICS56       --- It will warm start the JES2 along with CICS 5.4"
        echo "                     ./zdt_config_ipl                 --- It will cold start the JES2 along with ZOS."
        echo "                     ./zdt_config_ipl IMS14 ZOS            --- It will cold start the JES2 along with IMS14 and ZOS."
        echo "                     ./zdt_config_ipl JES2 CICS55 DB12 ZOS WAS RAT       --- It will warm start the JES2 along with all products."
        echo "           Use IPL parameter "AU" in runzpdt to ipl the ZOS."
        exit 8
        ;;
  esac

done

tmpdir=$RES_path/../tmp
mkdir -p $tmpdir

#RES_path="/home/ibmsys1/zVolumes"
cd "$RES_path"

DIR_volume_list=`ls`

PARMLIB_name=`listVtoc $RES_path/$RES_filename | grep -i DSNAME | grep $LIB_path | cut -d':' -f2| head -n 1 | xargs  `

echo "ZDTC10_0001i Parmlib name and path: $PARMLIB_name"

echo "ZDTC10_0002i Reading Netview Startup overrides member"
Netview_Startups=`pdsUtil $RES_path/$RES_filename $LIB_path/$COMMND_parmname --extract /$tmpdir/$COMMND_parmname.txt `
pdsUtil_rc=$?

if [ $pdsUtil_rc -ne 0 ]; then
        echo "ZDTC10_0001e pdsUtil extract failed "$LIB_path $COMMND_parmname 
        exit 8
fi

if [[ -z $args ]]; then
   echo " ZDTC10_0001e Please specify the arguments to proceed"
   exit 99
fi

#
# Calling different functions depending on arguments
#

if [[ $JES2_Parm == Y ]]; then
  
  echo "ZDTC10_0003i Calling JES2 with parameter WARM" 
  Replace_JES2 WARM
else
  echo "ZDTC10_0009i Calling JES2 with parameter COLD" 
  Replace_JES2 COLD
fi


if [[ $IMS15_Parm == Y ]]; then

  echo "ZDTC10_0020i Calling IMS 15 to enable startup" 
  Replace_Start IMS15RL1 
  Replace_Start IMS15CR1 
else
  Disable_Start IMS15RL1
  Disable_Start IMS15CR1
fi

if [[ $WAS_Parm == Y ]]; then

  echo "ZDTC10_0040i Calling Websphere to enable startup" 

fi

if [[ $DB13_Parm == Y ]]; then

  echo "ZDTC10_0050i Calling DB2 13.1 to enable startup"
  Replace_Start DBDGMSTR
else
  Disable_Start DBDGMSTR
fi

if [[ $DB12_Parm == Y ]]; then

  echo "ZDTC10_0050i Calling DB2 12.1 to enable startup"
  Replace_Start DBCGMSTR 
else
  Disable_Start DBCGMSTR
fi

if [[ $CICS61_Parm == Y ]]; then

  echo "ZDTC10_0070i Calling CICS61 to enable startup and enable LPA"
  Replace_Start CICSTS61 
  Replace_Parm uncomment LPALSTAU "*CICS61*" "*CICS56*" 
else
  echo "ZDTC10_0075i Calling CICS61 to comment" 
  Replace_Parm comment LPALSTAU "*CICS61*" "*CICS56*"
  Disable_Start CICSTS61
fi

if [[ $CICS56_Parm == Y ]]; then

  echo "ZDTC10_0080i Calling CICS56 to enable startup and enable LPA" 
  Replace_Start CICSTS56
  Replace_Parm uncomment LPALSTAU "*CICS56*" "**"
else
  echo "ZDTC10_0085i Calling CICS56 to comment" 
  Replace_Parm comment LPALSTAU "*CICS56*" "**"
  Disable_Start CICSTS56
fi

if [[ $IZU_Parm == Y ]]; then
  echo "ZDTC10_0090i Calling IZU to enable startup"
  Replace_Start IZUSVR1
  Replace_Start IZUANG1
else
  Disable_Start IZUANG1	
  Disable_Start IZUSVR1	
fi

if [[ $UCD_Parm == Y ]]; then
  echo "ZDTC10_0100i Calling UCD to enable startup"
  Replace_Start BUZAGNT
else
  Disable_Start BUZAGNT  
fi

if [[ $ZCON_Parm == Y ]]; then
  echo "ZDTC10_0110i Calling ZCON to enable startup"
  Replace_Start ZOSCSRV
  Replace_Start ZOSCAGL
else
  Disable_Start ZOSCAGL 
  Disable_Start ZOSCSRV 
fi

if [[ $ZOSX_Parm == Y ]]; then
  echo "ZDTC10_0120i Calling ZOSX to enable startup"
  Replace_Start JMON
  Replace_Start RSED
else
  Disable_Start JMON
  Disable_Start RSED
fi

if [[ $RTC_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling RTC to enable startup"
  Replace_Start BLZBFA
  Replace_Start BLZISPFD
else
  Disable_Start BLZBFA
  Disable_Start BLZISPFD
fi

if [[ $HTTP_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling HTTP to enable startup"
  Replace_Start HTTPD1
else
  Disable_Start HTTPD1
fi

if [[ $CSQ9_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling CSQ9 to enable startup"
  Replace_Start CSQ9CHIN
  Replace_Start CSQ9MSTR
else
  Disable_Start CSQ9MSTR
  Disable_Start CSQ9CHIN
fi

if [[ $NFS_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling NFS to enable startup"
  Replace_Start NFSS
else
  Disable_Start NFSS
fi

if [[ $DBB_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling DBB,DBBS to enable startup"
  Replace_Start DBB
  Replace_Start DBBS
else
  Disable_Start DBB
  Disable_Start DBBS
fi

if [[ $DBGMGR_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling DBGMGR,EQAPROF,EQARMTD to enable startup"
  Replace_Start DBGMGR
  Replace_Start EQAPROF
  Replace_Start EQARMTD
else
  Disable_Start DBGMGR
  Disable_Start EQAPROF
  Disable_Start EQARMTD
fi


if [[ $RSEAPI_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling RSEAPI to enable startup"
  Replace_Start RSEAPI
else
  Disable_Start RSEAPI
fi

if [[ $DBB_Parm == Y ]]; then
  echo "ZDTC10_0130i Calling DBB to enable startup"
  Replace_Start DBB
else
  Disable_Start DBB
fi


Netview_Startups=`pdsUtil $RES_path/$RES_filename $LIB_path/$COMMND_parmname --overlay /$tmpdir/$COMMND_parmname.txt `
pdsUtil_rc=$?

if [ $pdsUtil_rc -ne 0 ]; then
        echo "ZDTC10_0003e pdsUtil overlay failed for Netview Startup symbolics "$LIB_path $COMMND_parmname
        exit 8
fi


echo "***********************************End of Process*******************************"

exit 0

