#! /bin/sh
#
# Copyright (c) 2008 Reik Keutterling
#
### BEGIN INIT INFO
# Provides:          diablo
# Required-Start:    $network $named
# Required-Stop:     $null
# Default-Start:     3 5
# Default-Stop:
# Short-Description: Diablo News Server
# Description:       Diablo News
### END INIT INFO

. /etc/rc.status
. /etc/sysconfig/diablo

START_DIABLO=`echo $START_DIABLO|tr [:upper:] [:lower:]`
START_DREADERD=`echo $START_DREADERD|tr [:upper:] [:lower:]`

if [[ "$START_DIABLO" != "yes" && "$START_DREADERD" != "yes" ]]; then
	exit 6
fi
if [ ! -f $DIABLO_CONFIG ]; then
	echo "No configuration file found."
	exit 6
fi

PATHHOME=`sed -ne 's/^path_home\s*//p' < $DIABLO_CONFIG`
PATHRUN=${PATHHOME}`sed -ne 's/^path_run\s*%s//p' < $DIABLO_CONFIG`
PATHBIN=${PATHHOME}/dbin
PATHLOG=${PATHHOME}`sed -ne 's/^path_log\s*%s//p' < $DIABLO_CONFIG`

DIABLOBIN=${PATHBIN}/diablo
DREADERBIN=${PATHBIN}/dreaderd
DNEWSLINKBIN=${PATHBIN}/dnewslink

DREADERDPID=${PATHLOG}/dreaderd.pid

# The echo return value for success (defined in /etc/rc.config).
rc_reset
case "$1" in
    start)	if [ "$START_DIABLO" = "yes" ]; then
			echo -n "Starting News-Server Diablo (diablo)"
                	startproc $DIABLOBIN $DIABLO_OPTIONS
			rc_status -v
		fi
    		if [ "$START_DREADERD" = "yes" ]; then
    			echo -n "Starting News-Server Diablo (dreaderd)"
	                startproc $DREADERBIN $DREADER_OPTIONS
			rc_status -v
		fi
		;;
    stop)	if [ "$START_DIABLO" = "yes" ]; then
    			echo -n "Shutting down News-Server Diablo (diablo)"
			killproc $DIABLOBIN
			rc_status -v
    			echo -n "Shutting down News-Server Diablo (dnewslinks)"
			killproc $DNEWSLINKBIN
			rc_status -v
		fi
    		if [ "$START_DREADERD" = "yes" ]; then
    			echo -n "Shutting down News-Server Diablo (dreaderd)"
			killproc $DREADERBIN
			rc_status -v
		fi
		;;
    restart)
    		$0 stop
		$0 start
		rc_status
		;;
    reload)
		exit 3
		;;
    status)	if [ "$START_DIABLO" = "yes" ]; then
			echo -n "News-Server Diablo (diablo): "
			checkproc $DIABLOBIN
			rc_status -v
			echo -n "News-Server Diablo (dnewslinks): "
			checkproc $DNEWSLINKBIN
			rc_status -v
		fi
    			if [ "$START_DREADERD" = "yes" ]; then
			echo -n "News-Server Diablo (dreaderd): "
			checkproc $DREADERBIN
			rc_status -v
		fi
		;;		
    *)
		echo "Usage: $0 {start|stop|restart|reload|status}"
		exit 1
esac
rc_exit
