#! /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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

##################################################################
# case1: Test whether messages are logged to the specified file  #
#        in the configuration file.                              #
#                                                                #
#        Send messages to syslogd at some level and facility     #
#        and grep for those messages.                            #
#                                                                #
# syslog.conf should contain:                                    #
#        *.crit           /usr/adm/critical                      #
#         mail.info        /usr/spool/adm/syslog                 #
##################################################################

syslog_case1()
{
	#set the trap to handle signals.
	trap '
		echo Testing is terminating due to a signal...
		exit 1
	' 1 2 3 6 11 15

	echo
	echo "syslog: Testing whether messages are logged into log file..."
	echo
	# Create the configuration file specific to this test case.
	echo "*.crit		/var/log/messages" > /etc/syslog.conf
	echo "mail.info	/var/log/maillog" >> /etc/syslog.conf

	#Restart syslog
	kill -1 `ps -ef | grep syslogd | grep -v grep | awk '{ print $2 }'`
	
	# Grepping pattern has to be changed whenever the executable name
	# changes, ex: syslogtst executable.
	oldvalue1=`grep -c "syslogtst: mail info test" /var/log/maillog`
	# Call syslogtst executable with case number as argument
	syslogtst 1 2>/dev/null
	if [[ $? -ne 0 ]]; then
		status_flag=1
		return
	fi
	sleep 2
	newvalue1=`grep -c "syslogtst: mail info test" /var/log/maillog`
	(( diff=$newvalue1 - $oldvalue1 ))
	if [[ $diff -ne 1 ]]; then
		status_flag=1
	fi
}

echo start $0
echo "----------------------------------------------------------------"
status_flag=0

#Back up /etc/syslog.conf
cp /etc/syslog.conf /etc/syslog.conf.bak01

syslog_case1
if [ $status_flag -eq 0 ]
then
	echo
	echo $0 "PASSED"
else
	echo
	echo $0 "FAILED"
fi
echo "----------------------------------------------------------------"

#Restore syslog.conf
mv /etc/syslog.conf.bak01 /etc/syslog.conf

#Restart syslog
kill -1 `ps -ef | grep syslogd | grep -v grep | awk '{ print $2 }'`

exit $status_flag
