#!/bin/sh

cd ${0%${0##*/}}.

. ./setenv.sh


APP_HOME=.
NAME=inet-accounting
NAME_SHORT=accounting
ADMIN_PORT=1952
MEMORY=-Xmx256m

PARAMS="-Dboot.info=1 -Dapp.name=BGInetAccounting -Dlog.dir.path=log/ -Dlog4j.configuration=log4j-accounting.xml ${BGBILLING_SYSTEM_PROPERTIES}"

if [ -n "$BGBILLING_MEMORY" ]; then
  MEMORY=$BGBILLING_MEMORY
fi

if [ -z "$JAVA_VERSION" ]; then
  JAVA_VERSION="1.8"
fi

COMMON_PARAMS="-Dnetworkaddress.cache.ttl=3600 -Djava.net.preferIPv4Stack=true"
if [ "$JAVA_VERSION" = "1.8" ]; then
    COMMON_PARAMS="$COMMON_PARAMS -Djava.endorsed.dirs=${APP_HOME}/lib/endorsed:${JAVA_HOME}/lib/endorsed"
else
    COMMON_PARAMS="$COMMON_PARAMS --add-modules java.xml.bind,java.xml.ws"
fi

CLASSPATH=$APP_HOME:$APP_HOME/lib/ext/bgcommon-boot.jar

if [ "$1" = "start" ]; then
	nohup  ${JAVA_HOME}/bin/java ${COMMON_PARAMS} ${PARAMS} ${MEMORY} -Dadmin.port=$ADMIN_PORT -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot ru.bitel.bgbilling.kernel.application.server.Application ${NAME} > ./log/${NAME_SHORT}.out 2>&1 & echo $! > .run/${NAME_SHORT}.pid &
else
	if [ "$1" = "debug" ]; then
		#starting in debug mode
	    nohup  ${JAVA_HOME}/bin/java ${COMMON_PARAMS} ${PARAMS} ${MEMORY} -Dadmin.port=$ADMIN_PORT -cp ${CLASSPATH} -enableassertions -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5589,server=y,suspend=n ru.bitel.common.bootstrap.Boot ru.bitel.bgbilling.kernel.application.server.Application ${NAME} > ./log/${NAME_SHORT}.out 2>&1 & echo $! > .run/${NAME_SHORT}.pid
	else
		#execute command
		${JAVA_HOME}/bin/java ${COMMON_PARAMS} -Dadmin.port=$ADMIN_PORT -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot ru.bitel.bgbilling.kernel.application.server.Application ${NAME} "$@"
	fi
fi
