
# $Header: opatchauto/oms/createAddOMSScript.pl /st_opatchauto_pt-em24.1-ru01/1 2025/04/23 18:12:29 smkumbar Exp $
#
# createAddOMSScript.pl
# 
# Copyright (c) 2020, 2025, Oracle and/or its affiliates.
#
#    NAME
#      createAddOMSScript.pl - <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)
#    sallam      08/24/21 - bug#33268373
#    sallam      11/19/20 - Creation
# 
#!/usr/bin/perl

use Cwd ;
use File::Spec; 
use File::Path;
use File::Copy;
use File::stat;
use File::Basename;
use Fcntl qw(:flock);
local $| =1; 
$platform = $^O;
$platform=lc($platform);

#$platform = pop(@ARGV);
$get_args = $#ARGV + 1; 

my $oracleHome = @ARGV[0];
my $operType = @ARGV[1];
my $patchLoc = @ARGV[2];
my $scriptDir = @ARGV[3];
my $isSystemPatchRB = @ARGV[4];
my $spbFlag = @ARGV[5];
my $jdkUpgradeLoc = @ARGV[6];

my $agentHome = "";
my $hostname = "";

if ($get_args gt 7) {
	$agentHome = @ARGV[7];
	$hostname = @ARGV[8];
}

my $addOmsDir = File::Spec->catfile($oracleHome,".omspatcher_storage","oms_session",$scriptDir);
my $omspatcherExec = File::Spec->catfile($oracleHome,"OMSPatcher","omspatcher");
if ($platform =~ /win/){
    $omspatcherExec = File::Spec->catfile($oracleHome,"OMSPatcher","omspatcher.bat");
}
my $emctlcmd = File::Spec->catfile($oracleHome,"bin","emctl");
if ($platform =~ /win/){
    $emctlcmd = File::Spec->catfile($oracleHome,"bin","emctl.bat");
}
my $operCmd = "";
if ($operType eq "apply") {
	$operCmd = "apply $patchLoc";
}
else {
	#$operCmd = "rollback -id $patchLoc";
	if($isSystemPatchRB eq "true"){
		$operCmd = "rollback -systemid $patchLoc";
	}		
	else{
		$operCmd = "rollback -subpatchid $patchLoc";
	}	
}
$executcmd = "mkdir -p $addOmsDir";
#print "Executing the command $executcmd\n";
#$retVal = `$executcmd`;
#print "$retVal return value is : $?\n";
$exitCode = $?;
if ($exitCode != 0){
  print "\nCommand could not be executed, Please run manually using the command : $executcmd\n";
  exit $exitCode;
}

mkpath($addOmsDir, {error => \my $err, mode => 0755});

my $addOmsScript = File::Spec->catfile($addOmsDir,"addOMSPatcherScript.sh");
if ($platform =~ /win/){
    $addOmsScript = File::Spec->catfile($addOmsDir,$hostname."_addOMSPatcherScript.bat");
}

if(-e $addOmsScript){
	unlink($addOmsScript);
}

