#! /bin/sh
#
# tvheadend	Start/Stop the Tvheadend streaming server
#
# chkconfig:	- 80 10
# description:	TV streaming server
# processname:	tvheadend
# pidfile:	/var/run/tvheadend.pid

WITHOUT_RC_COMPAT=1

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

SourceIfNotEmpty /etc/sysconfig/tvheadend

RETVAL=0
PIDFILE=/var/run/tvheadend.pid
LOCKFILE=/var/lock/subsys/tvheadend

DAEMONARGS="-f -u _hts -g _hts -c ${CONFDIR:-/etc/tvheadend} $EXTRAARGS"

start()
{
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _hts -- tvheadend $DAEMONARGS
    RETVAL=$?
    return $RETVAL
}

stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _hts tvheadend
    RETVAL=$?
    return $RETVAL
}

restart()
{
    stop
    start
}

# See how we were called.
case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|reload)
    restart
    ;;
    condstop)
    if [ -e "$LOCKFILE" ]; then
	stop
    fi
    ;;
    condrestart|condreload)
    if [ -e "$LOCKFILE" ]; then
	restart
    fi
    ;;
    status)
    status --pidfile "$PIDFILE" --expect-user _hts tvheadend
    RETVAL=$?
    ;;
    *)
    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
    RETVAL=1
esac

exit $RETVAL
