#!/bin/bash
#
# Startup script for Wowza Streaming Engine
#
# chkconfig: - 80 20
# description: Wowza Streaming Engine is a media server
#
# 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:          WowzaStreamingEngine
# 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 4 Init Script
# Description:       Wowza Streaming Engine 4 Init Script
### END INIT INFO

WMCOMMAND=${1}

CloudPlatformFlag=0

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
WMSBASE_NAME=WowzaStreamingEngine
WMSCONFIG_SCRIPT="/usr/local/WowzaStreamingEngine/bin/setenv.sh"
WMSLICENSE_FILE="/usr/local/WowzaStreamingEngine/conf/Server.license"
CLOUD_INSTALL_SCRIPT="/usr/local/WowzaStreamingEngine/bin/CloudInstall.sh"
WMSDAEMON_CMD=/usr/bin/WowzaStreamingEngined
WMSPID_FILE="/var/run/$WMSBASE_NAME.pid"
WMSLOCK_FILE="/var/run/$WMSBASE_NAME"

if test -w "/var/lock/subsys" ; then
	WMSLOCK_FILE="/var/lock/subsys/$WMSBASE_NAME"
fi
SHUTDOWN_WAIT=20

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

if ! test -f "${WMSLICENSE_FILE}" ; then
        echo ""
        echo "ERROR: Missing license file: (${WMSLICENSE_FILE})"
        echo "You must first run Wowza Streamng Engine 4 in "
        echo "standalone mode to enter a license key. Execute the "
        echo "following commands to run in standalone mode:"
        echo ""
        echo "cd /usr/local/WowzaStreamingEngine/bin"
        echo "./startup.sh"
        echo ""
        exit 0
fi

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 $WMSPID_FILE ]; then
 	  rm -f $WMSPID_FILE
    fi
    if [ -a $WMSLOCK_FILE ]; then
 	  rm -f $WMSLOCK_FILE
    fi
   	echo -n -e $"\t$WMSBASE_NAME: starting...\n"
    if [[ 1 == $CloudPlatformFlag ]]; then
        bash -c "$CLOUD_INSTALL_SCRIPT" &
        wait $!
    fi
   	$WMSDAEMON_CMD $WMSCONFIG_SCRIPT $WMSPID_FILE start >/dev/null 2>&1 &
	success "$WMSBASE_NAME startup"
   	touch $WMSLOCK_FILE
	while [ ! -e "$WMSPID_FILE" ]
	do
		sleep 1
	done

    RETVAL=0
    return 0
}

stop() {

   if [ -a $WMSPID_FILE ]; then
	read kpid < $WMSPID_FILE
	$WMSDAEMON_CMD $WMSCONFIG_SCRIPT $WMSPID_FILE stop >/dev/null 2>&1 &
	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";
			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 $WMSPID_FILE
    rm -f $WMSLOCK_FILE
	success "$WMSBASE_NAME shutdown"
    RETVAL=0
   else
    RETVAL=0
	#echo -n $"$WMSBASE_NAME: not running"
   fi
   if [ -a $WMSLOCK_FILE ]; then
      rm -f $WMSLOCK_FILE
   fi
   echo
   return 0
}

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

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

exit $RETVAL
