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

##################################################################
# case5: Test the logging option: LOG_CONS                       #
#                                                                #
#        o Do openlog() with LOG_CONS option.                    #
#        o Disable /dev/syslog by moving it to a temporary file  #
#          name.                                                 #
#        o Send the syslog message.                              #
#        o Check whether this is written to the console i.e to   #
#          the  file /usr/adm/ktlog/<this year>/<this month>/    #
#          <to_day>                                              #
##################################################################

syslog_case5()
{
        #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 log option: LOG_CONS..."
        echo

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

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

        oldvalue=`grep -c "syslogtst: info to console test." /var/log/messages`
        # syslogtst does the disabling of /dev/syslog, sends the message and
        # enables /dev/syslog.
        syslogtst 5 2>/dev/null
        if [[ $? -ne 0 ]]; then
                status_flag=1
                return
        fi
        sleep 2

        newvalue=`grep -c "syslogtst: info to console test." /var/log/messages`

        (( diff=$newvalue - $oldvalue ))
        if [[ $diff -ne 1 ]]; then
                status_flag=1
                echo "*****--- Level $current failed ---*****"
        elif [[ $diff -eq 1 ]]; then
                echo "*****--- Level $current passed ---*****"
        fi
        (( diff=$newvalue - $oldvalue ))
        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.bak05

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

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

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

exit $status_flag
