#! /usr/local/bin/perl

# Paul LeMahieu
# 1 Oct 1996

# This script extracts the mail headers from a Unix-style
# mail file.  Headers are started with a 'From ' and 
# end with an eventual blank line.  If any Content-Length
# fields are found, that number of characters is read
# in and discarded following the header block.  

$/ = "\n\n";    # work in paragraph mode
$* = 1;         # allow \n in input strings
open(IN,$ARGV[0]);

while($_ = <IN>) {
   # find the headers, delimited by 'From ' and a blank line
   if (/(^From )(.|\n)*/) {
      print $_;
      if (/^Content-Length: (.*)\n/) {
         $clength = $1;
         $/ = "\n";
         while ($clength > 0) {$_ = <IN>; $clength -= length($_);}  # eat up the message
         $/ = "\n\n";
      }
   }
}
