#!/bin/sh

#---------------------------------------------------
#- Revision: 4.2                                   -
#- Author: R. GIRARDOT / G. VIGUIER                -
#- Date: November 14 2019                          -
#- Purpose: Launch Foxtrot                         -
#---------------------------------------------------
echo "Checking environment..."

#--------------------------------
#- ImageReducer Root Java Paths -
#--------------------------------
cd `dirname $0`
ROOT_DIR=`pwd`
cd -
JAR_ROOT=$ROOT_DIR/lib

#---------------------
#- Test Java version -
#---------------------
echo "Checking Java version..."
# Expected Java version
JAVA_VERSION=8
#JAVA_PATH=/path/to/java/standalone/directory/bin
if test -z $JAVA_PATH
then
# Installed java case
JAVA_COMMAND=java
else
# Standalone java case
JAVA_COMMAND=$JAVA_PATH/java
fi # end if test -z $JAVA_PATH .. else

# Installed Java version: since Java 9 -> version.X.Y
JAVA_INSTALLED_VERSION=`$JAVA_COMMAND -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'| cut -d'.' -f1`
if [ "$JAVA_INSTALLED_VERSION" = "1" ]
then
# Installed Java version: before Java 9 -> 1.version.X
JAVA_INSTALLED_VERSION=`$JAVA_COMMAND -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'| cut -d'.' -f2`
fi # end if [ "$JAVA_INSTALLED_VERSION" = "1" ]
JAVA_BITS=`$JAVA_COMMAND -version 2>&1 | grep 64-Bit`
if [ "$JAVA_BITS" = "" ]
then
  # 32bits JVM
  JBITS=32
else
  # 64bits JVM
  JBITS=64
fi
if [ "$JAVA_INSTALLED_VERSION" = "" ]
then
  # Java is not even installed
  echo "***********************************"
  echo "No java installed!"
  echo "Please install java $JAVA_VERSION 64 bits."
  echo "(Please read INSTALL.txt)"
  echo "***********************************"
# elif [ $((JAVA_INSTALLED_VERSION-$JAVA_VERSION)) -lt 0 ]
elif [ $JAVA_INSTALLED_VERSION -ne $JAVA_VERSION ]
then
  # Invalid Java version
  echo "*******************************************"
  echo "No valid java version found."
  echo "You have Java $JAVA_INSTALLED_VERSION $JBITS bits installed."
  echo "This software needs java $JAVA_VERSION 64 bits."
  echo "(Please read INSTALL.txt)"
  echo "*******************************************"
else
# Java version OK
if [ $JBITS = 32 ]
then
# 32bits JVM
echo "*******************************************"
echo "No valid java version found."
echo "You have Java $JAVA_INSTALLED_VERSION 32 bits installed."
echo "This software needs java $JAVA_VERSION 64 bits."
echo "(Please read INSTALL.txt)"
echo "*******************************************"
else
# 64bits JVM
echo "Setting up Java..."
#--------------------------
#- SOURCE COMMON ENV VARS -
#--------------------------
CLASSPATH=
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.gui-foxtrot-3.5.10-3979.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/ch.qos.logback-logback-classic-1.2.3.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.gui-imagereducer-3.5.10-3979.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-DockingVl-1.1.3-6181.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/ch.qos.logback-logback-core-1.2.3.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.slf4j-slf4j-api-1.7.25.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.famfamfam-silk-1.3.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.tango-project-tango-icon-theme-0.8.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.swinglabs-colorchooser-0.0.1.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.apache.commons-commons-lang3-3.5.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-BasicUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-SwingUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeDefinition-2.6.5-6202.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeSwing-2.8.3-6203.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-NumericalComputing-2.2.5-3965.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.ij.macro-MacroManager-0.1.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.gui-reduction-tools-3.5.10-3979.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.gui-data-export-3.5.10-3979.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-DockingCore-1.1.4-6178.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/commons-collections-commons-collections-3.2.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.apache.commons-commons-math3-3.5.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.vlsolutions-vldocking-3.0.2.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.fatcow-fatcow-icons-1.0.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-AwtUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.miglayout-miglayout-3.7.4.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-DataConnectionManagement-1.4.9-6169.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-SwingXUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-ExpressionAbstraction-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-ExpressionJEval-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-ImageJUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeAWT-2.2.2-6057.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.swinglabs-swingx-1.6.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/gov.nih-ij-1.45s.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.apache.maven-maven-plugin-api-2.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-ApplicationUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeBox-1.3.6-7705.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.cdma-core-3.7.3-2378.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.cdma.plugin-nexus-soleil-3.7.3-2378.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.cdma.plugin-edf-soleil-3.7.3-2378.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.cdma-utilities-3.7.3-2378.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CDMADataSource-1.3.2-6008.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-CDMABox-1.2.4-7854.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.jhlabs-filters-2.0.235.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.swinglabs-swing-worker-1.1.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.apache.felix-org.osgi.core-1.4.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.jdom-jdom2-2.0.5.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.cdma.engine-hdf-swmr-3.7.3-2378.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/commons-io-commons-io-2.0.1.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.hdfgroup-jarhdf5-1.10.3.jar

