@echo off
REM ######################################################################
REM #  Copyright (c) 2013 Oracle Corporation.  All rights reserved.
REM #
REM #  skkurapa  09/09/15 Add -Dweblogic.security.SSL.minimumProtocolVersion=TLSv1 in java call
REM #  skkurapa  10/03/13 Initial Creation
REM ######################################################################

setlocal enabledelayedexpansion

REM # Set the base path
set BASE=%~DP0%
set SCRIPTPATH=%~DP0%~NX0
set CMDNAME=omspatcher
set SPBFLAG=false
set ANALYZEFLAG=false
set APPLYFLAG=false
set JDKUPGRADEFLAG=false
set OSTYPE=
set analyze_flag=0

set FIRST=%1

REM # Get ORACLE_HOME from environment variable "ORACLE_HOME"
set OH=%ORACLE_HOME%

REM # Get OMSPATCHER_JRE_MEMoRY_OPTIONS from environment variable "OPATCH_JRE_MEMORY_OPTIONS"
set JRE_MEMORY_OPTIONS=%OMSPATCHER_JRE_MEMORY_OPTIONS%

REM # Get Middleware Home from environment variable "MW_HOME"
set MWH=%MW_HOME%

REM # Check for OPATCH_DEBUG env variable
set DEBUG=%OMSPATCHER_DEBUG%

REM # Get OPATCH_AUTO_CALL from environment variable "OPATCH_AUTO_CALL"
REM set OPATCH_AUTO_CALL=%OPATCH_AUTO_CALL%

set DEBUGVAL=false
if "%DEBUG%" == "TRUE" (set DEBUGVAL=true) else if "%DEBUG%" == "true" (set DEBUGVAL=true)

REM # Set CCR_INSTALL_DEFER_COLLECT=1
set CCR_INSTALL_DEFER_COLLECT=1

REM # Look for OPATCH_PLATFORM_ID
set PLATFORM=%OPATCH_PLATFORM_ID%

REM # Look for ORACLE_OCM_SERVICE
set OCM_SERVICE=
if NOT "%ORACLE_OCM_SERVICE%" == "" set OCM_SERVICE=-Docm.endpoint=%ORACLE_OCM_SERVICE%

REM # Preserve the PATH environment variable
set PATHENV=%PATH%

REM # If -oh is specified, use it to over-ride env. var. ORACLE_HOME
set getOH=0

REM # Look for OPATCH_NO_FUSER env. variable
set NO_FUSER=%OPATCH_NO_FUSER%

REM # All the OPatch properties to be passed in to Java
REM # Format for properties is abc=xyz
set PROPERTIES=

REM # If -jre or -jdk are specified, use it to launch opatch,
REM #   with -jdk > -jre.  And we expect there is a "bin/java" underneath
REM #   the value supplied
set getJRE=0
set getJDK=0
set getOUI=0
set fromOPatchAuto=0
set getPatchLoc=0
set getJdkUpgradeLoc=0

set JDK=
set JRE=
set OUI_LOCATION=
set FINAL_OUI_LOCATION=
set PATCH_LOCATION=
set JDK_UPGRADE_LOCATION=

REM # If -mw_home is specified, use it for the next of the session 
REM # after verification of its integrity
set getMWHOME=0

set PARAMS=%*


:FORLOOP

if "%1" == "" goto DONELOOP

if NOT "%getMWHOME%" == "1" goto CHECK1
set MWH=%1
set getMWHOME=0

:CHECK1
if NOT "%getOH%" == "1" goto CHECK2 
set OH=%1
set getOH=0

:CHECK2
if NOT "%getJRE%" == "1" goto CHECK3 
set JRE=%1
set getJRE=0

:CHECK3
if NOT "%getJDK%" == "1" goto CHECKFMW2
set JDK=%1
set getJDK=0

:CHECKFMW2
if NOT "%1" == "-mw_home" goto CHECK5
set getMWHOME=1

:CHECK5
if NOT "%1" == "-oh" goto CHECK6
set getOH=1

:CHECK6
if NOT "%1" == "-jre" goto CHECK7
set getJRE=1

:CHECK7
if NOT "%1" == "-jdk" goto CHECK8
set getJDK=1

:CHECK8
if NOT "%1" == "-oui_loc" goto CHECK9
set getOUI=1

:CHECK9
if /I NOT "%1" == "-spb_patch" goto CHECK10
set SPBFLAG=true
goto FORCHECK

