#! /bin/csh -f
#
#  look in directory ( $1 ) for "input.dat"; 
#  first attempt to create directory if necessary.
#  when file is found, move it to "prev_input.dat" and echo the contents.
#
#  note: no messages from the script to stdout.  The intent here is to
#  pipe stdout to another program-- a UREAD fortran program, perhaps.
#

  if ( $#argv != 1 ) then
    exit 1
  endif

  set comm_dir = $1
  if ( ! -d $comm_dir ) then
    mkdir $comm_dir
    if ( $status ) then
      exit 1
    endif
  endif

  if ( -f $comm_dir/ichannel.log ) then
    mv $comm_dir/ichannel.log $comm_dir/old_ichannel.log
  endif

  if ( -f $comm_dir/kill.dat ) then
    mv $comm_dir/kill.dat $comm_dir/no_kill.dat
  endif

  @ max_inputs = 100000
  @ num_inputs = 0

  while ( $num_inputs < $max_inputs )
    while ( ! -f $comm_dir/input.dat && ! -f $comm_dir/kill.dat )
      sleep 1
    end

    set dmark = `date`

    if ( -f $comm_dir/kill.dat ) then
      # "uread" program kill sequence
      mv $comm_dir/kill.dat $comm_dir/old_kill.dat
      echo "$dmark -- kill.dat detected." >> $comm_dir/ichannel.log
      echo '*A!'
      mv $comm_dir/pid.dat $comm_dir/killed_pid.dat
      exit 0
    endif

    @ num_inputs++
    echo "$dmark ( $num_inputs ) -- input.dat found." >> $comm_dir/ichannel.log
    mv $comm_dir/input.dat $comm_dir/prev_input.dat

    cat $comm_dir/prev_input.dat

  end

  echo " ichannel -- input limit: $max_inputs " >> $comm_dir/ichannel.log
  exit 2
