#!/usr/bin/env bash

classname="${groupId}.ServiceApplication"

command="server"

conf_dir=/data/conf

libs_dir=/data/lib

for jar in ${libs_dir}/*.jar; do
    classpath=$classpath:$jar
done

properties="-Djava.library.path=${libs_dir} "

properties="${properties} -Dcom.sun.management.jmxremote.rmi.port=1898"
properties="${properties} -Dcom.sun.management.jmxremote.port=1898"
properties="${properties} -Dcom.sun.management.jmxremote.ssl=false"
properties="${properties} -Dcom.sun.management.jmxremote.authenticate=false"
properties="${properties} -Djava.rmi.server.hostname=${HOST_IPADDR}"

if [ "${DEBUG_JAVA_SUSPEND}" == "" ]; then
    DEBUG_JAVA_SUSPEND="n";
fi

if [ "${DEBUG_JAVA}" == "true" ]; then
    properties="$properties -agentlib:jdwp=transport=dt_socket,server=y,suspend=${DEBUG_JAVA_SUSPEND},address=1044"
fi

echo "USING JAVA DEBUG $properties"

if [ -n "$ENV_CONF" ]; then
    echo "Using $ENV_CONF file."

    configFile=$conf_dir/configuration-$ENV_CONF.yml
else
    configFile=$conf_dir/configuration.yml
fi

cmd="java $properties -cp $classpath $classname $command $configFile"

echo ${cmd}

exec ${cmd}