:CHECK10
if /I NOT "%1" == "-analyze" goto CHECK11
set ANALYZEFLAG=true
set analyze_flag=1
goto FORCHECK

:CHECK11
if NOT "%1" == "apply" goto CHECK12
set APPLYFLAG=true
set getPatchLoc=1
goto FORCHECK

:CHECK12
if /I NOT "%1" == "-jdk_update" goto CHECK13
set JDKUPGRADEFLAG=true
set getJdkUpgradeLoc=1
goto FORCHECK

:CHECK13
if NOT "%getPatchLoc%" == "1" goto CHECK14
set PATCH_LOCATION=%1
set getPatchLoc=0

:CHECK14
if NOT "%getJdkUpgradeLoc%" == "1" goto FORCHECK
set JDK_UPGRADE_LOCATION=%1
set getJdkUpgradeLoc=0

:FORCHECK
shift
goto FORLOOP

:DONELOOP

if "%OH%" == "" (
   echo Oracle Home is not set through ORACLE_HOME environment variable. OMSPatcher cannot proceed!
   echo OMSPatcher returns with error code = 1
   set ERRORLEVEL=1
   goto OPATCHDONE
) 

set OH=%OH:"=%
if NOT EXIST %OH%\NUL (
   echo The Oracle Home %OH% could not be located. Please give proper Oracle Home.
   echo OMSPatcher returns with error code = 1
   set ERRORLEVEL=1
   goto OPATCHDONE
) 

if "%OH%" == "" goto CHECKFMW3
@REM Remove quotes

if NOT "%FMW_COMPONENT_HOME%" == "" goto SETMWHOME
if NOT EXIST %OH%\oracle_common\common\bin\setHomeDirs.cmd goto CHECKSUBDIR
if NOT EXIST %OH%\oracle_common\common\bin\setWlstEnv.cmd goto CHECKSUBDIR
set C_ORACLE_HOME=%OH%
set FMW_COMPONENT_HOME=%OH%
goto SETMWHOME

:CHECKSUBDIR
set SCRIPT_FILE_EXIST=1
for /D %%I in (%OH%\..\*) do (
	if NOT EXIST %%I\common\bin\setHomeDirs.cmd set SCRIPT_FILE_EXIST=0
	if NOT EXIST %%I\common\bin\setWlstEnv.cmd set SCRIPT_FILE_EXIST=0
	if !SCRIPT_FILE_EXIST! == 1 set FMW_COMPONENT_HOME=%%I
	if !SCRIPT_FILE_EXIST! == 1 goto CHECKCOMPONENTHOME
	set SCRIPT_FILE_EXIST=1
)


:CHECKCOMPONENTHOME
if NOT "%FMW_COMPONENT_HOME%" == "" goto SETMWHOME
if "%DEBUGVAL%" == "true" (
	echo OPatch was not able to set FMW_COMPONENT_HOME by itself.
)

set C_ORACLE_HOME=%OH%

:SETMWHOME
if NOT "%FMW_COMPONENT_HOME%" == "" set C_ORACLE_HOME=%FMW_COMPONENT_HOME%
@REM Calculate Middleware Home simply by moving up IFF User set Oracle Home
for %%? in ("%C_ORACLE_HOME%") do set C_MW_HOME=%%~f?
goto CHECKFMW4

:CHECKFMW3
@REM Determine the location of this script...
FOR %%i IN ("%SCRIPTPATH%") DO SET SCRIPTPATH=%%~fsi

@REM Calculate the ORACLE_HOME relative to this script...
FOR %%i IN ("%SCRIPTPATH%\..\..") DO SET C_ORACLE_HOME=%%~fsi

@REM Set the MW_HOME relative to the ORACLE_HOME...
FOR %%i IN ("%C_ORACLE_HOME%\..") DO SET C_MW_HOME=%%~fsi

if "%DEBUGVAL%" == "true" echo ORACLE_HOME is NOT set at OPatch invocation

:CHECKFMW4
if "%MWH%" == "" goto CHECKFMW5
for %%a in (%MWH%.) do set MWH=%%~dpfa

:CHECKFMW5
@REM Check if this is really a FMW home - WebTier and some others create fake
@REM Middleware homes. If TRUE Middleware Home, we are guaranteed of the
@REM presence of registry.dat
@REM Note: A TRUE Middleware Home can be established ONLY by the WebLogic installer

set FMW_ERROR=0
if NOT EXIST %C_MW_HOME%\domain-registry.xml goto CHECKFMW9

