File redhat.emby-server of Package emby-server-beta

#!/bin/sh
### BEGIN INIT INFO
# Provides:          emby-server
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:	 $local_fs $network $remote_fs
# Default-Start:     
# Default-Stop:      0 1 6
# Short-Description: control Emby server service
# Description:       This script controls Emby server service
#					 using start-stop-daemon
### END INIT INFO

# chkconfig: 2345 20 80
#The above indicates that the script should be started in levels 2, 3, 4, and 5, #that its start priority should be 20, and that its stop priority should be 80.
# Source function library.
. /etc/init.d/functions

# Define variables
NAME=emby-server
RETVAL=0
CONF_FILE=$NAME.conf
PKG_NAME=emby-server
LOCKFILE=/var/lock/subsys/$NAME
LOGFILE=/var/log/$NAME.log

# Source Emby server default configuration
. /etc/default/emby-server

# Source Emby user server configuration overrides
if [ -f /etc/$CONF_FILE ]; then
	. /etc/$CONF_FILE
else
	echo "/etc/${CONF_FILE} not found using default settings.";
fi

# Run as username
USER=$EMBY_USER

# Path for Emby server helper script
EMBYSERVER=${EMBY_DIR}/emby-server.sh

# Path to store PID file
PIDFILE=$EMBY_PIDFILE

start() {
	rh_status_q
	RETVAL=$?
	echo -n $"Starting $NAME daemon: "
	if [ $RETVAL -eq 3 ]; then
		#daemon --user $USER $EMBYSERVER start > $LOGFILE 2>&1 &
		daemon $EMBYSERVER start > $LOGFILE 2>&1 &
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch $LOCKFILE
		echo_success
		echo
	elif  [ $RETVAL -eq 0 ] || [ $RETVAL -eq 1 ] || [ $RETVAL -eq 2 ] || [ $RETVAL -eq 4 ]; then
		echo "emby-server is already running or you don't have enough privileges to start the service. "
		echo_failure
		echo
	fi
}

stop() {
	rh_status_q
	RETVAL=$?
	echo -n $"Stopping $NAME daemon: "
	if [ $RETVAL -eq 0 ] || [ $RETVAL -eq 1 ] || [ $RETVAL -eq 2 ]; then
		PID=`cat $PIDFILE`
		CPIDS=$(pgrep -P $PID)
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			sleep 2 && kill -KILL $CPIDS
			kill -TERM $CPIDS > /dev/null 2>&1
			kill -0 $PID >/dev/null 2>&1;
			CPIDS=$(pgrep -P $PID)
			RETVAL=$?
			if [ $RETVAL -eq 1 ]; then
				RETVAL=0
			fi
		else
			RETVAL=0
		fi
		[ $RETVAL -eq 0 ] && rm -f $LOCKFILE && rm -f $PIDFILE
		echo_success
		echo
	elif [ $RETVAL -eq 3 ] || [ $RETVAL -eq 4 ]; then
		echo "emby-server is not running or you don't have enough privileges to stop the service. "
		echo_failure
		echo
	fi
}


restart() {
	stop
	start
}

reload() {
	restart
}

force_reload() {
	restart
}

rh_status() {
	status -p $PIDFILE -l $LOCKFILE $NAME
}

rh_status_q() {
	rh_status >/dev/null 2>&1
}

case "$1" in
	start)
		start
	;;
	stop)
		stop
	;;
	restart)
		restart
	;;
	reload)
		restart
	;;
	force-reload)
		restart
	;;
	status)
		rh_status
	;;
	*)
		echo "Usage: /etc/init.d/emby-server {start|stop|status|restart|reload|force-reload}" >&2
		exit 2
	;;
esac

exit $?