#! /bin/sh
# Copyright (c) 2015 Packman Team <packman@links2linux.de>
# Copyright (c) 2005 SUSE LINUX Products GmbH, Nuernberg, Germany.
# Copyright (c) 1995-2004 SuSE Linux AG, Nuernberg, Germany.
#
#
# Please submit bugfixes or comments via http://bugs.links2linux.org/
#
# init.d/tvheadend
#
#   and symbolic its link
#
# /usr/sbin/rctvheadend
#
# System startup script for the tvheadend daemon
#
### BEGIN INIT INFO
# Provides: tvheadend
# Required-Start:    $local_fs $remote_fs boot.udev
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start tvheadend daemon
# Description:    Start tvheadend daemon
### END INIT INFO

# Source SuSE config
. /etc/rc.status

TVHEADEND_PIDFILE=/var/run/tvheadend.pid

TVHEADEND_BIN=/usr/bin/tvheadend
test -x $TVHEADEND_BIN || { echo "$TVHEADEND_BIN not installed";
    if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }

TVHEADEND_SYSCONFIG=/etc/sysconfig/tvheadend
test -r $TVHEADEND_SYSCONFIG || { echo "$TVHEADEND_SYSCONFIG not existing";
    if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }
. $TVHEADEND_SYSCONFIG

# Configure command line options
[ "$TVH_ENABLED" = "1" ] || exit 0

TVHEADEND_ARGS="-f"
[ -z "$TVH_USER"      ] || TVHEADEND_ARGS="$TVHEADEND_ARGS -u $TVH_USER"
[ -z "$TVH_GROUP"     ] || TVHEADEND_ARGS="$TVHEADEND_ARGS -g $TVH_GROUP"
[ -z "$TVH_CONF_DIR"  ] || TVHEADEND_ARGS="$TVHEADEND_ARGS -c $TVH_CONF_DIR"
[ -z "$TVH_ADAPTERS"  ] || TVHEADEND_ARGS="$TVHEADEND_ARGS -a $TVH_ADAPTERS"
[ "$TVH_IPV6" = "1"   ] && TVHEADEND_ARGS="$TVHEADEND_ARGS -6"
[ -z "$TVH_HTTP_PORT" ] || TVHEADEND_ARGS="$TVHEADEND_ARGS --http_port $TVH_HTTP_PORT"
[ -z "$TVH_HTTP_ROOT" ] || TVHEADEND_ARGS="$TVHEADEND_ARGS --http_root $TVH_HTTP_ROOT"
[ -z "$TVH_HTSP_PORT" ] || TVHEADEND_ARGS="$TVHEADEND_ARGS --htsp_port $TVH_HTSP_PORT"
[ -z "$TVH_ARGS"      ] || TVHEADEND_ARGS="$TVHEADEND_ARGS $TVH_ARGS"
[ "$TVH_DEBUG" = "1"  ] && TVHEADEND_ARGS="$TVHEADEND_ARGS -s"


# 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_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#

case "$1" in
    start)
	echo -n "Starting tvheadend daemon"
	udevadm settle
	/sbin/startproc -p $TVHEADEND_PIDFILE $TVHEADEND_BIN $TVHEADEND_ARGS
	rc_status -v
	;;
    stop)
	echo -n "Shutting down tvheadend daemon"
	/sbin/killproc -p $TVHEADEND_PIDFILE -TERM $TVHEADEND_BIN
	rm -f $TVHEADEND_PIDFILE
	rc_status -v
	;;
    try-restart|condrestart)
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
            echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        rc_status
        ;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo "Reload tvheadend daemon"
	$0 try-restart
	rc_status
	;;
    reload)
	# tvheadend does not support SIGHUP, so fail.
	echo -n "Reload tvheadend daemon"
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for tvheadend daemon: "
	/sbin/checkproc -p $TVHEADEND_PIDFILE $TVHEADEND_BIN
	rc_status -v
	;;
    probe)
	test $TVHEADEND_SYSCONFIG -nt $TVHEADEND_PIDFILE && echo restart
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
