#!/bin/bash

# NOTE: Here you can configure the JVM's built in JMX interface.
# See the "Server Management Console and Monitoring" chapter
# of the "User's Guide" for more information on how to configure the
# remote JMX interface in the [install-dir]/conf/Server.xml file.
export _EXECJAVA=java

if [ -d /Library/WowzaStreamingEngine ]
then
	# OS X
	export WMSMGR_HOME=/Library/WowzaStreamingEngine/manager
else
	# Linux
	export WMSMGR_HOME=/usr/local/WowzaStreamingEngine/manager

	ulimit -n 20000

fi
JMXOPTIONS=-Dcom.sun.management.jmxremote=true
#JMXOPTIONS="$JMXOPTIONs
#JMXOPTIONS="$JMXOPTIONS -Dcom.sun.management.jmxremote.authenticate=true"
#JMXOPTIONS="$JMXOPTIONS -Dcom.sun.management.jmxremote.ssl=false"
#JMXOPTIONS="$JMXOPTIONS -Dcom.sun.management.jmxremote.password.file=$WMSCONFIG_HOME/conf/jmxremote.password"
#JMXOPTIONS="$JMXOPTIONS -Dcom.sun.management.jmxremote.access.file=$WMSCONFIG_HOME/conf/jmxremote.access"
#JMXOPTIONS="$JMXOPTIONS -Djava.rmi.server.hostname=192.168.1.7"
BACKGRND=" > /dev/null"
RUNCMD="`which nohup` "
if [ "$WMSMGRCOMMAND" != "start" ]; then
        JMXOPTIONS=""
fi

rc=0
#while [ $rc -eq 0 ]
#do


# log interceptor com.wowza.wms.logging.LogNotify - see Javadocs for ILogNotify

CMD="$_EXECJAVA -Dcom.wowza.wms.ConfigURL=\"\" -Dcom.wowza.wms.ConfigHome=$WMSMGR_HOME -Djava.io.tmpdir=$WMSMGR_HOME/temp -Dlog4j.configuration=file://$WMSMGR_HOME/conf/winstone.properties -Djava.net.preferIPv4Stack=true -jar $WMSMGR_HOME/lib/wms-winstone-1.0.5-boot.jar --prefix="/enginemanager" --defaultWebApp="/enginemanager" --tempDirectory=$WMSMGR_HOME/temp  --webroot=$WMSMGR_HOME/temp --warfile=$WMSMGR_HOME/lib/WMSManager.war --httpPort=8088 --ajp13Port=-1 --directoryListings=false"

	if [ "$WMSMGRCOMMAND" = "start" ]; then
	   $RUNCMD $CMD $BACKGRND &
	else
  	   $CMD 
	fi
PID=$!
rc=$?

if ! [ -d /Library/WowzaStreamingEngine ]
then
	# Linux
	if [ "$WMSMGRCOMMAND" = "start" ]; then
		echo $! > ${WMSMGRPID_FILE}
		wait $PID
	fi
	if [ "$WMSMGRCOMMAND" = "start" ] ; then
		WMSMGRBASE_NAME=WowzaStreamingEngineManager
		WMSMGRLOCK_FILE="/var/run/$WMSMGRBASE_NAME"
		if test -w "/var/lock/subsys" ; then
	        	WMSMGRLOCK_FILE="/var/lock/subsys/$WMSMGRBASE_NAME"
		fi
		echo $WMSMGRLOCK_FILE > /tmp/enc.txt
		rm -f $WMSMGRLOCK_FILE
	fi
	serviceName=$WowzaMGRVersion 
#	service $serviceName stop >/dev/null
fi
#done
exit 0
