#!/bin/bash

##############################################################
#
# This is the startup script for Cumba DataBrowser on linux and Mac OS.
# 
#
##############################################################

OLD_PWD="$(pwd)"

# Get absolute path to script directory, resolving symlinks
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
  SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"

echo "Script directory: $SCRIPT_DIR"

BIN_DIR="${SCRIPT_DIR}"
JAVA_HOME="${BIN_DIR}/jre"

if [ -f "${BIN_DIR}/jre/Contents/Home/bin/java" ]
then
    JAVA_HOME="${BIN_DIR}/jre/Contents/Home"
fi
JAVA_BIN="${JAVA_HOME}/bin/java"

JAR_FILE="$(echo ${SCRIPT_DIR}/net.cumba.dataviewer.launch-*.jar)"


# To diagnose off-EDT Swing repaints, add
#     -Dnet.cumba.edt.check=warn      (or =strict to fail fast)
# to the java invocation below. It is OFF unless set: warn/strict replace the
# JVM RepaintManager and write an ERROR + stack trace per violation.

"${JAVA_BIN}" \
	--enable-native-access=ALL-UNNAMED         \
	--add-opens=java.base/java.nio=ALL-UNNAMED \
	-Duser.language=en     \
	-Duser.country=US      \
	-Duser.region=US       \
	-Dfile.encoding=UTF-8  \
	-Djava.util.logging.config.file="${BIN_DIR}/logging.properties" \
	-Dnet.cumba.librdata.jnr.native.libdir="${BIN_DIR}" \
	-Dnet.cumba.readstat.jnr.native.libdir="${BIN_DIR}" \
	-Dcorej.define.rules.dir="${BIN_DIR}/rules-define" \
	-XX:+UseShenandoahGC              \
	-XX:ShenandoahGCMode=generational \
	-XX:+UseCompressedOops            \
	-Xms512m  \
	-Xmx4g    \
	-jar "${JAR_FILE}"
