#!/usr/bin/env bash
#
# Gaia Sky launch script
#

# Get script path
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  GSDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$GSDIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
GSDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Detect installation to /usr/bin and /opt/gaiasky, in case the installation did not use and symlink
if [[ "$GSDIR" == "/usr/bin" ]]; then
  GSDIR="/opt/gaiasky"
fi

# Libraries path
GS_LIB_PATH="$GSDIR/lib"
GS_LIB_STRING="$GS_LIB_PATH/*"

# Check directories exist and are valid
if [ ! -d "$GSDIR" ]; then
  echo "Could not find Gaia Sky directory: $GSDIR"
  exit 1
fi
if [ ! -d "$GS_LIB_PATH" ]; then
  echo "Gaia Sky directory does not contain libraries: $GS_LIB_PATH"
  exit 1
fi

#
# Choose jre
#
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS, use bundled JRE
    if [[ -f "$GSDIR/../../PlugIns/jre.bundle/Contents/Home/jre/bin/java" ]]; then
        # Check default dmg jre location
        JAVA="$GSDIR/../../PlugIns/jre.bundle/Contents/Home/jre/bin/java"
    elif [[ -f "$GSDIR/../../PlugIns/jre.bundle/Contents/Home/bin/java" ]]; then
        # Check out this other location...
        JAVA="$GSDIR/../../PlugIns/jre.bundle/Contents/Home/bin/java"
    elif [[ "$JAVA_HOME" != "" ]]; then
        # Use $JAVA_HOME
        JAVA="$JAVA_HOME/bin/java"
    else
        # Just use default java installation, hopefully it's 11+
        JAVA="java"
    fi
elif [[ "$JAVA_HOME" != "" ]]; then
    # Use $JAVA_HOME
    JAVA="$JAVA_HOME/bin/java"
else
    # Use java in path, if any
    JAVA="java"
fi

# Escape
JAVA=$(printf %q "$JAVA")

#
# Settings.
#

# Memory
OPTS="$OPTS -Xms2g -Xmx5g"

# Parallel GC 
#OPTS="$OPTS -XX:+UseParallelGC"
# G1 GC
#OPTS="$OPTS -XX:+UseG1GC"
# Shenandoah GC (only in LTS)
#OPTS="$OPTS -XX:+UseShenandoahGC"
# ZGC
OPTS="$OPTS -XX:+UseZGC"

# GC debug only
#OPTS="$OPTS -verbose:gc -XX:+PrintGCDetails"
# Log GC to file
#OPTS="$OPTS -Xlog:gc*:file=gaiasky-gc.log:tags,uptime,time,level"
# JIT compiler debug only
#OPTS="$OPTS -XX:+PrintCompilation"
# Even more JIT compiler debugging - This produces a huge XML log file (for JITWatch)
#OPTS="$OPTS -XX:+UnlockDiagnosticVMOptions -XX:+TraceClassLoading -XX:+LogCompilation -XX:+PrintAssembly"

# macOS hack - needs a couple of args to be happy
if [[ "$OSTYPE" == "darwin"* ]]; then
	OPTS="$OPTS -XstartOnFirstThread -Djava.awt.headless=true"
fi

# Compact object headers
OPTS="$OPTS -XX:+UseCompactObjectHeaders"

# Assets location
OPTS="$OPTS -Dassets.location=\"$GSDIR/\""

# SimpleLogger defaults 
OPTS="$OPTS -Dorg.slf4j.simpleLogger.defaultLogLevel=warn -Dorg.slf4j.simpleLogger.showThreadName=false"

# Process name
OPTS="$OPTS -Dname=gaiasky"

#
# Run.
#

CMD="$JAVA $OPTS -cp \"$GS_LIB_STRING\" gaiasky.desktop.GaiaSkyDesktop $@"
# Uncomment to print run command
# echo "$CMD"

( cd "$GSDIR" && eval $CMD )
