#!/usr/bin/perl
# 
# $Header: opatchauto/oms/analyzeSpb.pl /st_opatchauto_pt-em24.1-ru01/1 2025/04/23 18:12:29 smkumbar Exp $
#
# analyzeSpb.pl
# 
# Copyright (c) 2023, 2025, Oracle and/or its affiliates.
#
#    NAME
#      analyzeSpb.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)
#    smkumbar    12/05/23 - Analyze script for Stack patches
#    smkumbar    12/05/23 - Creation
# 

use Cwd ;
use File::Spec; 
use File::Path;
use File::Copy;
use File::stat;
use File::Basename;
use Fcntl qw(:flock);
use IO::File;
use File::Path qw(mkpath);
local $| =1;

$platform = $^O;
$platform=lc($platform);

my $oracleHome = @ARGV[0];
my $patchLoc = @ARGV[1];
my $inputPlatform = @ARGV[2];

$datestring = localtime();
my $logTimeStamp = localtime();
$logTimeStamp =~ s/ /_/g;
$logTimeStamp =~ s/:/_/g;

my $omspatcherLog = File::Spec->catfile($oracleHome,"cfgtoollogs","omspatcher","SPB_PATCH","omspatcher_analyze_$logTimeStamp.log");
my $fh;
open ($fh, '>', $omspatcherLog);
print "\nOMSPatcher analyze log file :: $omspatcherLog\n";

#my $platformFile = "$inputPlatform_patchlist.txt";
my $platformPatchListFile = File::Spec->catfile($patchLoc,"binary_patches",$inputPlatform."_patchlist.txt");
my $absPathPatchListFile = File::Spec->catfile($oracleHome,$inputPlatform."_patchlist.txt");
my $opatchExec;
$opatchExec = File::Spec->catfile($oracleHome,"OPatch","opatch");
if ($platform =~ /win/){
	$opatchExec = File::Spec->catfile($oracleHome,"OPatch","opatch.bat");
}

my $parentOH = dirname($oracleHome);
my $extOracleHome = File::Spec->catfile($parentOH,"ext_oms_home");

print $fh "$datestring :: Copying the file\n";

#Generating the abs path patch list text file
my $input;
if (-e $platformPatchListFile) {
	$input = $platformPatchListFile;
	my $output = $absPathPatchListFile;
	open my $in, '<', $input or die "Could not open file '$input' $!";
	open my $out, '>', $output or die "Could not open file '$output' $!";
	my $absCompPathstr = File::Spec->catfile($patchLoc,"binary_patches");
	while (my $line = <$in>) {
		chomp $line;
		$line = File::Spec->catfile($absCompPathstr,$line);
		print $out $line . "\n";
    }
	close $in;
    close $out;
	
	my $lsPatchesCmd = $opatchExec." lspatches -oh ".$oracleHome;
	print "\nRunning Command :: ".$lsPatchesCmd."\n";
	print $fh "$datestring :: Running Command :: $lsPatchesCmd\n";
	my $lspatchesOut = `$lsPatchesCmd`;
	print $fh $lspatchesOut;

	my $noopPatchesCmd = $opatchExec." prereq CheckForNoOpPatches -oh ".$oracleHome." -phbasefile ".$absPathPatchListFile." -silent";
	print "\nRunning Command :: ".$noopPatchesCmd."\n";
	print $fh "$datestring :: Running Command :: $noopPatchesCmd\n";
	my $nooppatchesOut = `$noopPatchesCmd`;
	foreach my $data (split /[\r\n]+/, $nooppatchesOut){
		if(index($data, "The final list of no-op. patches are :") != -1){
			print "\n$data\n";
		}		
	}
	print $fh $nooppatchesOut;

	my $conflictExitCode = 0;
	my $conflictPatchesCmd = $opatchExec." prereq CheckConflictAgainstOH -oh ".$oracleHome." -phbasefile ".$absPathPatchListFile." -silent";
	print "\nRunning Command :: ".$conflictPatchesCmd."\n";
	print $fh "$datestring :: Running Command :: $conflictPatchesCmd\n";
	my $conflictPatchesOut = `$conflictPatchesCmd`;
	foreach my $data (split /[\r\n]+/, $conflictPatchesOut){
		if($data =~ m[Prereq.*checkConflictAgainstOH.*failed]){
			print "\nCheckConflictAgainstOH has detected Conflicts against Oracle Home, please check the omspatcher log for complete details on conflicts.\n";
			$conflictExitCode=50;
		}		
	}
	print "\n";
	print $fh $conflictPatchesOut;
	
	$conflictPatchesCmd = $opatchExec." prereq CheckConflictAgainstOH -oh ".$extOracleHome." -phbasefile ".$absPathPatchListFile." -silent";
	print "\nRunning Command :: ".$conflictPatchesCmd."\n";
	print $fh "$datestring :: Running Command :: $conflictPatchesCmd\n";
	$conflictPatchesOut = `$conflictPatchesCmd`;
	foreach my $data (split /[\r\n]+/, $conflictPatchesOut){
		if($data =~ m[Prereq.*checkConflictAgainstOH.*failed]){
			print "\nCheckConflictAgainstOH has detected Conflicts against Oracle Home, please check the omspatcher log for complete details on conflicts.\n";
			$conflictExitCode=50;
		}		
	}
	print "\n";
	print $fh $conflictPatchesOut;
	close($fh);
	exit $conflictExitCode;
}

close($fh);
