#! /bin/sh
### BEGIN INIT INFO
# Provides:          mythbackend
# Required-Start:    $syslog $remote_fs mysql
# Should-Start:      $time
# Required-Stop:     $syslog $remote_fs mysql
# Should-Stop:       $time
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: MythTV backend daemon
# Description:       Start the MythTV backend daemon
### END INIT INFO

MYTHBACKEND_BIN=/usr/bin/mythbackend
test -x $MYTHBACKEND_BIN || { echo "$MYTHBACKEND_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
MYTHBACKEND_CONF=/etc/sysconfig/mythbackend
test -r $MYTHBACKEND_CONF || { echo "$MYTHBACKEND_CONF not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

. $MYTHBACKEND_CONF

: ${MYTHBACKEND_USERID:=mythbackend}
: ${MYTHBACKEND_GROUPID:=video}
: ${MYTHBACKEND_HOME:=$(eval echo -n ~${MYTHBACKEND_USERID})}

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting MythTV backend"
	touch /var/log/mythtv/mythbackend.log
	chown ${MYTHBACKEND_USERID}.${MYTHBACKEND_GROUPID} /var/log/mythtv/mythbackend.log
	HOME=${MYTHBACKEND_HOME} /sbin/startproc \
		-u ${MYTHBACKEND_USERID} -g ${MYTHBACKEND_GROUPID} \
		-p /var/run/mythbackend.pid $MYTHBACKEND_BIN --daemon \
		--logpath /var/log/mythtv $MYTHBACKEND_OPTIONS
	rc_status -v
	;;
    stop)
	echo -n "Shutting MythTV backend"
	/sbin/killproc -TERM $MYTHBACKEND_BIN
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start

	rc_status
	;;
    force-reload)
	echo -n "Reload service MythTV backend"
	/sbin/killproc -HUP $MYTHBACKEND_BIN
	#touch /var/run/mythbackend.pid
	rc_status -v

	## Otherwise:
	#$0 try-restart
	#rc_status
	;;
    reload)
	# If it supports signaling:
	echo -n "Reload service MythTV backend"
	/sbin/killproc -HUP $MYTHBACKEND_BIN
	#touch /var/run/mythbackend.pid
	rc_status -v
	
	## Otherwise if it does not support reload:
	#rc_failed 3
	#rc_status -v
	;;
    status)
	echo -n "Checking for service MythTV backend"
	/sbin/checkproc $MYTHBACKEND_BIN
	rc_status -v
	;;
    probe)
	test $MYTHBACKEND_CONF -nt /var/run/mythbackend.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