open (MYWFILE, ">>$addOmsScript")
    or die "Cannot write file because $addOmsScript not opened($!)";
	if ($platform !~ /win/){
		printf(MYWFILE "#!/bin/bash\n");	
	}

	# add the code to handle the exit code
    my $chkRet = "if [ \$? != 0 ]; then \n exit 1; \n fi";
	if ($platform =~ /win/){
		$chkRet = "if ERRORLEVEL 1 exit /b 1\n";
    } 

	if ($platform =~ /win/){
		printf(MYWFILE "\@echo off \n");
		printf(MYWFILE "\@echo ------------------%%date% %%%time% : Stopping the agent:  ------------------\n");
		printf(MYWFILE "call cmd /c $agentHome/bin/emctl.bat stop agent\n");
		printf(MYWFILE "%s\n", $chkRet);
		printf(MYWFILE "\@echo ------------------ %%date% %%%time% : Agent stopped successfully: ------------------\n");

		printf(MYWFILE "\@echo ------------------ %%date% %%%time% : Stopping the OMS : ------------------\n");
		printf(MYWFILE "call cmd /c $emctlcmd stop oms -all\n");
		printf(MYWFILE "%s\n", $chkRet);
		printf(MYWFILE "\@echo %%date% %%%time% : OMS stopped successfully : ------------------\n");
	}
	my $oh_env = "export ORACLE_HOME=$oracleHome\n";
	my $printTS = "echo ------------------ `date \"+%Y-%m-%%d %H:%M:%S\"`: Patching started ------------------\n";
        if ($platform =~ /win/){
		$oh_env = "set ORACLE_HOME=$oracleHome\n";		
		$printTS = "\@echo ------------------ %%date% %%%time% : Patching started : ------------------\n";
        }
	printf(MYWFILE "%s\n", $oh_env);
	printf(MYWFILE $printTS);
	#my $msg = "$omspatcherExec $operCmd -bitOnly -silent -oh $oracleHome\n";
	my $msg;
    if($spbFlag){
    	$msg = "$omspatcherExec $operCmd -silent -oh $oracleHome -spb_patch";
        if ($jdkUpgradeLoc ne "NA"){
        	$msg = "$msg -jdk_update $jdkUpgradeLoc";
		}
		$msg = "$msg\n";
	}
	else{
		$msg = "$omspatcherExec $operCmd -bitOnly -silent -oh $oracleHome\n";
	}
	
	if ($platform =~ /win/){
		$msg = "call cmd /c ".$msg;
	}
    printf(MYWFILE "%s\n", $msg);
    printf(MYWFILE "%s\n", $chkRet);

	$printTS = "echo ------------------ `date \"+%Y-%m-%%d %H:%M:%S\"` : Patching completed ------------------\n";
    if ($platform =~ /win/){
		$printTS = "\@echo ------------------ %%date% %%%time% : Patching completed : ------------------\n";
	}
	printf(MYWFILE $printTS);

  #  $msg = "$omspatcherExec commit -oh $oracleHome\n";
#	if ($platform =~ /win/){
#		$msg = "call cmd /c ".$msg;
#	}
 #   printf(MYWFILE "%s\n", $msg);
  #  printf(MYWFILE "%s\n", $chkRet);

	if ($platform =~ /win/){
		printf(MYWFILE "\@echo ------------------%%date% %%%time% : Starting the agent:  ------------------\n");
		printf(MYWFILE "%s\n","call cmd /c $agentHome/bin/emctl.bat start agent\n");
		printf(MYWFILE "%s\n", $chkRet);
		printf(MYWFILE "\@echo ------------------%%date% %%%time% : Agent started sucessfully:  ------------------\n");
	}
#	$printTS = "echo ------------------ `date \"+%Y-%m-%%d %H:%M:%S\"`: Patching completed ------------------\n";
#    if ($platform =~ /win/){
#		$printTS = "\@echo ------------------ %%date% %%%time% : Patching completed : ------------------\n";
#	}
	printf(MYWFILE $printTS);
	$printTS = "echo ------------------ `date \"+%Y-%m-%%d %H:%M:%S\"`: Starting the OMS ------------------\n";
    if ($platform =~ /win/){
		$printTS = "\@echo ------------------ \%%date% %%%time% : Starting the OMS : ------------------\n";
	}
	printf(MYWFILE $printTS);
	$msg = "$emctlcmd start oms\n";
	if ($platform =~ /win/){
		$msg = "call cmd /c ".$msg;
	}
    printf(MYWFILE "%s\n", $msg);
    printf(MYWFILE "%s\n", $chkRet);
	$printTS = "echo ------------------ `date \"+%Y-%m-%%d %H:%M:%S\"`: OMS started successfully ------------------\n";
    if ($platform =~ /win/){
		$printTS = "\@echo ------------------ %%date% %%%time% : OMS started successfully : ------------------\n";
	}
	printf(MYWFILE $printTS);

close MYWFILE;

#print "\n*****************************************************************************\n";
#print "\n$addOmsScript is generated. \nCopy this script to all the add oms and execute it locally\n";
#print "\n*****************************************************************************\n";
