#!/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

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

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-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 3
	fi
	return 0
}

#
# Function that starts the daemon/service
#
do_start()
{
        checkPermissions
        if [ "$?" -ne "0" ]
        then
                return 1
        fi
        PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
        if [ "$PROC_COUNT" -ne "0" ]
        then
                return 0
        fi
        $DAEMON $DAEMON_ARGS >/dev/null 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]
        then
                echo "$DAEMON launched successfully"
                return 0
        else
                if [ $RETVAL -eq 8 ]
                then
                        echo "Error launching $DAEMON. License not found"
                        return 0
                else
                        if [ $RETVAL -eq 9 ]
                        then
                                echo "Error launching $DAEMON. License is invalid"
                                return 0
                        else
                                echo "Error launching $DAEMON. Unknown error"
                                return 0
                        fi
                fi
        fi
}

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
}
do_console()
{
	checkPermissions
	if [ "$?" -ne "0" ]
	then
		return 1
	fi
	PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
	if [ "$PROC_COUNT" -ne "0" ]
	then
		return 0
	fi
	$DAEMON $CONSOLE_ARGS
	if [ "$?" -ne "0" ]
	then
		return 1
	fi
	return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
	checkPermissions
	if [ "$?" -ne "0" ]
	then
		return 1
	fi
	ALLPIDS=`ps ax|grep $DAEMON|grep -v "grep"|sed "s/^[ \t]*\([0-9]*\).*/\1/"|tr "\n" " "`
	if [ "$ALLPIDS" = "" ]
	then
		return 0
	fi
	kill -TERM $ALLPIDS
	sleep 1
	PROC_COUNT=`ps ax|grep $DAEMON|grep -v "grep"|wc -l`
	if [ "$PROC_COUNT" -ne "0" ]
	then
		sleep 5
		kill -KILL $ALLPIDS
	fi
	return 0;
}

case "$1" in
	start)
		[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
		do_start
		if [ "$?" -ne "0" ]
		then
			[ "$VERBOSE" != no ] && log_end_msg 1
			exit 1
		else
			[ "$VERBOSE" != no ] && log_end_msg 0
			exit 0
		fi
		;;
	stop)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
		do_stop
		if [ "$?" -ne "0" ]
		then
			[ "$VERBOSE" != no ] && log_end_msg 1
			exit 1
		else
			[ "$VERBOSE" != no ] && log_end_msg 0
			exit 0
		fi
		;;
	status)
		[ "$VERBOSE" != no ] && log_daemon_msg "Status $DESC" "$NAME"
		do_status
		if [ "$?" -ne "0" ]
		then
				[ "$VERBOSE" != no ] && log_end_msg 1
				exit 1
		else
				[ "$VERBOSE" != no ] && log_end_msg 0
				exit 0
		fi
		;;
	start_console)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
		do_console
		if [ "$?" -ne "0" ]
		then
			[ "$VERBOSE" != no ] && log_end_msg 1
			exit 1
		else
			[ "$VERBOSE" != no ] && log_end_msg 0
			exit 0
		fi
		;;
	restart|force-reload)
		[ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
		do_stop
		if [ "$?" -ne "0" ]
		then
			[ "$VERBOSE" != no ] && log_end_msg 1
			exit 1
		else
			do_start
			if [ "$?" -ne "0" ]
			then
				[ "$VERBOSE" != no ] && log_end_msg 1
				exit 1
			else
				[ "$VERBOSE" != no ] && log_end_msg 0
				exit 0
			fi
		fi
		;;
	*)
		echo "Usage: $SCRIPTNAME {start|stop|status|restart|start_console|force-reload}" >&2
		exit 1
		;;
esac
