#!/usr/bin/perl
# 
# $Header: opatchauto/oms/PrereqSpb.pl /st_opatchauto_pt-em13.5mos/1 2024/11/27 21:59:28 smkumbar Exp $
#
# PrereqSpb.pl
# 
# Copyright (c) 2024, Oracle and/or its affiliates. 
#
#    NAME
#      PrereqSpb.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)
#    praaa       11/26/24 - Creation
# 
sub compareVersions {
    my ($version1, $version2) = @_;

    $version1 =~ s/^\s+|\s+$//g;
    $version2 =~ s/^\s+|\s+$//g;
    my @levels1 = split(/\./, $version1);
    my @levels2 = split(/\./, $version2);

    my $length = scalar(@levels1) > scalar(@levels2) ? scalar(@levels1) : scalar(@levels2);
    for (my $i = 0; $i < $length; $i++) {
        my $v1 = $i < scalar(@levels1) ? int($levels1[$i]) : 0;
        my $v2 = $i < scalar(@levels2) ? int($levels2[$i]) : 0;
        my $compare = $v1 <=> $v2;
        if ($compare != 0) {
            return $compare;
        }
    }
    return 0;
}

# compareHpmVersions(date1, date2), -1 means date2 < date1, 0 means equal, 1 means date2 > date1
sub compareHpmVersions {
    my ($s1, $s2) = @_;

	# Try parsing the dates using the "MM.YYYY" format
	my $date1 = Time::Piece->strptime($s1, "%m.%Y");
	my $date2 = Time::Piece->strptime($s2, "%m.%Y");
	my $compare = $date2 <=> $date1;
	if ($compare != 0) {
		return $compare;
	}		
	return 0;   
}


sub getPropValue {
	my ($propFileLoc,$key) = @_;	
	open(F,$propFileLoc) or die "Failed to open the file $!\n";

	while(<F>){
        ($name,$val)=m/(\w+)\s*=(.+)/;
        $properties{$name}=$val;
	}
	my $propVal = $properties{$key};
	close(F);
	return $propVal;	
}

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);
use Time::Piece;
use Time::Seconds;

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


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

my $omspatcherLog = File::Spec->catfile($oracleHome,"cfgtoollogs","omspatcher","SPB_PATCH","omspatcher_prereqs_$logTimeStamp.log");
my $fh;
open ($fh, '>', $omspatcherLog);

my $dbClient19C = File::Spec->catfile($oracleHome, "inventory", "distributions", "Database_Client_12.2.1.19.0.xml");
my $spbVersion;
my $min19cSpbVersion = "10.2024";
my $minRUSupported;
my $spbPropFile = File::Spec->catfile($patchLoc,"spbat-bundle.properties");
if (-e $spbPropFile) {
	$minRUSupported = getPropValue($spbPropFile,"SPB_MIN_OMS_RU_VERSION");
	$spbVersion = getPropValue($spbPropFile,"SPB_VERSION");
	print $fh "$datestring :: SPB Version :: $spbVersion\n";
	print $fh "$datestring :: Minimum RU Supported :: $minRUSupported\n";
}

if($minRUSupported eq "" || $spbVersion eq "") {
	die "Could not get the values from SPB properties file, Check the patch location passed\n";
}

my $result = compareHpmVersions($min19cSpbVersion, $spbVersion);

#spbVersion >= min19cSpbVersion
if($result >= 0) {
	if (!-e $dbClient19C) {
		print $fh "$datestring :: The holistic patch cannot be applied to OMS because the DB client in the system has not been upgraded to version 19c\n";
		print "\nERROR :: The holistic patch cannot be applied to OMS because the DB client in the system has not been upgraded to version 19c\n";
		close ($fh);
		die "Prereq failed in applying the holistic patch\n";
	}
}
#spbVersion < min19cSpbVersion	
else {
	if (-e $dbClient19C) {
		print $fh "$datestring :: The holistic patch cannot be applied to OMS because the DB client in the system has been upgraded to version 19c\n";
		print "\nERROR :: The holistic patch cannot be applied to OMS because the DB client in the system has been upgraded to version 19c\n";
		close ($fh);
		die "Prereq failed in applying the holistic patch\n";
	}
}

my $versionFile = File::Spec->catfile($oracleHome,"install","version.txt");
if (-e $versionFile) {
	my $envRUVersion = getPropValue($versionFile,"_omsRUVersion");
	print $fh "$datestring :: OMS RU Version :: $envRUVersion\n";

	my $compareVal = compareVersions($minRUSupported, $envRUVersion);

	if($compareVal <= 0){
		print $fh "$datestring :: The minimum Release Update (RU) check passed for applying the given holistic patch. Minimum OMS RU Version is $minRUSupported\n";
		print "\nSUCCESS :: The minimum Release Update (RU) check passed for applying the given holistic patch. Minimum OMS RU Version is $minRUSupported\n";
	}
	else{
		print $fh "$datestring :: The minimum Release Update (RU) check failed for applying the given holistic patch.\nRecommended action: If you wish to apply this holistic patch, ensure your OMS is patched with at least $minRUSupported\n";
		print "\nERROR :: The minimum Release Update (RU) check failed for applying the given holistic patch.\nRecommended action: If you wish to apply this holistic patch, ensure your OMS is patched with at least $minRUSupported\n";
		if(!$isAnalyze) {
			close ($fh);
			die "Prereq failed in applying the holistic patch\n";
		}	
	}
}
else{
	print $fh "$datestring :: The minimum Release Update (RU) check failed for applying the given holistic patch.\nRecommended action: If you wish to apply this holistic patch, ensure your OMS is patched with at least $minRUSupported\n";
	print "\nERROR :: The minimum Release Update (RU) check failed for applying the given holistic patch.\nRecommended action: If you wish to apply this holistic patch, ensure your OMS is patched with at least $minRUSupported\n";
	if(!$isAnalyze) {
			close ($fh);
			die "Prereq failed in applying the holistic patch\n";
	}	
}
close ($fh);