#!/bin/sh
#
# $Header: opatchauto/oms/generateMultiOMSPatchingScriptsForNZDT.sh /st_opatchauto_pt-em13.5mos/2 2021/05/31 06:21:54 sallam Exp $
#
# generateMulitOMSPatchingScriptsForNZDT.sh
#
# Copyright (c) 2017, 2021, Oracle and/or its affiliates. 
#
#    NAME
#      generateMulitOMSPatchingScriptsForNZDT.sh - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    skkurapa   11/15/17 - Add some more inputs to this utility
#    skkurapa   10/16/17 - update patching commands and instructions
#     			    add property to set nzdt mode as false for omspatcher cmd
#    vidjain    10/12/17 - Creation
#

ORACLE_HOME=$1
filename=$2  #successfully applied patch detail file path.
localHost=$3
#scriptDir=$ORACLE_HOME/.omspatcher_storage/nzdt
scriptDir=$4
scriptName=$5
scriptFile=$scriptDir/$scriptName
#scriptFile=/scratch/multiOmsScript.sh
command="";

echo "scriptFile path is ..................." $scriptFile 

function readInfoFromFile(){
#delete if script file already exist
[ -e $scriptFile ] && rm $scriptFile 
cat >> ${scriptFile} <<EOF
#!/bin/bash
ORACLE_HOME=$ORACLE_HOME
GENEREATED_SCRIPT_PATH=$scriptDir
mkdir -p $ORACLE_HOME/.omspatcher_storage/nzdt
EOF

while read -r line
do
    name="$line"
    echo "Name read from file - $name "
   # echo $line |cut -d" " -f2

IFS=;
ary=($line)
#for key in "${!ary[@]}"; do echo " key is ::::  $key ${ary[$key]}"; done

arr=(${line//;/ })
echo "arr:  ${arr}"
patchId=$(echo $arr | cut -d" " -f1)
echo "patchInstructionId" $patchId
patchAction=$(echo $arr | cut -d" " -f2)
echo "patchAction" $patchAction
patchLocation=$(echo $arr | cut -d" " -f3)
echo "patchLoc" $patchLocation

#if [ "$patchAction" == "APPLY" ]; then


generateSHFile $patchLocation $patchAction $patchId	
done < "$filename"
} 

function generateSHFile(){
patchLocation=$1
patchAction=$2
patchId=$3

if [ "$patchAction" == "APPLY" ]; then
sysid=$(echo $patchLocation | rev | cut -d'/' -f1 | rev) 
pLoc=/scratch/$patchId/$sysid
cat >> ${scriptFile} <<EOF
[ ! -f "$scriptDir/${patchId}.txt" ] && {	
		echo "$patchId marker file not exist..Need to run command for $patchAction and $patchLocation."
                mkdir -p /scratch/$patchId
		scp -r  $localHost:$patchLocation /scratch/$patchId
                #this property (nzdt mode=false)is needed to prevent patching in cloned home
		$ORACLE_HOME/OMSPatcher/omspatcher apply $pLoc -bitonly -silent -oh $ORACLE_HOME OMSPatcher.NZDT_MODE=false
	if [ $(echo ''\$\?'') == 0 ]; then
	echo "OMSPatcher executed successfully"
	mkdir -p $scriptDir
        touch $scriptDir/${patchId}.txt

	else
	echo "Error in OMSPatcher command execution,refer log file for details."
	exit 1
	fi

	} || echo "Skipping OMSPatcher apply command execution for patch instruction $patchId ......."

EOF
else
cat >> ${scriptFile} <<EOF
[ ! -f "$scriptDir/${patchId}.txt" ] && {                                                       
		echo "$patchId marker file not exist..Need to run command for $patchAction and $patchLocation."
                $ORACLE_HOME/OMSPatcher/omspatcher rollback -id $patchLocation -bitonly -silent -oh $ORACLE_HOME OMSPatcher.NZDT_MODE=false
	
	if [ $(echo ''\$\?'') == 0 ]; then
        echo "OMSPatcher executed successfully"
	mkdir -p $scriptDir
	touch $scriptDir/${patchId}.txt
        else
	echo "Error in OMSPatcher command execution,refer log file for details."
        exit 1
        fi

        } || echo "Skipping OMSPatcher rollback command execution for patch instruction $patchId ......."

EOF

fi


}

readInfoFromFile
