@echo off
REM ######################################################################
REM #  Copyright (c) 2013 Oracle Corporation.  All rights reserved.
REM #
REM #  sallam      06/02/22 - bug-34226753
REM #  kamlesku 08/10/2015 - Added omspatcher jar file in classpath -Bug - 21603279   
REM #  skkurapa 03/19/14 - Fix consistency in 'createkeys.cmd' name usage  
REM #  vganesan 02/07/14 - Fix Password hide on windows				
REM #  vganesan 11/28/13 - Add friendly wait remainders for weblogic processing time
REM #  vganesan 09/16/13   Move away from OPatch core
REM #  billi    05/30/13   Support environment ORACLE_HOME
REM #  billi    03/11/13   Create and support -oh, -location
REM ######################################################################

setlocal enabledelayedexpansion

@REM determine the location of this script
set base=%~dp0
for %%i in ("%base%") do set base=%%~fsi

REM # set the base path and binary name
REM #set OH=%ORACLE_HOME%\oms_home
set OH=%ORACLE_HOME%
set LOCATION=
set getOH=0
set getLocation=0
set getHelp=0

if "%1" == "" (set getHelp=1)

:FORLOOP
if "%1" == "" goto DONELOOP
if not "%getHelp%" == "1" goto OH
	goto DONELOOP
:OH
if not "%getOH%" == "1" goto LOCATION
REM #	set OH=%1\oms_home
        set OH=%1
	set getOH=0
:LOCATION
if not "%getLocation%" == "1" goto SETOH
	set LOCATION=%1
	set getLocation=0
:SETOH
if not "%1" == "-oh" goto SETLOCATION
	set getOH=1
:SETLOCATION
if not "%1" == "-location" goto SETHELP
	set getLocation=1
:SETHELP
if not "%1" == "-help" goto FORCHECK
	set getHelp=1
:FORCHECK
	shift
	goto FORLOOP
:DONELOOP

if "%getHelp%" == "1" (
	echo Usage:	
	echo       createkeys.bat [-help]
	echo       createkeys.bat -oh [OMS Platform home] -location [location to store the config and key files]
	echo                      -oh       OMS Platform home path
	echo                      -location Location to store the configuration and key files
	exit /b 0
)

REM # remove quotes
REM set OH=%OH:%"=%
if NOT EXIST %OH%\NUL  (
	echo ERROR: Invalid home. Please check and pass OMS platform home to -oh parameter.
	goto SET_RC
)

if NOT EXIST %LOCATION%\NUL  (
	echo ERROR: Invalid location. Please check location input and try again.
	goto SET_RC
)

if %LOCATION:~-1%==\ SET LOCATION=%LOCATION:~0,-1%

@REM SET MW_HOME
set MW_HOME=%OH%
set SCRIPTPATH=
set WLST_HOME=

if not exist %OH%\bin\emcli.bat goto ERROMS
	goto CHECK	
:ERROMS
	echo ERROR: Invalid home. Please check and pass OMS platform home to -oh parameter.
	goto SET_RC

@REM check
:CHECK
if not exist %MW_HOME%\domain-registry.xml (
	echo ERROR: Invalid home. Please check and pass OMS platform home to -oh parameter.
	goto SET_RC
)

%MW_HOME%\perl\bin\perl %MW_HOME%\OMSPatcher\wlskeys\runCreateKeys.pl %MW_HOME% %LOCATION%

SET RETURN_CODE=%ERRORLEVEL%

echo. 
echo %RETURN_CODE%
if %RETURN_CODE%==0 (
echo User configuration file created: %LOCATION%\config
echo User key file created: %LOCATION%\key
echo 'createkeys.bat' succeeded.
) else ( echo 'createkeys.bat' failed.)

IF DEFINED USE_CMD_EXIT (
  EXIT %RETURN_CODE%
) ELSE (
  EXIT /B %RETURN_CODE%
)

:SET_RC
EXIT /B %1
