#!/bin/bash
#
# Startup script for Wowza Streaming Engine Manager
#
# chkconfig: - 80 20
# description: Wowza Streaming Engine Manager is an manager UI for the Wowza Streaming Engine
#
# Note: If you want to use update-rc.d to install a service when the system starts up,
# it should exactly have 3 ### in the front. No more, No less.
### BEGIN INIT INFO
# Provides:          WowzaStreamingEngineManager
# Required-Start:    $syslog $time $local_fs $remote_fs
# Required-Stop:     $syslog $time $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Wowza Streaming Engine Manager 4 Init Script
# Description:       Wowza Streaming Engine Manager 4 Init Script
### END INIT INFO

WMSMGRCOMMAND=${1}

FUNCTIONS_EXIST=false
if [ -f /etc/rc.d/init.d/functions ] ; then
     . /etc/rc.d/init.d/functions
     FUNCTIONS_EXIST=true
fi
if [ -f /etc/init.d/functions ] ; then
     . /etc/init.d/functions
     FUNCTIONS_EXIST=true
fi

if ! $FUNCTIONS_EXIST ; then
failure() {
  return 0
}
success() {
  return 0
}
fi

# define vars
RETVAL=0
WMSMGRBASE_NAME=WowzaStreamingEngineManager
WMSMGRCONFIG_SCRIPT="/usr/local/WowzaStreamingEngine/manager/bin/setmgrenv.sh"
WMSMGRDAEMON_CMD=/usr/bin/WowzaStreamingEngineManagerd
WMSMGRPID_FILE="/var/run/$WMSMGRBASE_NAME.pid"
WMSMGRLOCK_FILE="/var/run/$WMSMGRBASE_NAME"
if test -w "/var/lock/subsys" ; then
	WMSMGRLOCK_FILE="/var/lock/subsys/$WMSMGRBASE_NAME"
fi
SHUTDOWN_WAIT=20

[ -r "$WMSMGRCONFIG_SCRIPT" ] && . "$WMSMGRCONFIG_SCRIPT"


   testjava=`which ${_EXECJAVA} 2>/dev/null`
   if ! test -f "$testjava" ; then
	echo ""
	echo "ERROR: The Java command (${_EXECJAVA}) could not be found."
	echo "Search path: $PATH"
	echo "In most cases this problem can be fixed by adding a symbolic "
	echo "link to the Java command in the /usr/bin directory. "
	echo "To do this first execute the command \"which java\" to identify "
	echo "the full path to the Java executable. Next, create a symbolic "
	echo "link to this file with the command"
	echo "\"ln -sf [path-to-java] /usr/bin/java\" where [path-to-java] is "
	echo "the path returned by the \"which\" command."
	echo ""
	exit 0
   fi

#
start() {

    if [ -a $WMSMGRPID_FILE ]; then
	   rm -f $WMSMGRPID_FILE
    fi
    if [ -a $WMSMGRLOCK_FILE ]; then
 	   rm -f $WMSMGRLOCK_FILE
    fi
	echo -n -e $"\t$WMSMGRBASE_NAME: starting...\n"
	$WMSMGRDAEMON_CMD $WMSMGRCONFIG_SCRIPT $WMSMGRPID_FILE start >/dev/null 2>&1 &
	success "$WMSMGRBASE_NAME startup"
	touch $WMSMGRLOCK_FILE
	while [ ! -e "$WMSMGRPID_FILE" ]
        do
                sleep 1
        done	
	    RETVAL=0
    return 0
}

stop() {

   if [ -a $WMSMGRPID_FILE ]; then
	read kpid < $WMSMGRPID_FILE
	let kwait=$SHUTDOWN_WAIT
	count=0;
	if [ -n "$kpid" ]; then
		until [ `ps -p $kpid | grep -c $kpid` = '0' ] || [ $count -gt $kwait ]
		do
			echo -n -e "\tWaiting for process ($kpid) to exit...\n"
			kill -9 $kpid
			sleep 1
			let count=$count+1;
		done
	fi
	if [ $count -gt $kwait ]; then
		echo -n -e "\tkilling process ($kpid) which didn't stop after $SHUTDOWN_WAIT seconds\n"
		kill -9 $kpid
	fi

	rm -f $WMSMGRPID_FILE
	success "$WMSMGRBASE_NAME shutdown"
	RETVAL=0
   else
	RETVAL=0
	#echo -n -e  $"\t$WMSMGRBASE_NAME: not running\n"
   fi
   if [ -a $WMSMGRLOCK_FILE ]; then
      rm -f $WMSMGRLOCK_FILE
   fi
   return 0
}

localstatus() {
   if [ -f $WMSMGRLOCK_FILE ]; then
	read pid < $WMSMGRPID_FILE
	echo -n -e "\t$WMSMGRBASE_NAME started PID:($pid)\n"
	RETVAL=0
   else
	echo -n -e "\t$WMSMGRBASE_NAME stopped\n"
	RETVAL=3
   fi
}

# See how we were called.
case "$WMSMGRCOMMAND" in
start)
	stop
	start
	;;
stop)
	echo -n -e $"\t$WMSMGRBASE_NAME: stopping...\n"
	stop
	;;
status)
	localstatus
	;;
restart)
   	echo -n -e "\tRestarting $WMSMGRBASE_NAME ....\n"
	stop
	start
	;;
*)
	echo -n -e $"\tUsage: $WMSMGRBASE_NAME {start|stop|restart|status}\n"
	exit 1
esac

exit $RETVAL
