#!/bin/sh
#
# tvheadend    Start/Stop the hts tvheadend daemon.
#
# chkconfig: 345 90 60
# description: Tvheadend is a TV streaming server for Linux supporting
#              DVB-S, DVB-S2, DVB-C, DVB-T, ATSC, IPTV, and Analog video
#              (V4L) as input sources. It also comes with a powerful and
#              easy to use web interface both used for configuration and
#              day-to-day operations, such as searching the EPG and
#              scheduling recordings.

### BEGIN INIT INFO
# Provides: tvheadend
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Default-Start:  345
# Default-Stop: 90
# Short-Description: run tvheadend daemon
# Description: Tvheadend is a TV streaming server for Linux supporting
#              DVB-S, DVB-S2, DVB-C, DVB-T, ATSC, IPTV, and Analog video
#              (V4L) as input sources. It also comes with a powerful and
#              easy to use web interface both used for configuration and
#              day-to-day operations, such as searching the EPG and
#              scheduling recordings.
### END INIT INFO

[ -f /etc/sysconfig/tvheadend ] || { 
    [ "$1" = "status" ] && exit 4 || exit 6 
}

RETVAL=0
prog="tvheadend"
exec=/usr/local/bin/tvheadend
lockfile=/var/lock/subsys/tvheadend
sysconfig=/etc/sysconfig/tvheadend

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

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $sysconfig ] || exit 6
    echo -n $"Starting $prog: "
    daemon $exec $OPTIONS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
}

stop() {
    echo -n $"Stopping $prog: "
	if [ -n "`pidfileofproc $exec`" ]; then
		killproc $exec
		RETVAL=3
	else
		failure $"Stopping $prog"
	fi
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
}

restart() {
    stop
    start
}

reload() {
	echo -n $"Reloading $prog: "
	if [ -n "`pidfileofproc $exec`" ]; then
		killproc $exec -HUP
	else
		failure $"Reloading $prog"
	fi
	retval=$?
	echo
}

force_reload() {
	# new configuration takes effect after restart
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p /var/run/tvheadend.pid $prog
}

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


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

