#! /bin/sh
##  Watch a log file and send mail when it gets new output.
##  By Steve Groom <stevo@elroy.Jpl.Nasa.Gov>
##  I set it up to start from rc.news the same way as innwatch.

##  =()<. @<_PATH_SHELLVARS>@>()=
. /news/lib/innshellvars

PROGNAME=logwatch
LOCK=${LOCKS}/LOCK.${PROGNAME}
##  Where to put the timestamp file (directory and filename).
TIMESTAMP=${LOCKS}/${PROGNAME}.time
##  Interval (seconds) between checks.
INTERVAL=600
##  Logfile to watch.
LOGFILE=${MOST_LOGS}/news.crit

##  Anyone else there?
shlock -p $$ -f ${LOCK} || {
    echo "${PROGNAME}: [$$] locked by [`cat ${LOCK}`]"
    exit 0
}
trap 'rm -f ${LOCK} ; exit 1' 1 3 15

while : ; do
    if [ -f ${LOGFILE} ]; then
	if [ ! -f ${TIMESTAMP} ]; then
	    DOIT=${LOGFILE}
	else
	    # use ls to print most recently modified file first.
	    # If that's ${LOGFILE}, it's changed since the last pass.
	    DOIT="`ls -t ${TIMESTAMP} ${LOGFILE} | sed -e 1q | grep ${LOGFILE}`"
	fi

	# If the file has been modified more recently than the timestamp,
	# and the file has length greater than 0, send the warning.
	if [ -n "${DOIT}" -a -s ${LOGFILE} ]; then
	    date >${TIMESTAMP}
	    (
		ls -l ${LOGFILE}
		echo "-----"
		ctlinnd -t120 mode
		echo "-----"
		cat ${LOGFILE}
	    ) 2>&1 \
	    | ${MAILCMD} -s "${PROGNAME} warning: messages in ${LOGFILE}" \
		${NEWSMASTER}
	fi
    fi
    sleep ${INTERVAL}
done