@REM Invoking the setHomeDirs.sh script to set the WebLogic environment
@REM Set the MW_HOME env variable temporarily for the following scripts
set SET_MW_HOME=%MW_HOME%
set MW_HOME=%C_MW_HOME%

if NOT EXIST %C_ORACLE_HOME%\oracle_common\common\bin\setHomeDirs.cmd goto CHECKFMW6
call "%C_ORACLE_HOME%\oracle_common\common\bin\setHomeDirs.cmd"

:CHECKFMW6
if "%DEBUGVAL%" == "true" echo WL_HOME is set by setHomeDirs.cmd script to %WL_HOME%

if "%WL_HOME%" == "" goto CHECKFMW6A

if NOT EXIST %WL_HOME%\NUL goto CHECKFMW6A

if NOT EXIST %WL_HOME%\common\bin\commEnv.cmd goto CHECKFMW6B

call %WL_HOME%\common\bin\commEnv.cmd
set TEMP_ORACLE_HOME=%ORACLE_HOME%
set ORACLE_HOME=%C_ORACLE_HOME%
IF EXIST %C_ORACLE_HOME%\oracle_common\common\bin\setWlstEnv.cmd CALL %C_ORACLE_HOME%\oracle_common\common\bin\setWlstEnv.cmd
set CURRENT_HOME=%COMMON_COMPONENTS_HOME%
IF EXIST %COMMON_COMPONENTS_HOME%\common\bin\setWlstEnvExtns.cmd CALL %COMMON_COMPONENTS_HOME%\common\bin\setWlstEnvExtns.cmd
set CURRENT_HOME=
set ORACLE_HOME=%TEMP_ORACLE_HOME%

goto CHECKFMW7

:CHECKFMW6A
set FMW_ERROR=-1
if "%DEBUGVAL%" == "true" (
   echo "OPatch was not able to set weblogic home. The Fusion Middleware home seems to be corrupted"
   echo OPatch will proceed only if JVM launcher found
)

:CHECKFMW6B
set FMW_ERROR=-2
if "%DEBUGVAL%" == "true" (
   echo "Fusion Middleware Home maybe corrupted (Common Env Script missing or Not executable)"
   echo OPatch will proceed only if JVM launcher found
)
goto CHECKFMW7

:CHECKFMW7
set MW_HOME=%SET_MW_HOME%

if "%MWH%" == "" set MWH=%C_MW_HOME% 
if "%OH%" == "" set OH=%C_ORACLE_HOME%

@REM We will use the JDK used by WebLogic unless user knows better and wants to override
if NOT "%JDK%" == "" goto CHECKFMW8
if NOT "%JRE%" == "" goto CHECKFMW8

set JDK=%JAVA_HOME%

:CHECKFMW8
REM set JRE_MEMORY_OPTIONS=%MEM_ARGS% %JVM_D64%
set JAVA_VM_OPTION=

if "%JAVA_VENDOR%" == "Oracle" goto CHECKFMW9
if "%JAVA_VENDOR%" == "HP" goto CHECKFMW9
if "%JAVA_VENDOR%" == "Sun" goto CHECKFMW9

set JAVA_VM_OPTION=-client

goto GETOUI1

:CHECKFMW9
REM # If Oracle Home not set, error out
if NOT "%OH%" == "" goto GETOUI1
if NOT EXIST %C_ORACLE_HOME%\oui (
   echo The Oracle Home %C_ORACLE_HOME% is not OUI based home. Please give proper Oracle Home.
   echo OPatch returns with error code = 1
   set ERRORLEVEL=1
   goto OPATCHDONE
)
set OH=%C_ORACLE_HOME%

:GETOUI1
if EXIST %OUI_LOCATION% goto INVOKEJAVA
set OUI_LOCATION_TEMP=%OH%\oui
set FINAL_OUI_LOCATION=%OH%\oui

if EXIST %OUI_LOCATION_TEMP% goto INVOKEJAVA

set FINAL_OUI_LOCATION=

@REM Determine the location of this script...
SET SCRIPTPATH=%BASE%
FOR %%i IN ("%SCRIPTPATH%") DO SET SCRIPTPATH=%%~fsi

@REM Calculate the ORACLE_HOME relative to this script...
FOR %%i IN ("%SCRIPTPATH%\..") DO SET C_ORACLE_HOME=%%~fsi

set OUI_LOCATION=%C_ORACLE_HOME%\oui
set FINAL_OUI_LOCATION=%C_ORACLE_HOME%\oui

