@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
GOTO SCRIPT_BEGIN

REM ---------------------------------------------------
REM - Revision: 4.2                                   -
REM - Author: R. GIRARDOT / G. VIGUIER                -
REM - Date: November 14 2019                          -
REM - Purpose: Launch add                             -
REM ---------------------------------------------------
:NO_VALID_JAVA
@ECHO ******************************************************
@ECHO You have Java !JVER! !JBITS! bits installed.
@ECHO Please install Java %JAVA_VERSION% 64 bits!
@ECHO (Please read INSTALL.txt)
@ECHO ******************************************************
PAUSE
GOTO END

:NO_JAVA
@ECHO ******************************************************
@ECHO No Java detected.
@ECHO Please install Java %JAVA_VERSION% 64 bits!
@ECHO (Please read INSTALL.txt)
@ECHO ******************************************************
PAUSE
GOTO END

:SCRIPT_BEGIN
REM ----------------
REM - Java version -
REM ----------------
SET JAVA_VERSION=8

REM --------------------------------
REM - 2D Operation Root Java Paths -
REM --------------------------------
SET OPERATION_DIR=%~dp0
REM Find DOS name
SET TMP_DIR=""
for /d %%I in ("%OPERATION_DIR%") do (
  if %TMP_DIR% == "" (
    SET TMP_DIR=%%~sI
  ) else (
    SET TMP_DIR=%TMP_DIR%%%~sI
  )
)
SET OPERATION_DIR=%TMP_DIR%
REM Remove last "\"
if "%OPERATION_DIR:~-1%" == "\" (
  SET OPERATION_DIR=%OPERATION_DIR:~0,-1%
)
SET ROOT_DIR=%OPERATION_DIR:~0,-15%
SET JAR_ROOT=%ROOT_DIR%\lib

REM ------------------------------------
REM - Test Java 32/64 bits and version -
REM ------------------------------------
SET JAVA_PATH=""
REM SET JAVA_PATH=path\to\java\standalone\directory\bin
SET J64=false
SET JVER=-1
if "%JAVA_PATH%" == """" (
  REM Installed java case
  SET JAVA_COMMAND=javaw
  SET JAVA_MAIN_COMMAND=java
) else (
  REM Standalone java case
  SET JAVA_COMMAND="%JAVA_PATH%\javaw"
  SET JAVA_MAIN_COMMAND="%JAVA_PATH%\java"
)
%JAVA_MAIN_COMMAND% -version 2>&1 | find /i "64-Bit" > nul && if %errorlevel%==0 SET J64=true
SET JVEROK=false
for /f tokens^=1-5^ delims^=^"^.^-^_ %%g in ('%JAVA_MAIN_COMMAND% -fullversion 2^>^&1') do (
  REM %%g contains "java full version"
  REM %%h contains searched version for java >= 9
  REM %%i contains searched version for java <= 8
  SET JVER=%%h
  if %%h EQU %JAVA_VERSION% (
    SET JVEROK=true
    GOTO J64TEST
  )
  if %%h == 1 (
    SET JVER=%%i
    if %%i EQU %JAVA_VERSION% (
      SET JVEROK=true
      GOTO J64TEST
    )
  )
  if !JVER! == -1 GOTO NO_JAVA
  GOTO J64TEST
)
:J64TEST
if !J64!==true (
  SET JBITS=64
) else (
  SET JBITS=32
  GOTO NO_VALID_JAVA
)
if !JVEROK!==false GOTO NO_VALID_JAVA

:CLASSPATH
REM ------------------------
REM - NeXus/HDF5 Libraries -
REM ------------------------
SET NATIVE_DIR=%ROOT_DIR%\native\win64
SET PATH_DIR=%ROOT_DIR%\native\path64

REM ---------------------
REM - Setting CLASSPATH -
REM ---------------------
SET CLASSPATH="%NATIVE_DIR%"\*
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.gui-image-sum-and-mean-1.2.2-3937.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\ch.qos.logback-logback-classic-1.2.3.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.gui-imageoperation-commons-1.2.2-3937.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\ch.qos.logback-logback-core-1.2.3.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.slf4j-slf4j-api-1.7.25.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-CDMABox-1.2.4-7854.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.cdma-core-3.7.3-2378.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.cdma.plugin-nexus-soleil-3.7.3-2378.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.cdma.plugin-edf-soleil-3.7.3-2378.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-BasicUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-ApplicationUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CometeDefinition-2.6.5-6202.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-SwingUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CometeSwing-2.8.3-6203.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CometeBox-1.3.6-7705.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.cdma-utilities-3.7.3-2378.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CDMADataSource-1.3.2-6008.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.famfamfam-silk-1.3.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.tango-project-tango-icon-theme-0.8.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.commons-commons-lang3-3.5.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.felix-org.osgi.core-1.4.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.jdom-jdom2-2.0.5.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.cdma.engine-hdf-swmr-3.7.3-2378.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\commons-io-commons-io-2.0.1.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-AwtUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-SwingXUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-DataConnectionManagement-1.4.9-6169.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.fatcow-fatcow-icons-1.0.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.miglayout-miglayout-3.7.4.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-ExpressionAbstraction-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-ExpressionJEval-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-ImageJUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CometeAWT-2.2.2-6057.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.swinglabs-colorchooser-0.0.1.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.swinglabs-swingx-1.6.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\gov.nih-ij-1.45s.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.hdfgroup-jarhdf5-1.10.3.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.commons-commons-math3-3.5.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.jhlabs-filters-2.0.235.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.swinglabs-swing-worker-1.1.jar"

SET PATH=%PATH_DIR%;%PATH%

REM -----------------------------
REM - Setting JAVA VM OPTIONS   -
REM -----------------------------
SET JAVA_OPTIONS=-Xms256m -Xmx1024m
REM ------------------------
REM - Setting DUMP OPTIONS -
REM ------------------------
for /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set NOW=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%-%DTS:~8,2%-%DTS:~10,2%-%DTS:~12,2%
SET BASE_FILE=%ROOT_DIR%/resources/dumps/add-%NOW%
SET JVM_DUMP_OPTIONS=-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%BASE_FILE%.hprof -XX:ErrorFile=%BASE_FILE%.log

REM -----------------------
REM - Setting LOG OPTIONS -
REM -----------------------
SET LOGBACK_FILE=%OPERATION_DIR%\conf\imageoperation-logback.xml
SET LOG_PATH=%ROOT_DIR%/resources/logs/add
SET LOG_OPTIONS=-Dlogback.configurationFile=%LOGBACK_FILE% -DlogPath=%LOG_PATH%

REM -------------------
REM - User Parameters -
REM -------------------
SET CDMA_DICTIONARY_ROOT="%ROOT_DIR%\CDMA"

REM -----------------------------------------
REM - Start add                             -
REM -----------------------------------------
%JAVA_MAIN_COMMAND% ^
-Duser.language=en ^
-Duser.country=US ^
-Djava.library.path="%NATIVE_DIR%" ^
-DCDMA_RESOURCES_DIRECTORY=%CDMA_DICTIONARY_ROOT% ^
%JAVA_OPTIONS% ^
%LOG_OPTIONS% ^
%JVM_DUMP_OPTIONS% ^
-cp %CLASSPATH% ^
fr.soleil.image.sumandmean.ImageSum %*
GOTO END

:END
ENDLOCAL