#!/usr/bin/perl
# 
# $Header: opatchauto/oms/ArgsValidations.pl /st_opatchauto_pt-em13.5mos/2 2024/07/17 16:10:21 smkumbar Exp $
#
# ArgsValidations.pl
# 
# Copyright (c) 2023, 2024, Oracle and/or its affiliates. 
#
#    NAME
#      ArgsValidations.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    08/18/23 - Script to validate arguments when spb_patch is
#                           passed
#    smkumbar    08/18/23 - Creation
# 
use Cwd ;
use File::Spec; 
use File::Path;
use File::Copy;
use File::stat;
use File::Basename;
local $| =1; 

my $total = $#ARGV + 1;
#print "the total args passed are $total";
my $counter = 0;
@supportedArgs = ("-spb_patch", "-skipautopatch_addoms", "-verbose", "-silent" , "-analyze", "-help", "-property_file", "-custcertpath",
"apply", "-oh", "-jdk_update", "-jre", "-jdk", "-invptrloc", "-oui_loc", "-mw_home");
@skipArgs = ("-property_file", "-custcertpath", "apply", "-oh", "-jdk_update", "-jre", "-jdk", "-invptrloc", "-oui_loc", "-mw_home");
my $skipFlag=0;
my $analyzeFlag=0;

#Handling the corner case
if(@ARGV[$#ARGV] eq "-analyze"){
	$analyzeFlag=1;
}

foreach my $a(@ARGV) {
	$a = lc($a);
	#we are not validating the args like omspatcher.debug=true, which have = in them
	if(index($a, '=') == -1){
	#Checking if the incoming argument starts with hyphen
    	#result returns 0 if the argument starts with hyphen, -1 if not
	    my $result = rindex($a, '-', 0);
	    #Even though the skipFlag is set, if the argument starts with hiphen, then we should process it in else part
	    if($skipFlag && $result == -1){
        	$skipFlag=0;
		    $counter++;
	    }
	    else{
        	if (!(grep( /^$a$/, @supportedArgs))) {
	            print "The argument $a is not valid\n";
        	    die "Please check the arguments passed and try again\n";
       		 }
		if($result == -1){
        		$skipFlag=0;
		}
        	$counter++;
	        if($a eq "-property_file"){
        	    my $propFileLoc = @ARGV[$counter];
	            #print "the prop file loc is $propFileLoc";        
        	    if (!(-e $propFileLoc))
	            { 
        	        die "Property File/Location passed is invalid\n";
	            }
        	}
	        if($a eq "-custcertpath"){      
        	    my $custCertLoc = @ARGV[$counter];
	            #print "the cust cert loc is $custCertLoc";
        	    if (!(-e $custCertLoc))
            	{ 
                	die "Cust Cert Path passed is invalid\n";
	            }
        	}

		if($a eq "-analyze"){
			$analyzeFlag=1;
		}

	        if($a eq "apply"){        
        	    my $patchLoc = @ARGV[$counter];
		   if($patchLoc eq "-analyze"){
			my $tempCount = $counter+1;
			$patchLoc = @ARGV[$tempCount];
			$analyzeFlag=1;
		   }
        	    if (!(-e $patchLoc))
	            {
			if($analyzeFlag){ 
				print "The Patch Location passed is invalid\n";
        	        	die "Please use correct syntax \n Eg : omspatcher apply -analyze <Absolute path of Holistic Patch> -spb_patch\n";
			}
			else{
				print "The Patch Location passed is invalid\n";
				die "Please use correct syntax \n Eg : omspatcher apply <Absolute path of Holistic Patch> -spb_patch\n";
			}
	            }
        	}
        	if (grep( /^$a$/, @skipArgs)){
            	$skipFlag=1;
        	}
    	}
	}    
}
