#! /bin/sh
#
# quartzpvr-server	Control the QuartzPVR Server
#
# Author: Steve Price <sdprice@cpan.org>
#
### BEGIN INIT INFO
# Provides: quartzpvr-server
# Required-Start: $network $remote_fs
# Required-Stop: $null
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Audio/Video recording
# Description:    Server providing communication between web frontend and the Perl scheduling scripts 
### END INIT INFO

# Source function library.
if [ -f /etc/rc.status ]; then
  # SUSE
  . /etc/rc.status
  rc_reset
else
  # Reset commands if not available
  rc_status() {
    case "$1" in
	-v)
	    true
	    ;;
	*)
	    false
	    ;;
    esac
    echo
  }
  alias rc_exit=exit
fi

SERVER_BIN=/usr/sbin/quartzpvr
test -x $SERVER_BIN || exit 5

# 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
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# See how we were called.
case "$1" in
  start)
	echo -n "Starting Quartz PVR: "

	## Start daemon with startproc(8). If this fails
    ## the echo return value is set appropriate.
	startproc $SERVER_BIN

	# Remember status and be verbose
    rc_status -v
	;;
  stop)
	echo -n "Stopping Quartz PVR agents: "
        ## Stop daemon with killproc(8) and if this fails
        ## set echo the echo return value.

        killproc -TERM $SERVER_BIN

        # Remember status and be verbose
        rc_status -v
	;;
  restart|reload)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
  status)
        echo -n "Checking for Quartz PVR: "
        checkproc $SERVER_BIN
        rc_status -v
        ;;
  *)
	echo "Usage: $0 {start|stop|status|restart}"
	exit 1
esac
rc_exit