#----------------
#- CRASH REPORT -
#----------------
BASE_FILE=$ROOT_DIR/resources/dumps/Foxtrot-$(date +%y%m%d-%H%M%S) 

JVM_DUMP_OPTIONS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$BASE_FILE.hprof -XX:ErrorFile=$BASE_FILE.log"

#--------------
#- VM Options -
#--------------
#64 bits JVM can allow 2GB memory, and even more (as long as there is enough memory left)
XMX="2048m"
JAVA_OPTIONS="-Xms256m -Xmx$XMX -Dcom.sun.management.jmxremote=true"
LOOK_AND_FEEL="-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel"

#-----------
#- LOGGING -
#-----------
LOGBACK_FILE=$ROOT_DIR/resources/configuration/logback.xml
LOG_PATH=$ROOT_DIR/resources/logs/Foxtrot
LOG_OPTIONS="-Dlogback.configurationFile=$LOGBACK_FILE -DlogPath=$LOG_PATH"

#------------------------
#- NeXus/HDF5 Libraries -
#------------------------
NEXUS_PATH=$ROOT_DIR/native/linux

#-------------------
#- USER PARAMETERS -
#-------------------
CDMA_DICTIONARY_ROOT=$ROOT_DIR/CDMA
PERSPECTIVEDIRECTORY=$ROOT_DIR/resources/perspective
WORKINGPATH=$HOME
FITDIRECTORY=$ROOT_DIR/resources/fit
FORMULASDIRECTORY=$ROOT_DIR/resources/axisMacro
MASKDIRECTORY=$ROOT_DIR/resources/masks
IMAGEMACRODIRECTORY=$ROOT_DIR/resources/macros
USERMACRODIRECTORY=$ROOT_DIR/resources/userMacros
USERCONTEXTDATADIRECTORY=$ROOT_DIR
EXTERNALWORKERDIRECTORY=$ROOT_DIR/resources/tools
LASTUSEDPATHSFILE=$ROOT_DIR/resources/paths/lastUsedPaths-linux.properties
USERGUIDE=$ROOT_DIR/doc/Foxtrot-UserGuide.pdf
EXPORTPATH=$HOME
EXTRACTORPATH=$ROOT_DIR/python/extractor.sh
USERGUIDE=$ROOT_DIR/doc/Foxtrot-UserGuide.pdf
# -------------------------------------
# - Calibration highlight color setup -
# -------------------------------------
# CALIBRATION_COLOR represents the highlight color you want to use in detector calibration.
# It is expected to be encoded one of these 2 ways:
# CALIBRATION_COLOR=R,G,B
# or
# CALIBRATION_COLOR=R,G,B,A
# where:
# R,G,B and A are integers between 0 and 255 included.
# R represents red, G green, B blue, and A alpha.
# 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).
# If not set, default alpha value will be 120.
# If not set or badly configured, CALIBRATION_COLOR will take the value "0,0,0,120".
# 245,159,200 is pink
CALIBRATION_COLOR=245,159,200,100

#-----------------------------------------
#- Start Foxtrot                         -
#-----------------------------------------
echo "Launching Foxtrot..."
$JAVA_COMMAND $JVM_DUMP_OPTIONS $JAVA_OPTIONS $LOG_OPTIONS -cp $CLASSPATH \
-Djava.library.path=$NEXUS_PATH \
$LOOK_AND_FEEL \
-DCDMA_RESOURCES_DIRECTORY=$CDMA_DICTIONARY_ROOT \
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
fi # end if [ $JBITS = 32 ] ... else
fi # end if [ "$JAVA_INSTALLED_VERSION" = "" ] ... elif [ $JAVA_INSTALLED_VERSION -ne $JAVA_VERSION ] ... else
