#!/bin/csh -f
#
# rc_add_to_log (used by receive_coursework)
#
# Takes three arguments, and implements a critical region during which
#  $1/rc_temp_receipt_log.$3 is appended to (critical) $2/.receipt_log.
#
# The first two arguments should both be absolute directory pathnames.
#  The third should be the process id no. of the calling routine.
#
#  NOTE: because this is run (via receive_coursework) from .mailfilter, it
#   is no good giving up after a timeout. Rather, the program should just go
#   on and on battering its head against the .busy, so that it shows up in
#   the process table! [very low machine overhead: it's just sleeping between
#   the tests.]
#
#  On the other hand, argument errors etc. can't really be handled other than by
#   aborting.
#

set progname = `basename $0`

if ($#argv != 3) then
  errecho "$progname : argument error"
  exit 1
endif

if !(-f $1/rc_temp_receipt_log.$3) then
  errecho "$progname : file missing in $1"
  exit 1
endif

if !(-d $2) then
  errecho "$progname : directory $2 not prepared."
  exit 1
endif

# START CRITICAL REGION

tas $2/.busy
set already_busy = $status
while ($already_busy)
  sleep 2
  tas $2/.busy
  set already_busy = $status
end #while

# we've bagged it here.....

# START NAKED TASK
cat $1/rc_temp_receipt_log.$3 >> $2/.receipt_log
# END NAKED TASK

# we've finished!
rm -f $2/.busy

# END CRITICAL REGION
