#!/usr/bin/perl

$state = "BODY" ;
$filenumber = shift @ARGV;
$FILE = ">GARBAGE.ERR";
open (FILE);
binmode FILE;

while (<>) {

    if ($state eq "HEADER")
    { 
        if (/^From: /) { print FILE $_ ; }
        if (/^Newsgroups: /) { print FILE $_ ; }
        if (/^Subject: /) { print FILE $_ ; }
        if (/^Date: /) { print FILE $_ ; }
        if (/^Message-I[dD]: /) { print FILE $_ ; }
        if (/^Keywords: /) { print FILE $_ ; }
        if (/^References: /) { print FILE $_ ; }
        if (/^$/) { print FILE $_ ; $state = "BODY"; }
    } else {
        if (/^From /) {
            $state = "HEADER" ;
	    close FILE;
	    $FILE = sprintf (">MSG%05d", $filenumber);
	    open (FILE);
	    binmode FILE;
	    $filenumber += 1;
        } else {
            if (/^MagicHutchHeader/) {
                $state = "HEADER" ;
	        close FILE;
	        $FILE = sprintf (">MSG%05d", $filenumber);
	        open (FILE);
	        binmode FILE;
	        $filenumber += 1;
            } else { print FILE $_ ; }
        }
    }

}
