#! /bin/sh
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: 
#  Marius Tomaschewski <mt@suse.de>
#
# /etc/init.d/syslog-ng
#
#   and symbolic its link
#
# /sbin/rcsyslog-ng
#
### BEGIN INIT INFO
# Provides: syslog-ng
# Required-Start: network
# Required-Stop:  network
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Start the system logging daemons
### END INIT INFO

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# 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.

# preset default values:
SYSLOG_NG_REPLACE="yes"
SYSLOG_NG_PARAMS=""

# source SuSE config
. /etc/rc.status
. /etc/sysconfig/syslog-ng

# check binaries and config
klogd="/sbin/klogd"
syslogng="/sbin/syslog-ng"
configng="/etc/syslog-ng/syslog-ng.conf"
[ -x "$klogd"       ] || exit 5
[ -x "$syslogng"    ] || exit 5
[ -f "$configng"    ] || exit 6

# Source status shell functions 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_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
[ -r /etc/rc.status ] || exit 1
. /etc/rc.status

# reset status of this service
rc_reset

case "$1" in
start)
	if [ "x$SYSLOG_NG_REPLACE" = xyes ] ; then
		checkproc "$klogd" && killproc "$klogd" 2> /dev/null
	fi
	if checkproc "$syslogng" ; then
		killproc "$syslogng"  2> /dev/null
		sleep 2
		echo -n "Re-"
	fi
	echo -n "Starting syslog-ng service"
	startproc "$syslogng" $SYSLOG_NG_PARAMS
	rc_check
	# in case it returns != 0 because of temporary errors
	if [ $? -ne 0 ] ; then
		sleep 3
		checkproc "$syslogng"
		rc_check
	fi
	if [ "$SYSLOG_NG_REPLACE" = "yes" ] ; then
		sleep 1
		test -z "$KERNEL_LOGLEVEL" && KERNEL_LOGLEVEL=1
		startproc "$klogd" -c $KERNEL_LOGLEVEL
	fi
	rc_status -v
;;
stop)
	echo -n "Shutting down syslog-ng service"
	if [ "$SYSLOG_NG_REPLACE" = "yes" ] ; then
		killproc -TERM "$klogd"
		rc_check
	fi
	killproc -TERM "$syslogng" 2>/dev/null
	rc_status -v
;;
try-restart)
	## Stop the service and if this succeeds (i.e. the
	## service was running before), start it again.
	$0 status >/dev/null &&  $0 restart
	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|reload)
	## Signal the daemon to reload its config.
	echo -n "Reload syslog-ng service"
	if [ "$SYSLOG_NG_REPLACE" = "yes" ] ; then
		killproc -TSTP "$klogd"
		rc_check
	fi
	killproc -HUP "$syslogng"
	rc_check
	if [ "$SYSLOG_NG_REPLACE" = "yes" ] ; then
		killproc -CONT "$klogd"
		rc_check
		killproc -USR2 "$klogd"
		rc_check
	fi
	rc_status -v
;;
status)
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0. checkproc
	## returns LSB compliant status values:
	## 0 - service running
	## 1 - service dead, but /var/run/  pid  file exists
	## 2 - service dead, but /var/lock/ lock file exists
	## 3 - service not running
	echo -n "Checking for syslog-ng service: "
	checkproc "$syslogng"
	rc_status -v
;;
probe)
	## Optional: Probe for the necessity of a reload,
	## give out the argument which is required for a reload.
	test "$configng" -nt /var/run/syslog-ng.pid && echo reload
;;
*)
	echo "Usage: $0 {start|stop|status|restart|reload|probe}"
	exit 1
	;;
esac
rc_exit

