#! /bin/sh
#
# chkconfig: - 85 15
#
### BEGIN INIT INFO
# Provides: revproxy
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Phosphoresce Reverse Proxy Server
# Description: Phosphoresce Reverse Proxy Server
### END INIT INFO

PATH=/usr/sbin:/usr/bin:/sbin:/bin

LAUNCHER_DIR=/usr/local/revproxy/
LAUNCHER_EXEC=/usr/local/revproxy/launcher.sh
LAUNCHER_PID=/usr/local/revproxy/launcher.pid

case "$1" in
  start)
	if [ -f ${LAUNCHER_PID} ] ; then
		PIDNUM=`cat ${LAUNCHER_PID}`
		PIDCNT=`ps --pid ${PIDNUM} | grep -c java`
		if [ ${PIDCNT} -ge 1 ] ; then
			echo "Reverse Proxy Already Running"
		else
			cd ${LAUNCHER_DIR}
			${LAUNCHER_EXEC} ${LAUNCHER_PID}&
			echo "Reverse Proxy Started"
		fi
	else
			cd ${LAUNCHER_DIR}
			${LAUNCHER_EXEC} ${LAUNCHER_PID}&
			echo "Reverse Proxy Started"
	fi
	;;
  stop)
	if [ -f ${LAUNCHER_PID} ] ; then
		PIDNUM=`cat ${LAUNCHER_PID}`
		PIDCNT=`ps --pid ${PIDNUM} | grep -c java`
		if [ ${PIDCNT} -ge 1 ] ; then
			kill `cat ${LAUNCHER_PID}` ; sleep 1
			echo "Reverse Proxy Stopped"
		else
			echo "Reverse Proxy Not Running"
		fi
	else
		echo "Reverse Proxy Not Running"
	fi
	;;
  restart|force-reload)
	if [ -f ${LAUNCHER_PID} ] ; then
		PIDNUM=`cat ${LAUNCHER_PID}`
		PIDCNT=`ps --pid ${PIDNUM} | grep -c java`
		if [ ${PIDCNT} -ge 1 ] ; then
			kill `cat ${LAUNCHER_PID}` ; sleep 1
		fi
	fi
	cd ${LAUNCHER_DIR}
	${LAUNCHER_EXEC} ${LAUNCHER_PID}&
	echo "Reverse Proxy Restarted"
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
	;;
esac

:
