#!/bin/ksh
#
#    "%W% %G% 
#    This script is used to monitor the news spool directory
#    for articles that discuss the comp.sources.misc newsgroup.
#
# 32 20 * * * /bin/su news -c "/u1/csm/bin/watch_csm"
#
SPOOLDIR=/usr/spool/news
ARTICLE=/usr/local/bin/article

POSTQUEUE=/u1/csm
DATAFILE=$POSTQUEUE/queue/.articles
LISTIT=$POSTQUEUE/queue/.listit
NEW=$POSTQUEUE/queue/.new$$.articles
TMPFILE=$POSTQUEUE/queue/.tmp$$.articles

#
# Locate the files that need to be checked...
#
find $SPOOLDIR -type f -print | 
    xargs egrep -li 'comp.sources.misc|c.s.misc|Landfield' | 
      grep -v 'comp/sources/misc' > $TMPFILE

#
# minor bug in that you will get the cross posts as well... sigh....
# If I find the time someday I'll fix it...
#

#
#  If we found files...
#
if [ -s $TMPFILE ]
then
    if [ -s $DATAFILE ]
    then 
        # 
        # For each file in the 'found' file
        # see if it was in the previous run's
        # output file. If it was, no need to
        # send it to the moderator.
        #
        while read filename
        do
            grep $filename $DATAFILE
            case $? in
                0) # found a match, this has already been seen
                   ;;
                1) # No match, may be new article
                   # check for duplicates/crossposts
                   # Check to see in the duplicate is an existing one or
                   # if it is a new one...
                   #
                   if [ -s $NEW ]
                   then
                        CHECKFILES="$DATAFILE $NEW"
                   else
                        CHECKFILES="$DATAFILE"
                   fi
                   mid=`$ARTICLE -f "%M" $filename`
              
                   grep $mid $CHECKFILES
                   case $? in
                        0) # found a match, this has already been seen
                          ;;
                        1) # No match, new article
                           echo "$filename" >> $NEW
                           ;;
                        *) # some error here... 
                          ;;
                       esac
                   ;;
                *) # some error here... 
                   ;;
            esac
        done < $TMPFILE
    fi
    #
    # Here we have a TMPFILE with all found filenames
    # a NEW file with just the articles that were not 
    # in the previous days files. NEW also has the message-ids
    # to be concerned with...
    #
    if [ -s $NEW ]
    then
        cat $NEW | awk '{ print $1 }' | sed "s/^/less /" |
            tee $LISTIT | /usr/ucb/Mail -s "c.s.misc Articles" kent
        rm -f $NEW
    fi
    cat $TMPFILE | $ARTICLE -f "%O   %M" > $DATAFILE
    rm -f $TMPFILE
fi
