#!@SHELL@

if [ $# -lt 1 ]; then
    echo "usage: $0 <instance> [args...]"
    exit 1
fi

if [ ! $TANGO_HOST ] && [ -f @TSD_RC_FILE@ ]; then
    . @TSD_RC_FILE@
fi

JAVALIB=@CMAKE_INSTALL_PREFIX@/share/java
LOG_HOME=/var/tmp/TangoRestServer
LOG_LEVEL=ERROR
TANGO_REST_SERVER_JAR=$JAVALIB/RestServer.jar
LOGBACK=${TANGO_LOGBACK_SERVER:-@CMAKE_INSTALL_PREFIX@/share/tango/logback-server.xml}

JAVA_OPTS="-Xmx2G -server -Xshare:off -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_HOME/TangoRestServer.hprof -XX:-OmitStackTraceInFastThrow"

INSTANCE="$1"
shift

@TSD_JAVA_EXEC@ \
    $JAVA_OPTS \
    -Duser.dir=$LOG_HOME \
    -DTANGO_HOST=$TANGO_HOST \
    -DLOG_HOME=$LOG_HOME \
    -DLOG_LEVEL=$LOG_LEVEL \
    -Dlogback.configurationFile="$LOGBACK" \
    -jar $TANGO_REST_SERVER_JAR \
    "$INSTANCE" \
    org.tango.TangoRestServer \
    "$INSTANCE" \
    "$@"
