#!/bin/sh
# 
# Copyright (c) 2003, MiTAC Inc.
#
# a script check and dump mshd log messages. rain.w 20031009.
#

logfile=/var/log/messages
tmpfile=/tmp/mshd.log

usage () {
	echo "  usage: chklog [-h] [all|info|alert|notice] [-f file]"
	echo "     -h: print this help message"
	echo "    all: print all log messages"
	echo "   info: print only info messages"
	echo "  alert: print only alert messgaes"
	echo " notice: print only notice messages"
	echo "   file: dump log messages to a file"
}

if [ "$1" = "-h" ]; then
	usage
	exit
fi

touch $tmpfile
case "$1" in
all)
	cat $logfile | grep 'mshd:' > $tmpfile
	;;
info)
	cat $logfile | grep 'mshd:' | grep 'info:' > $tmpfile
	;;
alert)
	cat $logfile | grep 'mshd:' | grep 'ALERT:' > $tmpfile
	;;
notice)
	cat $logfile | grep 'mshd:' | grep 'NOTE:' > $tmpfile
	;;
*)
	usage
	rm -f $tmpfile
	exit
esac

case "$2" in
"-f")
	if [ -z "$3" ]; then
		usage
		exit
	fi
	file=$3
	if [ ! -f $file ]; then
		touch $file
	fi
	cat $tmpfile > $file
	;;
*)
	cat $tmpfile
esac
rm -f $tmpfile

exit $?
