#!/bin/sh

# Variables
# ---------
# DO NOT MODIFY THIS FILE
# Instead set variables via a script YAMCS_HOME/bin/setenv.sh
#
# JMX           Set to 1 to allow remote JMX connections (jconsole).
#               (only temporarily for debugging purposes !)
#
# JAVA_OPTS     Java runtime options

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`
YAMCS_HOME=`cd "$PRGDIR/.." ; pwd`

ETC_DIR="$YAMCS_HOME/etc"
while [ $# -gt 0 ]; do
  if [[ $1 == "--etc-dir" ]]; then
    ETC_DIR="$2"
    shift
  else
    echo "Usage: $0 [--etc-dir DIR]"
    exit 1;
  fi
  shift
done

if [ ! -d "$ETC_DIR" ]; then
  echo "Configuration directory '$ETC_DIR' does not exist"
  exit 1;
fi

# Convert to absolute path
ETC_DIR=`cd $ETC_DIR; pwd`

# cd into YAMCS_HOME to support relative links in configuration files
cd "$YAMCS_HOME"

if [ -r bin/setenv.sh ]; then
  . bin/setenv.sh
fi

. bin/setclasspath.sh

if [ "$JMX" = 1 ]; then
  JMX_OPTS="-Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
fi

exec "$_RUNJAVA" $MODULE_OPTS $JAVA_OPTS $JMX_OPTS \
    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/ \
    -Djxl.nowarnings=true \
    -Djava.util.logging.config.file=$ETC_DIR/logging.properties \
    -Djacorb.home=$YAMCS_HOME \
    -Djavax.net.ssl.trustStore=$ETC_DIR/trustStore \
    org.yamcs.YamcsServer
