@echo off
SETLOCAL

REM Save the original working directory
SET ORIGINAL_DIR=%CD%
SET ROOT_DIR=%CD%\saas-migration-tool
CD /D %ROOT_DIR%

set "jarFile=bin\CtmSaasMigrationTool-ctmconvert-jar-with-dependencies.jar"
set "fipsJar1=%EM_HOME%\classes\emapicli\bc-fips-1.0.1.jar"
set "fipsJar2=%EM_HOME%\classes\emapicli\bc-fips-1.0.2.jar"
set "fipsJar3=%EM_HOME%\classes\emapicli\bc-fips-1.0.2.5.jar"
set "fipsJar4=%EM_HOME%\classes\bc-fips-1.0.2.5.jar"
set "ctmemInfraJar=%EM_HOME%\classes\infra-9.0.jar"
set "ctmemCommonJar=%EM_HOME%\classes\common-9.0.jar"
set "ctmemContractsJar=%EM_HOME%\classes\contracts-9.0.jar"
set "ctmemSecurityJar=%EM_HOME%\classes\security-9.0.jar"
set "ctmemConfigurationJar=%EM_HOME%\classes\commons-configuration2-2.12.0.jar"
set "ctmemTextJar=%EM_HOME%\classes\commons-text-1.13.1.jar"


set command_args=-Dorg.apache.poi.ss.ignoreMissingFontSystem=true -Xmx8G -cp ".;%fipsJar1%;%fipsJar2%;%fipsJar3%;%fipsJar4%;%ctmemInfraJar%;%ctmemCommonJar%;%ctmemContractsJar%;%ctmemSecurityJar%;%ctmemConfigurationJar%;%ctmemTextJar%;%jarFile%" com.bmc.ctmconvert.onemigrate.MainKt %*

IF NOT EXIST "%jarFile%" (
    echo.
    echo "%CD%"
    echo The file "%jarFile%" was not found
    echo.
)

IF NOT EXIST "%ROOT_DIR%\JRE" (GOTO PATH_JAVA)

IF "%RUN_MY_JAVA%"=="" GOTO CONVERSION_JAVA
GOTO PATH_JAVA


:CONVERSION_JAVA
:: echo using Java from conversion
:: echo 64-bit...
"%ROOT_DIR%\JRE\jre_64\bin\java.exe" %command_args%
GOTO EXIT

:PATH_JAVA
echo using Java from PATH
java -version >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 goto NO_JAVA

::echo RUN_MY_JAVA = %RUN_MY_JAVA%
java.exe %command_args%
goto EXIT

:EXIT
SET RC=%ERRORLEVEL%
CD /D %ORIGINAL_DIR%
echo.
ENDLOCAL & set "RC=%RC%"
exit /B %RC%

:NO_JAVA
  echo.
  echo This utility requires Java Runtime Environment version 21 or higher.
  echo Make sure Java is installed and configured before rerunning the utility.
  echo.
  pause
  goto EXIT
