#!/bin/bash
#
#	/etc/rc.d/init.d/mythbackend
#
# Starts the mythbackend as a daemon
#
# chkconfig: 345 90 10
# description: Starts the mythbackend process as a daemon after the XWindows \
#              system is started, in runlevel 5. This allows scheduled \
#              recordings to occur without manual intervention.
# processname: mythbackend

# Copyright (c) by Michael Thomson <linux at m-thomson dot net>
# With thanks to Stu Tomlinson <stu at nosnilmot dot com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

# Default values to use if none are supplied in the config file.
# User who should start the mythbackend processes
MBE_USER="root"
# Directory holding the mythbackend binary
MBE_LOCATION="/usr/local/bin/"
# Name of mythbackend binary
MBE_PROG="mythbackend"
# Full path to mythbackend log file
MBE_LOGFILE="/var/tmp/mythbackend.log"

# Source function library.
. /etc/init.d/functions

# Source config file if available
if [ -f "/etc/sysconfig/mythbackend" ]; then
  . /etc/sysconfig/mythbackend
fi

test -x ${MBE_LOCATION}${MBE_PROG} || exit 0

RETVAL=0

#
#	See how we were called.
#

start() {
	# Check if mythbackend is already running
	if [ ! -f /var/lock/subsys/${MBE_PROG} ]; then
	    echo -n "Starting ${MBE_PROG}: "
	    # /usr/local/bin/mythbackend -d -l /some/log/file
	    daemon --user ${MBE_USER} ${MBE_LOCATION}${MBE_PROG} -d -l ${MBE_LOGFILE}
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${MBE_PROG}
	    echo
	fi
	return $RETVAL
}

stop() {
	echo -n "Stopping ${MBE_PROG}: "
	killproc ${MBE_LOCATION}${MBE_PROG}
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/${MBE_PROG}
	echo
        return $RETVAL
}


restart() {
	stop
	start
}	

reload() {
	restart
}	

status_at() {
 	status ${MBE_LOCATION}${MBE_PROG}
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
reload|restart)
	restart
	;;
condrestart)
	if [ -f /var/lock/subsys/${MBE_PROG} ]; then
	    restart
	fi
	;;
status)
	status_at
	;;
*)
	echo "Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $?
exit $RETVAL
