#!/bin/csh -f

if ( $#argv < 3 ) then
   echo "Usage ${0} directory filename size_in_bytes"
   exit 1
endif

set CHECK_INTERVAL=30

cd $1
set FILE = $2

if ( ! -e .complete ) touch .complete
@ DONE = 0

while ( $DONE < 10 )
  touch .complete_new
  sleep $CHECK_INTERVAL

  if ( -e $FILE) then 
     set SIZE=`/bin/wc -c $FILE`
     test $SIZE[1] -ge $3
     if ( ! $status ) then
        echo "$FILE has reached $3 bytes"
        break
     endif
     # 
     test $FILE -nt .complete
     if ( $status ) then
         @ DONE = $DONE + 1
         echo "$FILE is available, and not being updated"
     else
         echo "$FILE still being written to"
         mv .complete_new .complete
         @ DONE = 0
     endif
  else
     echo "$FILE not available, waiting"
     @ DONE = $DONE + 1
  endif
end

echo "Proceed with processing $DONE"
rm .complete*