if EXIST %OUI_LOCATION% goto INVOKEJAVA

set OUI_LOCATION=
set FINAL_OUI_LOCATION=

:INVOKEJAVA

REM # Use ORACLE_HOME to set Java CLASS_PATH
REM # default location
set JAVA=

REM # Use JDK if supplied
if NOT "%JAVA%" == "" goto CHECKJRE
if "%JDK%" == "" goto CHECKJRE
REM if NOT EXIST %JDK%\bin\java.exe goto CHECKJRE
set JAVA=%JDK%\bin\java.exe
set JAVA_HOME=%JDK%
REM if EXIST %JAVA% goto JAVATEST
goto JAVATEST
REM set JAVA=

:CHECKJRE
REM # Use JRE if supplied
if NOT "%JAVA%" == "" goto CHECKOHJRE
if "%JRE%" == "" goto CHECKOHJRE
REM if NOT EXIST %JRE%\bin\java.exe goto CHECKOHJRE
set JAVA=%JRE%\bin\java.exe
set JAVA_HOME=%JRE%
REM if EXIST %JAVA% goto JAVATEST
goto JAVATEST
REM set JAVA=

:CHECKOHJRE
REM # Use OH\jre\*, it should be 1.5 or above
if NOT "%JAVA%" == "" goto CHECKOHJDK
set JRE_HIGH=
if NOT EXIST %OH%\jre goto CHECKOHJDK
for /F "usebackq tokens=1" %%A in (`dir /ON /B %OH%\jre`) do set JRE_HIGH=%%A
if "%JRE_HIGH%" == "" goto CHECKOHJDK
set JRE_HIGH_FIRST=
set JRE_HIGH_SECOND=
for /F "tokens=1,2 delims=." %%A in ("%JRE_HIGH%") do set JRE_HIGH_FIRST=%%A
for /F "tokens=1,2 delims=." %%A in ("%JRE_HIGH%") do set JRE_HIGH_SECOND=%%B
if "%JRE_HIGH_FIRST%" LSS "1" goto CHECKOHJDK
if "%JRE_HIGH_SECOND%" LSS "5" goto CHECKOHJDK
set JAVA=%OH%\jre\%JRE_HIGH%\bin\java.exe
set JAVA_HOME=%OH%\jre\%JRE_HIGH%
if EXIST %JAVA% goto JAVATEST
set JAVA=

:CHECKOHJDK
REM # Check for jdk location inside OH
if NOT "%JAVA%" == "" goto CHECKJDK6
if NOT EXIST %OH%\jdk\bin\java.exe goto CHECKJDK6
set JAVA=%OH%\jdk\bin\java.exe
set JAVA_HOME=%OH%\jdk
if EXIST %JAVA% goto JAVATEST
set JAVA=

:CHECKJDK6
REM # Check for jdk6 from one level up of OH
if NOT "%JAVA%" == "" goto CHECKORAPARAM
if NOT EXIST %OH%\..\jdk6\bin\java.exe goto CHECKORAPARAM
set JAVA=%OH%\..\jdk6\bin\java.exe
set JAVA_HOME=%OH%\..\jdk6
if EXIST %JAVA% goto JAVATEST
set JAVA=

:CHECKORAPARAM
REM # Last option is to look inside oraparam.ini for JRE_LOCATION
if NOT "%JAVA%" == "" goto CHECKUPLEVELJRE
if NOT EXIST %FINAL_OUI_LOCATION%\oraparam.ini goto CHECKUPLEVELJRE
if "%DEBUGVAL%" == "true" echo "Looking for java in %FINAL_OUI_LOCATION%\oraparam.ini"
set JRE_LOCATION=
for /F "usebackq tokens=2 delims==" %%A in (`findstr "JRE_LOCATION=" %FINAL_OUI_LOCATION%\oraparam.ini`) do set JRE_LOCATION=%%A
if "%JRE_LOCATION%" == "" goto CHECKUPLEVELJRE
set ABS_PATH=
for /F "eol=\ tokens=1" %%A in ("%JRE_LOCATION%") do set ABS_PATH=%%A
if "%ABS_PATH%" == "" goto JAVAABSPATH
for /F "tokens=1,2 delims=:" %%B in ("%JRE_LOCATION%") do set ABS_PATH=%%B
if NOT "%ABS_PATH%" == "%JRE_LOCATION%" goto JAVAABSPATH
set JAVA=%FINAL_OUI_LOCATION%\bin\%JRE_LOCATION%\bin\java.exe
if EXIST %JAVA% goto JAVATEST
set JAVA=

