#!/usr/bin/perl

use POSIX qw(strftime);
use Time::HiRes qw( time usleep ualarm gettimeofday tv_interval nanosleep 
 clock_gettime clock_getres clock_nanosleep clock stat );

$dir='/cygdrive/c/GWR_o054/2012o054/RAW_o054';
$file=0;
`rm -f STOPFS`;

if ( $ARGV[0] ){
   while ( $ARGV[0] =~ m/[-].*/ )
   { if     ( $ARGV[0] eq '-d' )
     { $dir=$ARGV[1]; shift }
     elsif ( $ARGV[0] eq '-f' )
     { $file=$ARGV[1]; shift }
   shift }
 }

$now = strftime "%y%m%d", gmtime(time); 
if ( ! $file ){ $file = $dir.'/G1'.$now.'.054 '.$dir.'/G1'.$rct.'.054' }

$s = `ls -lt $file 2> /dev/null | head -1`;

if ( ! $s ){ print "STOP: No files $file\n"; die "No files $file\n" }
$s =~ s/\n//g;
if ( ! $s ){ print "STOP: No files $file\n"; die "No files $file\n" }
@flist = split(/ +/, $s);
$oldsz=-1;

#  print "$flist[8] $flist[4]\n"  # name and size
while ( ! -r STOPFS ){
  $s = `ls -lt $file 2> /dev/null | head -1`;
  if ( ! $s ){ print "STOP: No files $file\n"; die "No files $file\n" }
  @flist = split(/ +/, $s);
  if ( $flist[4] != $oldsz ){ 
    $now = strftime "%H:%M:%S", gmtime(time);
    print "Size $flist[4] at $now\n"; $oldsz=$flist[4]; $oldtime=$flist[5]
  }
  else
  { usleep(50000) }
}
exit
