#!/bin/sh


### BEGIN INIT INFO
# Provides:       mythbackend
# Required-Start: $network $remote_fs
# Required-Stop:
# Should-Start:  mysql
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Start the MythTV backend
### END INIT INFO

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

unset MBE

# Check for existence of needed config file and read it
backend_CONFIG=/etc/sysconfig/mythbackend
test -r $backend_CONFIG || exit 6
. $backend_CONFIG


logdir=/var/log

if test -x /usr/bin/mythbackend
then
        MBE=/usr/bin/mythbackend
	basedir=/usr
	bindir=/usr/bin
	datadir=/tmp
elif test -x /usr/local/bin/mythbackend
then
        MBE=/usr/local/bin/mythbackend
	basedir=/usr/local
	bindir=/usr/local/bin
	datadir=/tmp
elif test -x /opt/mythtv/bin/mythbackend
then
        MBE=/opt/mythtv/bin/mythbackend
	basedir=/opt/mythtv
	bindir=/opt/mythtv/bin
	datadir=/opt/mythtv
fi


test "$MBE" || { echo "/usr/bin/mythbackend nor /usr/local/bin/mythbackend nor /opt/mythtv/bin/mythbackend exists"; rc_failed 5; rc_status -v; rc_exit; }


if test -z "$pid_file"
then
  pid_file=$datadir/mythbackend.`/bin/hostname`.pid
else
  case "$pid_file" in
    /* ) ;;
    * )  pid_file="$datadir/$pid_file" ;;
  esac
fi

mode=$1 # start or stop
# Safeguard (relative paths, core dumps..)
cd $basedir

case "$1" in
    start)
	# exit gracefully, if we are already running
	checkproc $MBE && echo -n "Starting service Myth Backend" && \
	rc_status -v && rc_exit

	
	test -x $MBE || { echo "$MBE does not exist"; rc_failed 5; rc_status -v; rc_exit; }

	echo "Starting service MythTV Backend"
	
	startproc $MBE -l $logdir/mythbackend.log 2>> $logdir/mythbackend.error.log &

	# Rmember status and be verbose
	rc_status -v
	;;

    stop)
	echo "Shutting down service MythTV Backend"
	killproc -p $pid_file -TERM $MBE

	# Remember status and be verbose
	rc_status -v
	;;

    try-restart)
	## Stop the service and if this succeeds (i.e. the 
	## service was running before), start it again.
	## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
	$0 status >/dev/null &&  $0 restart

	# Remember status and be quiet
	rc_status
	;;

    restart|force-reload)
	echo "Restarting service MythTV Backend"
	$0 stop
	$0 start

	rc_status
	;;

	reload)
	echo -n "Reloading service MythTV Backend"
	killproc -p $pid_file -HUP $MBE
	touch $pid_file
	rc_status -v
	;;
	
    check|status)
	echo -n "Checking for service MythTV BackendL: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
	checkproc $MBE
	rc_status -v
	;;

    *)
	echo "Usage: $0 {start|stop|status|reload|restart|try-restart|force-reload}"
	exit 1
	;;
esac
rc_exit



