#!/bin/sh

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

if [ -z "$JAVA_HOME" ]; then
  echo "The JAVA_HOME environment variable is not defined"
  echo "This environment variable is needed to run this program"
  exit 1
fi



BGBILLING_SERVER_DIR=.
CLASSPATH=${BGBILLING_SERVER_DIR}:${BGBILLING_SERVER_DIR}/lib/*

if [ ! -d "${BGBILLING_SERVER_DIR}/.run" ] ; then
    mkdir ${BGBILLING_SERVER_DIR}/.run
fi

if [ ! -d "${BGBILLING_SERVER_DIR}/lib/endorsed" ] ; then
    mkdir ${BGBILLING_SERVER_DIR}/lib/endorsed
    cp ${BGBILLING_SERVER_DIR}/lib/jax-ws/jaxws-api.jar ${BGBILLING_SERVER_DIR}/lib/endorsed
fi

if [ "$1" = "start" ]; then
	#starting
    nohup  ${JAVA_HOME}/bin/java -Djava.endorsed.dirs=./lib/endorsed -Djava.net.preferIPv4Stack=true -Dapp.name=BGBillingServer -Dnetworkaddress.cache.ttl=3600 -Xmx256m -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot bitel.billing.server.Server $1 $2 $3 > ./log/server.out 2>&1 & echo $! > .run/bgbilling.pid &
else
	if [ "$1" = "debug" ]; then
		#starting in debug mode
	    nohup  ${JAVA_HOME}/bin/java -Djava.net.preferIPv4Stack=true -enableassertions -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=5589,server=y,suspend=n -Dapp.name=BGBillingServer -Dnetworkaddress.cache.ttl=3600 -Xmx256m -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot bitel.billing.server.Server start $2 $3 > ./log/server.out 2>&1 & echo $! > .run/bgbilling.pid &
	else
		#execute command
		${JAVA_HOME}/bin/java -Djava.net.preferIPv4Stack=true -Dnetworkaddress.cache.ttl=3600 -Xmx256m -Djava.awt.headless=true -cp ${CLASSPATH} bitel.billing.server.Server $1 $2 $3
	fi
fi