#!/usr/bin/perl

$| = 1;  # autoflush

if ($#ARGV < 0) { 
  die "Usage: monitor.pl [files to monitor changes to]\n";
}

$cmd = "./mboxparser.sh";
@targets = @ARGV;
print "Monitoring: ", join(' ', @targets), "\n";

# initialize the %last hash for last mod time of each file.
foreach $t (@targets) {
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks) = stat($t);
  $last{$t} = $mtime;
}

$i = 0;
while(1)
{
  foreach $t (@targets) {
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks) = stat($t);

    if ($mtime != $last{$t}) {
      print "\nCHANGE DETECTED to $t!\n";
      print `$cmd`;
      $last{$t} = $mtime;
    } elsif ($i % 3600 == 0) { print "."; }
  }

  sleep(10);
  $i++;
}

