#!/usr/local/bin/perl
# 

use Cwd;
use File::Spec; 
use File::Copy;
use File::Path;
$platform = $^O;
$isWindow=false;
$delimiter=":";

#print("platform s:$platform");

if(( $^O =~ "Windows" ) || ($^O =~ "MSWin32")){  
  $isWindow=true;
  $delimiter=";";
}

sub usage()
{
	print "\nUsage createLCMUser.pl -oh <absolute path of ORACLE HOME home> [-silent -property_file <property_file path> ] \n\n";
	exit 1;
}

sub help()
{

        print "\nUsage createLCMUser.pl -oh <absolute path of ORACLE HOME home> [-silent -property_file <property_file path>] \n\n";
        print "-oh        Absolute path of middleware home. \n";
        print "-silent        This will run utility in silent mode, all inputs will be read from property file.\n";
        print "-property_file  Path of the property file. This option is mandatory if -silent is used.\n\n";
        exit 1;
}

#sub routines starts here.
sub validateArgs{

## Read arguments and initialize global variables.
while ($ARGV[$args]  ){
		$option = $ARGV[$args];
		$option =~ s/\s+//g;
		if($option =~ /^-oh$/) {
			   $ARGV[$args + 1] =~ s/\s+//g;
		   if($ARGV[$args+1] =~ /^-/) {
			   print "Invalid Oracle Home specified.\n";
			   $args++;
			   next;
		 }
		   $oh = $ARGV[++$args] ;
		   chomp $oh;
		   last if($oh ne "");
	   }
          elsif ($option =~ /^-help$/){
                           help();
           }

		$args++;
	   
}
## Validate Conditions. In case of Silent, propertyFiles must be supplied.
  
	if ($oh eq ""){
		print "\nError: Oracle Home value is not passed. Provide a valid Oracle Home.\n";
		usage();
		exit 1;	
	}

	$oh = File::Spec->catfile($oh);
	if ($isWindow eq true){
		$oh=~tr!\\!/!;
	}
	$javaHome=File::Spec->catfile($oh,"oracle_common","jdk","bin");
	if ($isWindow eq true){
		$javaPath=File::Spec->catfile($javaHome,"java.exe");
	}else{
		$javaPath=File::Spec->catfile($javaHome,"java");
		}

	$libPath=File::Spec->catfile($oh,"sysman","jlib");
	$lcmLibPath=File::Spec->catfile($oh,"OMSPatcher","jlib");
	
	if(!(-e $oh ) || ! (-e $javaPath )  || ! (-e $libPath ) ){
		print "$oh location is not valid or not exist. Provide a valid Oracle Home.\n";
    	exit 1;
	}	
}

sub createLCMUser{
	my $emConfigJarPath;
	my $emCoreSDKImplJarPath;
	my $emlcmuserJarPath;
	my $ojdbcJarPath;
	my $command;

	my $emOpatchUnixJarPath=File::Spec->catfile($oh,"OPatch","jlib","oracle.opatch.classpath.unix.jar");
	my $emOpatchWindowJarPath=File::Spec->catfile($oh,"OPatch","jlib","oracle.opatch.classpath.windows.jar");
	my $emConfigJarPath=File::Spec->catfile($libPath,"emConfig.jar");
	my $emCoreSDKImplJarPath=File::Spec->catfile($libPath,"emCoreSDKImpl.jar");
	my $emCoreClientJarPath=File::Spec->catfile($lcmLibPath,"emcore_client.jar");
	my $emlcmuserJarPath=File::Spec->catfile($lcmLibPath,"emlcmuser.jar");
	my $ojdbcJarPath=File::Spec->catfile($oh,"oui","em","ojdbc8.jar");
	
	my $javaUnixArgs = " -cp ". "$emOpatchUnixJarPath$delimiter$emConfigJarPath$delimiter$emCoreClientJarPath$delimiter$emCoreSDKImplJarPath$delimiter$emlcmuserJarPath$delimiter$ojdbcJarPath";
	my $javaWinArgs = " -cp ". "$emOpatchWindowJarPath$delimiter$emConfigJarPath$delimiter$emCoreClientJarPath$delimiter$emCoreSDKImplJarPath$delimiter$emlcmuserJarPath$delimiter$ojdbcJarPath";

	my $className = "oracle.opatchauto.oms.CreateLCMUser";
	
	if ($isWindow eq true){
		$command = "$javaPath ". $javaWinArgs ." " . $className ." " . join(' ', @ARGV);
	}else{
		$command = "$javaPath ". $javaUnixArgs ." " . $className ." " . join(' ', @ARGV);
	}
	#print "command $command \n";
	my $ret = system($command);
}



###Starting the main code here 

$args = 0 ;
$oh="";
$javaPath="";
$libPath="";
validateArgs();
createLCMUser();

