#! /bin/sh

#  Copyright (c) International Business Machines  Corp., 2002
#
#  This program is free software;  you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY;  without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#  the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program;  if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>
# 02/05/03  Modified - Manoj Iyer <manjo@mail.utexas.edu> use USCTEST macros
#		   fixed bugs.
# 07/26/05  Michael Reed  <mreedltp@vnet.ibm.com>
#		   Made changes to account for the replacement of syslogd
#		   with syslog-ng
#
##################################################################
# case 8: Test all the facilities at a particular level.	 #
#								 #
# Facilities available are: LOG_KERN, LOG_USER, LOG_MAIL	 #
# LOG_DAEMON, LOG_AUTH, LOG_LPR.				 #
# Don't know how to send kernel messages from syslog()   	 #
#								 #
# o Create seperate entries in config file for each facility.	 #
# o Send the message and grep for the entry in log file.	 #
##################################################################

. syslog-lib.sh || exit 1

syslog_case8()
{
	local facility_no=1
	local facilities="user mail daemon auth lpr"

	tst_resm TINFO "testing all the facilities"

	for facility in $facilities; do

		tst_resm TINFO "Doing facility: $facility..."

		# Create the configuration file specific to this facility
		# Level is fixed at info.
		case "$CONFIG_FILE" in
		/etc/syslog.conf|/etc/rsyslog.conf)
			echo "$RSYSLOG_CONFIG" > $CONFIG_FILE
			echo "$facility.info	/var/log/messages" >> $CONFIG_FILE
			echo "$facility.info	/var/log/maillog" >> $CONFIG_FILE
			;;

		/etc/syslog-ng/syslog-ng.conf)
			echo "source src{ internal(); unix-dgram(\"/dev/log\"); udp(ip(\"0.0.0.0\") port(514)); };" > $CONFIG_FILE
			echo "filter f_syslog-$facility { level(info) and facility($facility); };" >> $CONFIG_FILE
			echo "destination syslog-messages { file(\"/var/log/messages\"); };" >> $CONFIG_FILE
			echo "destination syslog-mail { file(\"/var/log/maillog\");};" >> $CONFIG_FILE
			echo "log { source(src); filter(f_syslog-$facility); destination(syslog-mail); };"  >> $CONFIG_FILE
			echo "log { source(src); filter(f_syslog-$facility); destination(syslog-messages); };"  >> $CONFIG_FILE
			;;

		esac

		restart_syslog_daemon

		if [ -e /var/log/messages ]; then
			oldvalue=`grep -c "syslogtst: $facility info test." /var/log/messages`
		else
			oldvalue=0
		fi

		if [ -e /var/log/maillog ]; then
			old_mail_check=`grep -c "syslogtst: $facility info test." /var/log/maillog`
		else
			old_mail_check=0
		fi

		# syslogtst has to be called with one more
				# additional facility argument(1-6)
		if ! syslogtst 8 $facility_no 2>/dev/null; then
			status_flag=1
			return
		fi
		sleep 2
		# check if /var/log/maillog script exists
		for logf in messages maillog
		do
			if [ ! -e /var/log/$logf ]; then
				tst_resm TBROK "/var/log/$logf no such log file"
				cleanup 1
			fi
		done

		new_mail_check=`grep -c "syslogtst: $facility info test." /var/log/maillog`
		newvalue=`grep -c "syslogtst: $facility info test." /var/log/messages`
		diff=$(( $newvalue - $oldvalue ))
		mail_check=$(( $new_mail_check - $old_mail_check ))

		if [ $facility = "mail" ]; then
			if [ $mail_check -ne 1 ]; then
				tst_resm TFAIL " Facility $facility failed"
				status_flag=1
			elif [ $mail_check -eq 1 ]; then
				tst_resm TPASS " Facility $facility passed"
			fi
		elif [ $diff -ne 1 ]; then
			tst_resm TFAIL " Facility $facility failed"
			status_flag=1
		else
			tst_resm TPASS " Facility $facility passed"
		fi
		# Increment the facility_no for next...
		: $(( facility_no += 1 ))
	done
}

tst_resm TINFO " Test all the facilities at a particular level."
tst_resm TINFO " Facilities available are: LOG_KERN, LOG_USER, LOG_MAIL"
tst_resm TINFO " LOG_DAEMON, LOG_AUTH, LOG_LPR."
tst_resm TINFO " Don't know how to send kernel messages from syslog()"
tst_resm TINFO " o Create seperate entries in config file for each facility."
tst_resm TINFO " o Send the message and grep for the entry in log file."

setup
syslog_case8
cleanup ${status_flag:=0}