#! /bin/sh
#
# Copyright (c) 1996-2001 SuSE GmbH Nuernberg, Germany.
# Copyright (c) 2002-2003 SuSE Linux AG, Nuernberg, Germany.
#
# Author: Florian La Roche <florian@suse.de>, 1996
#	  Werner Fink <werner@suse.de>, 1998-2001
#
# /etc/init.d/syslog
#
### BEGIN INIT INFO
# Provides:       syslog
# Required-Start: network 
# Should-Start:   earlysyslog
# Required-Stop:  network
# Default-Start:  2 3 5
# Default-Stop:
# Description:    Start the system logging daemons
### END INIT INFO

. /etc/sysconfig/syslog

BINDIR=/sbin

# update $SYSLOG_DAEMON if not up-to-date
test -n "$SYSLOG_DAEMON" && \
test -x "${BINDIR}/$SYSLOG_DAEMON" || {
     for SYSLOG_DAEMON in syslog-ng syslogd ; do
         test -x "${BINDIR}/$SYSLOG_DAEMON" && break
     done
}

# set daemon dependent variables
case "$SYSLOG_DAEMON" in
    syslog-ng)
	syslog=syslog-ng
	config=/etc/syslog-ng/syslog-ng.conf
	params="$SYSLOG_NG_PARAMS"
	;;
    *)
	syslog=syslogd
	config=/etc/syslog.conf
	params="$SYSLOGD_PARAMS"
	;;
esac
# add additional sockets to params
for variable in ${!SYSLOGD_ADDITIONAL_SOCKET*}; do
    eval value=\$$variable
    test -n "${value}" && test -d ${value%/*} && \
    params="$params -a $value"
done

syslog_pid="/var/run/${syslog}.pid"
syslog_bin=${BINDIR}/$syslog
  klog_bin=${BINDIR}/klogd
syslog_use=""

# check config and programs
test -s ${config}	  || {
    echo 1>&2 "${config} does not exist"
    if test "$1" == "stop" ; then exit 0 ; else exit 6 ; fi
}
test -x ${syslog_bin} || {
    echo 1>&2 "${syslog_bin} is not installed"
    if test "$1" == "stop" ; then exit 0 ; else exit 5 ; fi
}
test -x ${klog_bin}   || {
    echo 1>&2 "${klog_bin} is not installed"
    if test "$1" == "stop" ; then exit 0 ; else exit 5 ; fi
}
test -s /var/lock/subsys/syslogd && {
    read -t 5 syslog_use < /var/lock/subsys/syslogd
}
test -z "$syslog_use" && syslog_use=${syslog_bin}
syslog_use_pid="/var/run/${syslog_use##*\/}.pid"

#
# Do not translate symbol addresses for 2.6 kernel
#
case `uname -r` in
0.*|1.*|2.[0-4].*)
    ;;
*)
    KLOGD_PARAMS="${KLOGD_PARAMS:+$KLOGD_PARAMS }-x"
    ;;
esac

. /etc/rc.status
rc_reset
case "$1" in
    start)
	if test -f /var/lock/subsys/earlysyslog ; then
	    # Syslog service already done
	    rm -f  /var/lock/subsys/earlysyslog
	    exit 0
	fi
	if test "$2" = "early" ; then
	    # Start syslog service early
	    touch  /var/lock/subsys/earlysyslog
 	fi

	if ! test -e /dev/xconsole ; then
	    mkfifo -m 0600 /dev/xconsole
	    chown root:tty /dev/xconsole
	fi
	if test -s /var/run/klogd.pid ; then
	    killproc ${klog_bin}
	fi
	if test -s ${syslog_use_pid} ; then
	    killproc -p ${syslog_use_pid} ${syslog_use}
	    echo -n "Re-"
	    rm -f /var/lock/subsys/syslogd
	fi
	echo -n "Starting syslog services"
	test -z "$KERNEL_LOGLEVEL" && KERNEL_LOGLEVEL=1

	startproc -p ${syslog_pid} ${syslog_bin} ${params}
	rc_status

	# Remember the currently used syslog daemon
	echo ${syslog_bin} > /var/lock/subsys/syslogd

	if test $? -eq 0 ; then
	    startproc ${klog_bin} -c $KERNEL_LOGLEVEL $KLOGD_PARAMS
	    rc_status
	fi
	rc_status -v
	;;
    stop)
	rm -f /var/lock/subsys/earlysyslog
	test "$2" = "early" && exit 0

	echo -n "Shutting down syslog services"
	killproc		      -TERM ${klog_bin}   ; rc_status
	killproc -p ${syslog_use_pid} -TERM ${syslog_use} ; rc_status -v
	rm -f /var/lock/subsys/syslogd
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
	    echo "${attn} Use try-restart ${done}(LSB)${attn} instead${norm}"
	fi
	$0 status
	if test $? = 0; then
	    $0 restart
	else
	    rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    reload|force-reload)
	echo -n "Reload syslog service"
	killproc		      -TSTP ${klog_bin}   ; rc_status
	killproc -p ${syslog_use_pid} -HUP  ${syslog_use} ; rc_status
	killproc		      -CONT ${klog_bin}   ; rc_status
	killproc		      -USR2 ${klog_bin}   ; rc_status -v
	;;
    status)
	echo -n "Checking for service syslog:"
	checkproc		  	${klog_bin}   ; rc_status
	checkproc -p ${syslog_use_pid}	${syslog_use} ; rc_status -v
	;;
    probe)
	if test "${syslog_use_pid}" != "${syslog_pid}" ; then
	    echo restart
	else
	    test ${config} -nt ${syslog_pid} && echo reload
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
