#!/bin/bash

##############################################################################
##
##  vertx start up script for UN*X
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and VERTX_OPTS to pass JVM options to this script.

# You can configure any property on VertxOptions or DeploymentOptions by setting system properties e.g.
# VERTX_OPTS="-Dvertx.options.eventLoopPoolSize=26 -Dvertx.options.deployment.worker=true"

JVM_OPTS="-XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0"

JMX_OPTS=""
# To enable JMX uncomment the following
# JMX_OPTS="-Dcom.sun.management.jmxremote -Dvertx.options.jmxEnabled=true"

# To enable remote debug port, uncomment the following
# JVM_DEBUG="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=18092"

# You can specify the path to the vertx cache directory. If not specified a ${KNOTX_HOME}/.vertx is used
# VERTX_CACHE_DIR=

# You can specify path to your custom logger configuration file. If not specified a ${KNOTX_HOME}/conf/logback.xml is used
# KNOTX_LOGBACK_CONFIG =

#################### DO NOT CHANGE BELOW ######################################

APP_NAME="knotx-launcher"
APP_BASE_NAME=`basename "$0"`

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
    echo "$*"
}

die ( ) {
    echo
    echo "$*"
    echo
    exit 1
}

# Detect MacOSX
darwin=false
if [[ "$OSTYPE" == "darwin"* ]]; then
    darwin=true
fi

# Attempt to set VERTX_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
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
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/.."
KNOTX_HOME="`pwd -P`"
cd "$SAVED"

CLASSPATH=${CLASSPATH}:${KNOTX_HOME}/conf:${KNOTX_HOME}/lib/*

# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
        # IBM's JDK on AIX uses strange locations for the executables
        JAVACMD="$JAVA_HOME/jre/sh/java"
    else
        JAVACMD="$JAVA_HOME/bin/java"
    fi
    if [ ! -x "$JAVACMD" ] ; then
        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
    fi
else
    JAVACMD="java"
    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$darwin" = "false" ] ; then
    MAX_FD_LIMIT=`ulimit -H -n`
    if [ $? -eq 0 ] ; then
        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
            MAX_FD="$MAX_FD_LIMIT"
        fi
        ulimit -n $MAX_FD
        if [ $? -ne 0 ] ; then
            warn "Could not set maximum file descriptor limit: $MAX_FD"
        fi
    else
        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
    fi
fi

# Split up the JVM_OPTS And VERTX_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts ( ) {
    JVM_OPTS=("$@")
}
eval splitJvmOpts $JVM_OPTS $JAVA_OPTS $JMX_OPTS $VERTX_OPTS $JVM_DEBUG

exec "$JAVACMD" \
    "${JVM_OPTS[@]}" \
    -Dlogback.configurationFile=${KNOTX_LOGBACK_CONFIG:-${KNOTX_HOME}/conf/logback.xml}\
    -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory\
    -Dknotx.home="$KNOTX_HOME"\
    -Dvertx.cacheDirBase=${VERTX_CACHE_DIR:-${KNOTX_HOME}/.vertx}\
    -Dvertx.cli.usage.prefix=knotx\
    -classpath "$CLASSPATH" \
    io.vertx.core.Launcher "$@"
