#!/bin/sh
#
# tvheadend	tvheadend tv streaming service
#
# chkconfig:	345 40 60
#
# description:	tvheadend tv streaming service
#
# processname:	tvheadend
#

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

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "tvheadend"
		exit 1
	fi
else
	exit 0
fi

# Set defaults
TVHEADEND_ARGS=""

# Get service config - may override defaults
[ -f /etc/sysconfig/tvheadend ] && . /etc/sysconfig/tvheadend

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/tvheadend ]; then
		msg_already_running "tvheadend"
		return
	fi

	msg_starting "tvheadend"
	daemon /usr/bin/tvheadend -f -u tvheadend -g video $TVHEADEND_ARGS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/tvheadend
}

stop() {
	if [ ! -f /var/lock/subsys/tvheadend ]; then
		msg_not_running "tvheadend"
		return
	fi

	# Stop daemons.
	msg_stopping "tvheadend"
	killproc tvheadend
	rm -f /var/lock/subsys/tvheadend
}

condrestart() {
	if [ ! -f /var/lock/subsys/tvheadend ]; then
		msg_not_running "tvheadend"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status tvheadend
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
