#!perl
# [eichin:19910202.0233EST] scan RMAIL files for a pattern and print out
# and index line for sorting and use by rmail-map.perl.

$upattern = $ARGV[0]; shift;
$sep = " ";

$mode = "babyl header";
$index = 1;

print "pat = $upattern, sep = <$sep>\n";

@header=();
while (<>) {
  if ($mode eq "babyl header") {
    if($_ eq "\n") {
      $mode = "message";
      next;
    }
    push(@header,$_);
  } elsif ($mode eq "message") {
    if($_ eq "\n") {
      $index++;
      next;
    }
    if($_ eq "\n") {
      $mode = "message body";
      next;
    }
    if(/$upattern/) {
      print $index,$sep,$_;
# if you want to only print any line once, just uncomment the following:
#     $mode = "message body"; next;
    }
  } elsif ($mode eq "message body") {
    if($_ eq "\n") {
      $index++; 
      $mode = "message";
      next;
    }
  }
}