:JAVAABSPATH
set JAVA=%JRE_LOCATION%\bin\java.exe
if EXIST %JAVA% goto JAVATEST
set JAVA=

:CHECKJDK6
if NOT EXIST %ORACLE_HOME%/jdk6 goto CHECKUPLEVELJRE
if 

:CHECKUPLEVELJRE
@REM Determine the location of this script...
SET SCRIPTPATH=%BASE%
FOR %%i IN ("%SCRIPTPATH%") DO SET SCRIPTPATH=%%~fsi

@REM Calculate the ORACLE_HOME relative to this script...
FOR %%i IN ("%SCRIPTPATH%\..") DO SET C_ORACLE_HOME=%%~fsi

if NOT "%JAVA%" == "" goto CHECKUPLEVELJDK
set JRE_HIGH=
if NOT EXIST %C_ORACLE_HOME%\jre goto CHECKUPLEVELJDK
for /F "usebackq tokens=1" %%A in (`dir /ON /B %C_ORACLE_HOME%\jre`) do set JRE_HIGH=%%A
if "%JRE_HIGH%" == "" goto CHECKUPLEVELJDK
set JRE_HIGH_FIRST=
set JRE_HIGH_SECOND=
for /F "tokens=1,2 delims=." %%A in ("%JRE_HIGH%") do set JRE_HIGH_FIRST=%%A
for /F "tokens=1,2 delims=." %%A in ("%JRE_HIGH%") do set JRE_HIGH_SECOND=%%B
if "%JRE_HIGH_FIRST%" LSS "1" goto CHECKUPLEVELJDK
if "%JRE_HIGH_SECOND%" LSS "5" goto CHECKUPLEVELJDK
set JAVA=%C_ORACLE_HOME%\jre\%JRE_HIGH%\bin\java.exe
set JAVA_HOME=%C_ORACLE_HOME%\jre\%JRE_HIGH%
if EXIST %JAVA% goto JAVATEST

:CHECKUPLEVELJDK
if NOT "%JAVA%" == "" goto JAVATEST
if NOT EXIST %C_ORACLE_HOME%\jdk\bin\java.exe goto JAVATEST
set JAVA=%C_ORACLE_HOME%\jdk\bin\java.exe
set JAVA_HOME=%C_ORACLE_HOME%\jdk
goto JAVATEST

REM # Java executable exists and has execute permission, exit otherwise
:JAVATEST
if NOT "%JAVA%" == "" goto JAVATEST1
echo calling omspatcher_jvm_discovery
CALL %BASE%\oms_child_scripts\omspatcher_jvm_discovery.bat
if NOT "%JAVA%" == "" goto JAVATEST1
echo Java could not be located. OPatch cannot proceed!
set ERRORLEVEL=1
goto OPATCHDONE

:JAVATEST1
if EXIST %JAVA% goto CALLOPATCH
if %FMW_ERROR% == -1 echo Fusion Middleware Home is corrupted (WebLogic Home is not found)!
if %FMW_ERROR% == -2 echo Fusion Middleware Home is corrupted (Common Env Script missing or Not executable)!
echo %JAVA% could not be located. OPatch cannot proceed!
set ERRORLEVEL=1
goto OPATCHDONE


REM echo.
REM echo.
REM echo Path to Java binary    %JAVA%
REM echo Classpath for java     %CP%
REM echo Oracle Home to be used %OH%
REM echo.
REM echo.

:CALLOPATCH
if NOT "%OUI_LOCATION%" == "" set CP=%OUI_LOCATION%\modules

if "%OUI_LOCATION%" == "" set CP=%OH%\oui\modules

set FUSION_TRUST_OPTION=
if NOT "%WL_HOME%" == "" (
   if EXIST %WL_HOME%\server\lib\fusion_trust.jks set FUSION_TRUST_OPTION=-Dweblogic.security.SSL.trustedCAKeyStore=%WL_HOME%\server\lib\fusion_trust.jks
)

:HANDLE_BASE_DIR
if "%BASE%" == "" goto HANDLE_OH_DIR
set LAST_CHAR=%BASE:~-1,1%
set TEMP_BASE=%BASE%
if "%LAST_CHAR%" == "\" set BASE=%TEMP_BASE:~0,-1%

