#!/bin/sh

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

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

#---------------------
#- Test 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-image-division-1.2.2-3937.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/ch.qos.logback-logback-classic-1.2.3.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.gui-imageoperation-commons-1.2.2-3937.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/fr.soleil.lib-CDMABox-1.2.4-7854.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/fr.soleil.lib-BasicUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-ApplicationUtilities-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-SwingUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeSwing-2.8.3-6203.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib.comete-CometeBox-1.3.6-7705.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/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.apache.commons-commons-lang3-3.5.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/fr.soleil.lib-AwtUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-SwingXUtilities-1.5.8-6201.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/fr.soleil.lib-DataConnectionManagement-1.4.9-6169.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.fatcow-fatcow-icons-1.0.0.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.miglayout-miglayout-3.7.4.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-colorchooser-0.0.1.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.hdfgroup-jarhdf5-1.10.3.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.apache.commons-commons-math3-3.5.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/com.jhlabs-filters-2.0.235.jar
CLASSPATH=$CLASSPATH:$JAR_ROOT/org.swinglabs-swing-worker-1.1.jar

#----------------
#- CRASH REPORT -
#----------------
SOFTWARE=$(basename "$0")
BASE_FILE=$ROOT_DIR/resources/dumps/$SOFTWARE-$(date +%y%m%d-%H%M%S) 
JVM_DUMP_OPTIONS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$BASE_FILE.hprof -XX:ErrorFile=$BASE_FILE.log"

#--------------
#- VM Options -
#--------------
JAVA_OPTIONS="-Xms256m -Xmx1024m -Dcom.sun.management.jmxremote=true"

#-----------
#- LOGGING -
#-----------
LOGBACK_FILE=$OPERATION_DIR/conf/imageoperation-logback.xml
LOG_PATH=$ROOT_DIR/resources/logs/$SOFTWARE
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

#-----------------------------------------
#- Start divide                          -
#-----------------------------------------
$JAVA_COMMAND $JVM_DUMP_OPTIONS $JAVA_OPTIONS $LOG_OPTIONS -cp $CLASSPATH \
-Djava.library.path=$NEXUS_PATH \
-Duser.language=en \
-Duser.country=US \
-DCDMA_RESOURCES_DIRECTORY=$CDMA_DICTIONARY_ROOT \
fr.soleil.image.division.ImageDivision $@
fi # end if [ $JBITS = 32 ] ... else
fi # end if [ "$JAVA_INSTALLED_VERSION" = "" ] ... elif [ $JAVA_INSTALLED_VERSION -ne $JAVA_VERSION ] ... else
