#!/bin/sh

### BEGIN INIT INFO
# Provides:          evostreamms
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: EvoStream Media Server
# Description:       start/stop EvoStream Media Server
### END INIT INFO

# Author: EvoStream <support@evostream.com>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="EvoStream Media Server"
NAME=evostreamms
DAEMON=/usr/bin/$NAME
EVO_UID=`id -u evostreamd`
EVO_GID=`id -g evostreamd`
EVO_PID=/var/run/evostreamms/evostreamms.pid
EVO_CONFIGFILE=/etc/evostreamms/config.lua
GENERIC_ARGS="--uid=$EVO_UID --gid=$EVO_GID --pid=$EVO_PID $EVO_CONFIGFILE"
DAEMON_ARGS="--daemon $GENERIC_ARGS"
CONSOLE_ARGS=$GENERIC_ARGS
PIDFILE=$EVO_PID
SCRIPTNAME=/etc/init.d/$NAME
LOCKFILE=/var/lock/subsys/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

. /etc/rc.d/init.d/functions

checkPermissions()
{
	WANTED=`id -u root`
	CURRENT_USERNAME=`whoami`
	GOT=`id -u $CURRENT_USERNAME`
	if [ "$WANTED" -ne "$GOT" ]
	then
		echo "This script must be run as root"
		return 1
	fi
	return 0
}

#
# Function that starts the daemon/service
#
do_start()
{
	echo -n "Starting $DESC: "
	checkPermissions
	if [ "$?" -ne "0" ]
	then
		failure
		echo
		return 1
	fi
	PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
	if [ "$PROC_COUNT" -ne "0" ]
	then
		success
		echo
		return 0
	fi
	$DAEMON $DAEMON_ARGS >/dev/null 2>&1
	if [ "$?" -ne "0" ]
	then
		failure
		echo
		return 1
	fi
	touch $LOCKFILE
	success
	echo
	return 0
}

do_status()
{
        checkPermissions
        if [ "$?" -ne "0" ]
        then
                return 1
        fi
        PIDS=`pidof $NAME`
        if [ "$PIDS" = "" ]
        then
                echo "$NAME is not running"
	else
		echo "$NAME is runnning with PIDS "$PIDS
        fi
        return 0
}
#
# Function that starts the server in console mode
#
do_console()
{
	echo -n "Starting $DESC: "
	checkPermissions
	if [ "$?" -ne "0" ]
	then
		failure
		echo
		return 1
	fi
	PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
	if [ "$PROC_COUNT" -ne "0" ]
	then
		success
		echo
		return 0
	fi
	$DAEMON $CONSOLE_ARGS
	exit 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
	echo -n "Stopping $DESC: "
	checkPermissions
	if [ "$?" -ne "0" ]
	then
		failure
		echo
		return 1
	fi
	ALLPIDS=`ps ax|grep $DAEMON|grep -v "grep"|sed "s/^[ \t]*\([0-9]*\).*/\1/"|tr "\n" " "`
	if [ "$ALLPIDS" = "" ]
	then
		success
		echo
		return 0
	fi
	echo -n $" ($ALLPIDS)"
	kill -TERM $ALLPIDS
	COUNTER=5
	FORCE=1
	until [ $COUNTER -le 0 ]; do
		PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
		let COUNTER=COUNTER-1
		if [ "$PROC_COUNT" -eq "0" ]
		then
			COUNTER=0
			FORCE=0
		fi
		sleep 1
	done
	if [ "$FORCE" -ne "0" ]
	then
		kill -KILL $ALLPIDS
	fi
	rm -f $LOCKFILE
	success
	echo
	return 0;
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	status)
        	do_status
		;;
	start_console)
		do_stop
		do_console
		;;
	restart)
		do_stop
		do_start
		;;
	*)
		echo "Usage: $SCRIPTNAME {start|stop|status|restart|start_console}" >&2
		exit 1
		;;
esac
