@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 Foxtrot                         -
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
@ECHO Checking environment...
REM ----------------
REM - Java version -
REM ----------------
SET JAVA_VERSION=8

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

REM ------------------------------------
REM - Test Java 32/64 bits and version -
REM ------------------------------------
@ECHO Checking Java version...
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=java
  SET JAVA_MAIN_COMMAND=java
) else (
  REM Standalone java case
  SET JAVA_COMMAND="%JAVA_PATH%\java"
  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 ------------------------
@ECHO You have Java !JVER! !JBITS! bits installed.
@ECHO Setting up Java...
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-foxtrot-3.5.10-3979.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\ch.qos.logback-logback-classic-1.2.3.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.gui-imagereducer-3.5.10-3979.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-DockingVl-1.1.3-6181.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%\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.swinglabs-colorchooser-0.0.1.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.commons-commons-lang3-3.5.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-BasicUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-SwingUtilities-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.comete-CometeSwing-2.8.3-6203.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-NumericalComputing-2.2.5-3965.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.ij.macro-MacroManager-0.1.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.gui-reduction-tools-3.5.10-3979.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.gui-data-export-3.5.10-3979.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-DockingCore-1.1.4-6178.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\commons-collections-commons-collections-3.2.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.commons-commons-math3-3.5.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.vlsolutions-vldocking-3.0.2.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.fatcow-fatcow-icons-1.0.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-AwtUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\com.miglayout-miglayout-3.7.4.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-DataConnectionManagement-1.4.9-6169.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-SwingXUtilities-1.5.8-6201.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-swingx-1.6.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\gov.nih-ij-1.45s.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\org.apache.maven-maven-plugin-api-2.0.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib-ApplicationUtilities-1.5.8-6201.jar"
SET CLASSPATH=%CLASSPATH%;"%JAR_ROOT%\fr.soleil.lib.comete-CometeBox-1.3.6-7705.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%\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%\fr.soleil.lib-CDMABox-1.2.4-7854.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 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%\org.hdfgroup-jarhdf5-1.10.3.jar"

SET PATH=%PATH_DIR%;%PATH%

REM -----------------------------
REM - Setting JAVA VM OPTIONS   -
REM -----------------------------
REM 64 bits JVM can allow 2GB memory, and even more (as long as there is enough memory left)
SET XMX=2048m
SET JAVA_OPTIONS=-Xms256m -Xmx%XMX% -XX:+UseG1GC

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/Foxtrot-%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=%ROOT_DIR%\resources\configuration\logback.xml
SET LOG_PATH=%ROOT_DIR%/resources/logs/Foxtrot
SET LOG_OPTIONS=-Dlogback.configurationFile=%LOGBACK_FILE% -DlogPath=%LOG_PATH%

REM -------------------
REM - User Parameters -
REM -------------------
SET CDMA_DICTIONARY_ROOT="%ROOT_DIR%\CDMA"
SET PERSPECTIVEDIRECTORY="%ROOT_DIR%\resources\Perspective"
SET FITDIRECTORY="%ROOT_DIR%\resources\fit"
SET FORMULASDIRECTORY="%ROOT_DIR%\resources\axisMacro"
SET MASKDIRECTORY="%ROOT_DIR%\resources\masks"
SET IMAGEMACRODIRECTORY="%ROOT_DIR%\resources\macros"
SET USERMACRODIRECTORY="%ROOT_DIR%\resources\userMacros"
SET USERCONTEXTDATADIRECTORY="%ROOTDIR%"
SET EXTERNALWORKERDIRECTORY="%ROOT_DIR%\resources\tools"
SET LASTUSEDPATHSFILE="%ROOT_DIR%\resources\paths\lastUsedPaths-windows.properties"
SET USERGUIDE="%ROOT_DIR%\doc\Foxtrot-UserGuide.pdf"
SET EXTRACTORPATH="%ROOT_DIR%\python\extractor.bat"
SET WORKINGPATH=C:
SET EXPORTPATH=C:
REM -------------------------------------
REM - Calibration highlight color setup -
REM -------------------------------------
REM CALIBRATION_COLOR represents the highlight color you want to use in detector calibration.
REM It is expected to be encoded one of these 2 ways:
REM CALIBRATION_COLOR=R,G,B
REM or
REM CALIBRATION_COLOR=R,G,B,A
REM where:
REM R,G,B and A are integers between 0 and 255 included.
REM R represents red, G green, B blue, and A alpha.
REM For alpha, 0 means totally transparent (i.e. invisible) and 255 means totally opaque (i.e. you won't see any pixel color under it).
REM If not set, default alpha value will be 120.
REM If not set or badly configured, CALIBRATION_COLOR will take the value "0,0,0,120".
REM 245,159,200 is pink
SET CALIBRATION_COLOR=245,159,200,100

REM -----------------------------------------
REM - Start Foxtrot                         -
REM -----------------------------------------
@ECHO Launching Foxtrot...
START "Foxtrot" %JAVA_COMMAND% ^
-Djava.library.path="%NATIVE_DIR%" ^
-DCDMA_RESOURCES_DIRECTORY=%CDMA_DICTIONARY_ROOT% ^
%JAVA_OPTIONS% ^
%LOG_OPTIONS% ^
%JVM_DUMP_OPTIONS% ^
-cp %CLASSPATH% ^
fr.soleil.fusion.image.Foxtrot ^
WorkingPath=%WORKINGPATH% ^
ExportPath=%EXPORTPATH% ^
ExtractorPath=%EXTRACTORPATH% ^
ImageMacroDirectory=%IMAGEMACRODIRECTORY% ^
UserMacroDirectory=%USERMACRODIRECTORY% ^
UserContextDataDirectory=%USERCONTEXTDATADIRECTORY% ^
MaskDirectory=%MASKDIRECTORY% ^
PerspectiveDirectory=%PERSPECTIVEDIRECTORY% ^
FitDirectory=%FITDIRECTORY% ^
FormulasDirectory=%FORMULASDIRECTORY% ^
ExternalWorkerDirectory=%EXTERNALWORKERDIRECTORY% ^
LastUsedPathsFile=%LASTUSEDPATHSFILE% ^
UserGuide=%USERGUIDE% ^
ContextModificationInMacrosEnabled=true ^
DisplayImageLoading=false ^
CalibrationColor=%CALIBRATION_COLOR% ^
FitEnabled=true ^
ColorsEnabled=false ^
Language=EN ^
InnerRoiForDynamicMacro=true
GOTO END

:END
ENDLOCAL