#! /bin/csh -f
#
#  write data (for a "uread" program under "ichannel") and wait for a file
#  to appear
#
#  uread_send_wait <dir> <wait-file> <input...>
#
#     <dir> -- path to communications directory
#     <wait-file> -- path to file for which to wait (rel. to $cwd not <dir>).
#     <input...> -- input to program presumed to be running under ichannel 
#                   using <dir>
#
#  fail after time_out...
#
  @ time_out = 180
  if ( $?UREAD_SERVER_TIMEOUT ) then
    @ time_out = $UREAD_SERVER_TIMEOUT
  endif
#
  if ( $#argv < 3 ) then
    echo " ?uread_send_wait: syntax error: too few arguments."
    exit 1
  endif
#
  set dir = $1
  shift
#
  if ( ! -d $dir ) then
    echo " ?uread_send_wait: communications directory not found: $dir "
    exit 1
  endif
#
  set wait_file = $1
  shift

  if ( -f $wait_file ) then
    mv $wait_file $wait_file~
  endif

#
# OK send input
#
  echo "$argv" > $dir/input.dat

  @ iwait = 0 
  @ iwarn = 0

  while ( $iwait < $time_out ) 
    @ iwait++
    @ iwarn++

    if ( -f $wait_file ) then
      exit 0
    endif

    if ( $iwarn == 10 ) then
      @ iwarn = 0
      echo "uread_send_wait: waiting for: $wait_file ... "
    endif

    sleep 1
  end

  echo " ?uread_send_wait: time_out = $time_out "
  exit 1
