#! @PERL_PATH@

use POSIX; # for mktime
use Getopt::Std;

getopts('h:') || die;
if ($opt_h) { # hours
   $then = time - 60*60*$opt_h
} else {
   $then = 0
}

my $file = shift @ARGV;

open(FILE, "<$file") || die "open: \"$file\": $!\n";

my @VRULE = ('COMMENT:\n');
while (<FILE>) {
   @F = split;
   my $date = shift(@F);
   my $time = shift(@F);
   if ("$date $time" !~ m|^(\d\d\d\d)/(\d\d)/(\d\d) (\d\d):?(\d\d)$|) {
      warn "bad date/time: \"$date $time\"! (skipping)\n";
      next
   }

   my $whence = mktime($6,$5,$4,$3,$2-1,$1-1900,0,0,-1);
   next unless $whence > $then;
   push(@VRULE, sprintf("VRULE:%s#ff0000:$date $time @F", $whence),
                'COMMENT:\n');
}
   
close(FILE);
   
if (@ARGV) {
   exec @ARGV, @VRULE;
   die "exec $ARGV[0]: $!\n"
} else { # for debugging
   print join("\n", @VRULE), "\n"
}
