#! /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

# 12/05/02  Port to bash -Robbie Williamson <robbiew@us.ibm.com>

##################################################################
# case 7: Test the priorities....                                #
#                                                                #
#         o Add lowest prority level i.e debug level entry to    #
#           configuration file.                                  #
#         o Send syslog messages at all levels and see whether   #
#           higher level messages are logged.                    #
##################################################################

syslog_case7()
{
        #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 the priorities ..."
        echo

        # Create the configuration file specific to this test case.
        echo "user.debug        /var/log/messages" > /etc/syslog.conf

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

        # Grep for the following patterns in the log file...
        emerg_old=`grep -c "syslogtst: emergency log" /var/log/messages`
        alert_old=`grep -c "syslogtst: alert log" /var/log/messages`
        crit_old=`grep -c "syslogtst: critical log" /var/log/messages`
        err_old=`grep -c "syslogtst: error log" /var/log/messages`
        warning_old=`grep -c "syslogtst: warning log" /var/log/messages`
        notice_old=`grep -c "syslogtst: notice log" /var/log/messages`
        info_old=`grep -c "syslogtst: info log" /var/log/messages`
        debug_old=`grep -c "syslogtst: debug log" /var/log/messages`

        # Call syslogtst. It will send the messages of all levels.
        syslogtst 7 2>/dev/null
        if [[ $? -ne 0 ]]; then
                status_flag=1
                return
        fi
        sleep 2

        emerg_new=`grep -c "syslogtst: emergency log" /var/log/messages`
        alert_new=`grep -c "syslogtst: alert log" /var/log/messages`
        crit_new=`grep -c "syslogtst: critical log" /var/log/messages`
        err_new=`grep -c "syslogtst: error log" /var/log/messages`
        warning_new=`grep -c "syslogtst: warning log" /var/log/messages`
        notice_new=`grep -c "syslogtst: notice log" /var/log/messages`
        info_new=`grep -c "syslogtst: info log" /var/log/messages`
        debug_new=`grep -c "syslogtst: debug log" /var/log/messages`

        (( emerg=$emerg_new - $emerg_old ))
        (( alert=$alert_new - $alert_old ))
        (( crit=$crit_new - $crit_old ))
        (( err=$err_new - $err_old ))
        (( warning=$warning_new - $warning_old ))
        (( notice=$notice_new - $notice_old ))
        (( info=$info_new - $info_old ))

        if [[ $emerg -ne 1 || $alert -ne 1 || $crit -ne 1 || $err -ne 1 || \
        $warning -ne 1 || $notice -ne 1 || $info -ne 1 || $info -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.bak07
syslog_case7

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

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

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

exit $status_flag
