#!/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 [ "$1" = "-estart" ]; then
	#starting
	nohup  ${JAVA_HOME}/bin/java -Djava.net.preferIPv4Stack=true -Dapp.name=BGScheduler -Xmx256m -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot bitel.billing.server.TaskExecuter $1 $2 $3 > ./log/scheduler.out 2>&1 & echo $! > .run/bgscheduler.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=BGScheduler -Xmx256m -cp ${CLASSPATH} ru.bitel.common.bootstrap.Boot bitel.billing.server.TaskExecuter -estart $2 $3 > ./log/scheduler.out 2>&1 & echo $! > .run/bgscheduler.pid &
	else
		#execute command
		${JAVA_HOME}/bin/java -Djava.net.preferIPv4Stack=true -cp ${CLASSPATH}: bitel.billing.server.TaskExecuter $1 $2 $3
	fi
fi