:HANDLE_OH_DIR
if "%OH%" == "" goto HANDLE_MWH_DIR
set LAST_CHAR=%OH:~-1,1%
set TEMP_OH=%OH%
if "%LAST_CHAR%" == "\" set OH=%TEMP_OH:~0,-1%

:HANDLE_MWH_DIR
if "%MWH%" == "" goto HANDLE_WL_HOME_DIR
set LAST_CHAR=%MWH:~-1,1%
set TEMP_MWH=%MWH%
if "%LAST_CHAR%" == "\" set MWH=%TEMP_MWH:~0,-1%

:HANDLE_WL_HOME_DIR
if "%WL_HOME%" == "" goto HANDLE_COMMON_COMPONENT_HOME_DIR
set LAST_CHAR=%WL_HOME:~-1,1%
set TEMP_WL_HOME=%WL_HOME%
if "%LAST_CHAR%" == "\" set WL_HOME=%TEMP_WL_HOME:~0,-1%

:HANDLE_COMMON_COMPONENT_HOME_DIR
if "%COMMON_COMPONENT_HOME%" == "" goto HANDLE_OUI_LOCATION_DIR
set LAST_CHAR=%COMMON_COMPONENT_HOME:~-1,1%
set TEMP_COMMON_COMPONENT_HOME=%COMMON_COMPONENT_HOME%
if "%LAST_CHAR%" == "\" set COMMON_COMPONENT_HOME=%TEMP_COMMON_COMPONENT_HOME:~0,-1%

:HANDLE_OUI_LOCATION_DIR
if "%OUI_LOCATION%" == "" goto HANDLE_FMW_COMPONENT_HOME_DIR
set LAST_CHAR=%OUI_LOATION:~-1,1%
set TEMP_OUI_LOCATION=%OUI_LOCATION%
if "%LAST_CHAR%" == "\" set OUI_LOCATION=%TEMP_OUI_LOCATION:~0,-1%

:HANDLE_FMW_COMPONENT_HOME_DIR
if "%FMW_COMPONENT_HOME%" == "" goto ECHOOPATCHDEBUG
set LAST_CHAR=%FMW_COMPONENT_HOME:~-1,1%
set TEMP_FMW_COMPONENT_HOME=%FMW_COMPONENT_HOME%
if "%LAST_CHAR%" == "\" set FMW_COMPONENT_HOME=%TEMP_FMW_COMPONENT_HOME:~0,-1%

:VALIDATEARCH
if "%APPLYFLAG%" == "true" if "%SPBFLAG%" == "true" (
goto PROCESSARCH
) else (
	goto ECHOOPATCHDEBUG
)

:PROCESSARCH
set RESULT1=1
set list=AMD64 IA64 ARM64
set ARCH_TYPE=%PROCESSOR_ARCHITECTURE%
for %%a in (%list%) do (	
	if %%a == %ARCH_TYPE% set RESULT1=0
)
set OSTYPE=windows64

