#!/bin/csh -f

# simple script to mail me new debugging output from restricted-list

# 1/6/06

# hacked to work under linux, take potential email destination, and
# allow for just bounces; 11/19/20

switch (`uname -s``uname -r`)
case SunOS5*:
    set MAIL = /usr/ucb/mail
    set SLIDE = /usr/local/bin/slide
	breaksw
case Linux*:
    set MAIL = /usr/bin/mail
    set SLIDE = /usr/bin/sudo
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -b:
		set JUSTBOUNCES
		shift
		breaksw
	default:
	    if (! $?EDEST) then
		echo "$1" | grep "@" > /dev/null
		if (! $status) then
		    set EDEST = $1
		    shift
		    breaksw
		endif
	    endif
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

if (! $?EDEST) set EDEST = watrous@mx.farside.rutgers.edu
set OUT = /var/tmp/`basename $0`.$EDEST
if ($?JUSTBOUNCES) set OUT = /var/tmp/`basename $0`.bounces.$EDEST

#if (! -e $OUT.old) touch $OUT.old
if (! -e $OUT.old) then
    echo `date` Initializing $OUT.old
    /bin/ls /tmp | grep '\.dist\.debug\.[0-9]*$' | sort > $OUT.old
    set STATUS = $status
    if ($STATUS) exec echo ${0}: error encountered -- aborting
endif

/bin/ls /tmp | grep '\.dist\.debug\.[0-9]*$' | sort > $OUT.new
# swap space ran out 6/8/08 resulting in flood of "new" messages when it returned
set STATUS = $status
if (-z $OUT.new) exit
if ($STATUS) exec echo ${0}: error encountered -- aborting

set SHOST = `/bin/hostname | /bin/sed 's;\.rutgers\.edu;;'`

if ($?DEBUG) comm -13 $OUT.{old,new}

set FILES =  ( `comm -13 $OUT.{old,new}` )
foreach FILE ( $FILES )
    $SLIDE cat /tmp/$FILE | \
	egrep "^WHO = |^X-Spam-Level: |^Substituting |^From: |^Subject: |^To: |^Not bouncing |^Bouncing |^Posting |is not allowed|^Date:" > $OUT
# reorder/format this for readability
#	egrep "^From: |^Subject: |^To: |^Date: " $OUT > $OUT.tmp
	egrep -i "^From: |^Subject: |^To: |^Cc: |^Date: " $OUT > $OUT.tmp
if (! $?VERBOSE) echo "" >> $OUT.tmp
	egrep "^WHO = |^X-Spam-Level: |^Substituting " $OUT | sed 's;.;    &;' >> $OUT.tmp
if (! $?VERBOSE) echo "" >> $OUT.tmp
	egrep "^Not bouncing |^Bouncing |^Posting |is not allowed" $OUT >> $OUT.tmp
    echo "" >> $OUT.tmp

#	/bin/mv $OUT.tmp $OUT

    echo "" >> $OUT

if ($?JUSTBOUNCES) then
    grep -i "bouncing message" $OUT.tmp > /dev/null
    if ($status) continue
endif

# watrous@cs below changed to watrous because Hanz was blocking thru dragon
# text in the body which was allowed in the subject.  3/21/08
  if ($?VERBOSE) then
    $SLIDE cat $OUT $OUT.tmp /tmp/$FILE | \
	$MAIL -s "/tmp/$FILE on $SHOST" $EDEST > /dev/null
  else
    cat $OUT.tmp | \
	$MAIL -s "/tmp/$FILE on $SHOST" $EDEST > /dev/null
  endif
end

if ($#FILES) then
    if ( -e $OUT.old.4) /bin/mv $OUT.old.{4,5}
    if ( -e $OUT.old.3) /bin/mv $OUT.old.{3,4}
    if ( -e $OUT.old.2) /bin/mv $OUT.old.{2,3}
    if ( -e $OUT.old.1) /bin/mv $OUT.old.{1,2}
    if ( -e $OUT.old.0) /bin/mv $OUT.old{.0,}

    /bin/mv $OUT.old{,.0}
    /bin/mv $OUT.{new,old}
else
    /bin/rm -f $OUT.new
endif
