#!/bin/csh -f
#
# This is my server's watchdog.
# New features- doesn't die until after it overflows mbox (instead of something
# else doing it too).

set path =  (/usr/ucb /usr/bin /usr/local/mh ~/bin)

set gmaddr = bampton@cs.utk.edu
setenv PROFDIR profiles

if ( $#argv == 1 ) then
  set sleeptime = $1
else
  set sleeptime = 90
endif

onintr quit

set prog = `basename $0`
echo `hostname` $$ > /tmp/$prog.pid
cp /tmp/$prog.pid ~/.prog.pid
echo -n "Starting on "
date

while (1)
  # If there is mail, log it, inc it, forward it, and die if needed
  msgchk -nonotify all && date && inc -width 150 && multi-forw 
  if ( -e ~/Mail/outgoing/mbox) then
    set x = `ls -l ~/Mail/outgoing/mbox | awk '{print $4}'`
    if ($x >1200000) then
      echo ""
      echo -n "Terminating on "
      date
      mail -s "Dead forwarder" $gmaddr < /tmp/$prog.pid
      rm /tmp/$prog.pid ~/.prog.pid
      exit 5
    endif
  endif
  touch ~/.sleep
sleep:
  sleep $sleeptime
  if ( -f ~/.sleep ) then
    rm ~/.sleep
  else
    goto sleep
  endif
  echo `hostname` $$ > /tmp/$prog.pid
  cp /tmp/$prog.pid ~/.prog.pid
  touch /tmp/$prog.pid 
end

quit:
rm /tmp/$prog.pid $HOME/.$prog.pid
echo -n "Terminating (by ^C/SIGTERM) on "
date
exit