:JDKUPGRADESTEP
if "%APPLYFLAG%" == "true" (
	if "%SPBFLAG%" == "true" (
		CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\ArgsValidations.pl %*
			if NOT !ERRORLEVEL! == 0 (
				set ERRORLEVEL=1
				goto EXIT
			)
	
	if EXIST "%OH%\.omspatcher_storage\.patch_failure.properties" (
		echo OMSPatcher finds that previous patching session is not yet completed. Please refer log file for the previous session and execute the below command to complete the previous session. OMSPatcher can proceed to execute new operations only if previous session is completed successfully
		echo omspatcher resume
		echo OMSPatcher failed with error code 255
        set ERRORLEVEL=1
		goto EXIT
	)

	if EXIST "%OH%\predowntime_success" (
		echo "This command apply is not the supported operation in the online patching. Execute either ('lspatches', 'update', 'rollback deploy') command or check 'help' for more details."
		echo OMSPatcher failed with error code 255
        	set ERRORLEVEL=1
		goto EXIT
	)

	CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\PrereqSpb.pl %OH% %PATCH_LOCATION% %analyze_flag%
	if NOT !ERRORLEVEL! == 0 (
			goto EXIT
	)
	

	if "%ANALYZEFLAG%" == "false" (
		CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\PrePatchApply.pl %OH% %PATCH_LOCATION%
		if NOT !ERRORLEVEL! == 0 (
				echo [ERROR] The holistic patch is not compatible with the OMS.
				goto EXIT
		)
	)	
	
	if "%JDKUPGRADEFLAG%" == "true" (
		echo Argument -jdk_update is skipped, as this argument should be used only for AIX platforms
	)
	if EXIST "%OH%\.omspatcher_storage\temp" RMDIR /S /Q %OH%\.omspatcher_storage\temp				

	if NOT EXIST "%OH%\cfgtoollogs\omspatcher\SPB_PATCH" MKDIR "%OH%\cfgtoollogs\omspatcher\SPB_PATCH"
	if EXIST "%PATCH_LOCATION%\tools\jdk\generic\spb_jdk_info.json" (
			echo Running JDK update validations.....			
      		CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\JdkUpgradeValidations.pl %OH% %PATCH_LOCATION%\binary_patches\jdk\windows64 %OSTYPE% %PATCH_LOCATION%\tools\jdk\generic\spb_jdk_info.json %analyze_flag%			
			
		if NOT !ERRORLEVEL! == 0 (
		if "%ANALYZEFLAG%" == "false" (
			echo [ERROR] JDK Validation has failed
			if EXIST "%OH%\.omspatcher_storage\temp" RMDIR /S /Q %OH%\.omspatcher_storage\temp
			set ERRORLEVEL=1
			goto EXIT
		))
		if EXIST "%OH%\.omspatcher_storage\temp" RMDIR /S /Q %OH%\.omspatcher_storage\temp		
	) else (
		echo SPB Patch content is not as per standards...cannot find spbat-bundle.properties....Exiting
		echo OMSPatcher failed with error code 255
		set ERRORLEVEL=1
		goto EXIT
	)
	if EXIST "%OH%\opatch_update_temp" RMDIR /S /Q %OH%\opatch_update_temp
	CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\OpatchUpgrade.pl %OH% %PATCH_LOCATION% %analyze_flag%
	if NOT !ERRORLEVEL! == 0 (
		if EXIST "%OH%\.omspatcher_storage\opatch_unzip" RMDIR /S /Q %OH%\.omspatcher_storage\opatch_unzip
		set ERRORLEVEL=1
		goto EXIT
	)
	if EXIST "%OH%\.omspatcher_storage\opatch_unzip" RMDIR /S /Q %OH%\.omspatcher_storage\opatch_unzip
	if EXIST "%OH%\opatch_update_temp" RMDIR /S /Q %OH%\opatch_update_temp
	CALL %OH%\perl\bin\perl.exe %BASE%\..\..\oms\analyzeSpb.pl %OH% %PATCH_LOCATION% %OSTYPE%
		if !ERRORLEVEL! == 2  (
			if "%ANALYZEFLAG%" == "false" (
				if EXIST "%OH%\windows64_patchlist.txt" DEL %OH%\windows64_patchlist.txt
				echo OMSPatcher failed with error code 255
        		set ERRORLEVEL=1
                goto EXIT
			)
		)
		if NOT !ERRORLEVEL! == 0 (
			if NOT !ERRORLEVEL! == 2 (
				if EXIST "%OH%\windows64_patchlist.txt" DEL %OH%\windows64_patchlist.txt
				set ERRORLEVEL=1
				goto EXIT
			)
		)
		if EXIST "%OH%\windows64_patchlist.txt" DEL %OH%\windows64_patchlist.txt
		if "%ANALYZEFLAG%" == "true" (
			echo OMSPatcher succeeded.
			goto EXIT
		)
	))

:ECHOOPATCHDEBUG
set BASE=%BASE%\..\..

