#! /bin/sh
# Copyright (c) 2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Marius Tomaschewski <mt@suse.de>, 2003
#
# Send comments and suggestions to <feedback@suse.de>
#
# This module creates the syslog-ng(8) configuration file
#    /etc/syslog-ng/syslog-ng.conf
# using variables from /etc/sysconfig/syslog-ng and the
# configuration file template
#    /etc/syslog-ng/syslog-ng.conf.in
#

# check if we are started as root
# only one of UID and USER must be set correctly
if test "$UID" != 0 ; then
    echo "You must be root to start $0."
    exit 1
fi

r=$ROOT
test -f $r/lib/YaST/SuSEconfig.functions || {
    echo "ERROR - can not find $r/lib/YaST/SuSEconfig.functions!!"
    echo "This should not happen.  Exit..."
    exit 1
}
. $r/lib/YaST/SuSEconfig.functions

#
# syslog-ng configuration file
#
syslog_ng_config="/etc/syslog-ng/syslog-ng.conf"

#
# check if template is present
#
test -r "${r}/${syslog_ng_config}.in" || exit 1

#
# syslog system configurations
#
sysconfig_syslog="/etc/sysconfig/syslog"
. ${r}/${sysconfig_syslog}            || exit 1


gen_log_sockets ()
{
	# main /dev/log socket
	echo '	unix-dgram("/dev/log");'

	# additional sockets for chroot-services
	for var in ${!SYSLOGD_ADDITIONAL_SOCKET*} ; do
		eval val=\$$var
		if test -n "$val" && test -d "${val%/*}" ; then
			echo '	unix-dgram("'${val}'");'
		fi
	done
}

sed_log_sockets ()
{
	tmp_sockets=$(mktemp -q ${r}/tmp/syslog-ng.sockets.XXXXXX) && {
		gen_log_sockets >> ${tmp_sockets} && \
		sed -e "/@SuSEconfig_SOCKETS@/{
			r ${tmp_sockets}
			/@SuSEconfig_SOCKETS@/d
			}"
		rm -f ${tmp_sockets}
	}
}

create_ng_config ()
{
	sed_log_sockets | sed -e '/@SuSEconfig.*@/d'
}

if test "$SYSLOG_NG_CREATE_CONFIG" = "yes" ; then

	rm -f "${r}/${syslog_ng_config}.SuSEconfig"
	{
	  echo_warning "${syslog_ng_config}.SuSEconfig" \
	               "# you can modify ${syslog_ng_config}.in instead"
	  create_ng_config < "${r}/${syslog_ng_config}.in"

	}  > "${r}/${syslog_ng_config}.SuSEconfig" || {
	  rm -f "${r}/${syslog_ng_config}.SuSEconfig"
	  echo "ERROR: generation of ${syslog_ng_config}.SuSEconfig failed" 1>&2
	  exit 1
	}
	check_md5_and_move "${r}/${syslog_ng_config}"

	# reload if new config created and we have no different root
	test -f "${r}/${syslog_ng_config}.SuSEconfig" && exit 1 || {
		if test -z "$r" && test "$SYSLOG_DAEMON" = "syslog-ng" ; then
			/sbin/rcsyslog reload &>/dev/null
			# LSB 7 means "not running" => OK
			if test $? -eq 7 ; then exit 0; fi
		fi
	}
fi