REM # We removed all unnecessary jars from classpath as we were getting issues related to jars in holistic patching
if "%SPBFLAG%" == "true" (
	set omsPatcherClassPath="%BASE%\jlib\oracle.omspatcher.classpath.windows.jar;%CP%\OraInstaller.jar;.\;.";
) else (
	set omsPatcherClassPath="%BASE%\jlib\oracle.omspatcher.classpath.windows.jar;%BASE%\..\oracle_common\jlib\rcucommon.jar;%BASE%\..\oracle_common\modules\oracle.jdbc_12.1.0\ojdbc7.jar;%BASE%\..\oracle_common\modules\internal\features\apache_commons_compress_lib.jar;%BASE%\..\oracle_common\modules\internal\features\apache_commons_cli_lib.jar;%BASE%\..\oracle_common\modules\oracle.odl_12.1.3\ojdl.jar;%CP%\OraInstaller.jar;.\;.";
)
if NOT "%DEBUGVAL%" == "true" goto CALLOPATCHNODEBUG
  echo %JAVA% %JAVA_VM_OPTION% %JRE_MEMORY_OPTIONS% -cp %omsPatcherClassPath% -DOMSPatcher.ORACLE_HOME=%OH% -DOMSPatcher.DEBUG=%DEBUGVAL% -Dos.platform.name=%OSTYPE% -Djdk.upgrade.loc=%JDK_UPGRADE_LOCATION% -DOMSPatcher.RUNNING_DIR=%BASE% -DOMSPatcher.MW_HOME=%MWH% -DOMSPatcher.WL_HOME=%WL_HOME% -DOMSPatcher.COMMON_COMPONENTS_HOME=%COMMON_COMPONENTS_HOME% -DOMSPatcher.OUI_LOCATION=%OUI_LOCATION% -DOMSPatcher.FMW_COMPONENT_HOME=%FMW_COMPONENT_HOME% -DOMSPatcher.OPATCH_CLASSPATH=%CLASSPATH% -DOMSPatcher.WEBLOGIC_CLASSPATH=%WEBLOGIC_CLASSPATH% -DOMSPatcher.CALLER=%CMDNAME% -DOMSPATCHER.JAVA_PATH=%JAVA%  -Dweblogic.security.TrustKeyStore=DemoTrust -Dweblogic.security.SSL.minimumProtocolVersion=TLSv1 %FUSION_TRUST_OPTION% %OCM_SERVICE% oracle/opatchauto/oms/OPatchAuto %PARAMS%
    
:CALLOPATCHNODEBUG
CALL %OH%/wlserver\server\bin\setWLSEnv.cmd

copy %BASE%\..\sysman\jlib\emConfig.jar  %BASE%\..\sysman\install\emConfig.jar
copy %BASE%\..\sysman\jlib\emCoreSDKImpl.jar %BASE%\..\sysman\install\emCoreSDKImpl.jar

copy %BASE%\oms\PatchCmds.pm %BASE%\..\bin\PatchCmds.pm

   %JAVA% %JAVA_VM_OPTION% %JRE_MEMORY_OPTIONS% -cp "%omsPatcherClassPath%" -DOMSPatcher.ORACLE_HOME="%OH%" -DOMSPatcher.DEBUG="%DEBUGVAL%" -Dos.platform.name=%OSTYPE% -Djdk.upgrade.loc=%JDK_UPGRADE_LOCATION% -DOMSPatcher.RUNNING_DIR="%BASE%" -DOMSPatcher.MW_HOME="%MWH%" -DOMSPatcher.WL_HOME="%WL_HOME%" -DOMSPatcher.COMMON_COMPONENTS_HOME="%COMMON_COMPONENTS_HOME%" -DOMSPatcher.OUI_LOCATION="%OUI_LOCATION%" -DOMSPatcher.FMW_COMPONENT_HOME="%FMW_COMPONENT_HOME%" -DOMSPatcher.OPATCH_CLASSPATH="%CLASSPATH%" -DOMSPatcher.WEBLOGIC_CLASSPATH="%WEBLOGIC_CLASSPATH%" -DOMSPatcher.CALLER="%CMDNAME%" -DOMSPATCHER.JAVA_PATH="%JAVA%"  -Dweblogic.security.TrustKeyStore=DemoTrust -Dweblogic.security.SSL.minimumProtocolVersion=TLSv1 %FUSION_TRUST_OPTION% %OCM_SERVICE% oracle/opatchauto/oms/OPatchAuto %PARAMS%

:OPATCHDONE
set SCRIPT_NAME=OMSPatcher
set RESULT=%ERRORLEVEL%
REM if "%FIRST%" == "lspatches" goto EXIT
if %ERRORLEVEL% == 0 goto SUCCEXIT
if %ERRORLEVEL% LSS 5 goto FAILEXIT
if %ERRORLEVEL% GTR 7 goto WARNEXIT
echo.
echo %SCRIPT_NAME% stopped on request.
set RESULT=0
goto EXIT

:WARNEXIT
if %ERRORLEVEL% GTR 8 goto FAILEXIT
echo.
echo %SCRIPT_NAME% completed with warnings.
set RESULT=0
goto EXIT

:FAILEXIT
echo.
echo %SCRIPT_NAME% failed with error code = %RESULT%
goto EXIT

:SUCCEXIT
echo.
echo %SCRIPT_NAME% succeeded.
goto EXIT

:EXIT
exit /b %RESULT%
