#!/afs/athena/contrib/perl/perl
#
# MPQS Mail unparser, to unparse mail received from MPQS processes
# into a file of partials and fulls, counting the number of inputs
# from each machine by ID.
#
# Created by: Derek Atkins <warlord@MIT.EDU>
#
# $Source$
# $Author$
#
# $Log$
#

$FS = '#';			# field separator from -F switch
$ID = 1;			# Running ID number
$fulls = "fulls";		# filename of full relations
$parts = "partials";		# filename of partial relations
$log = "sep-log";		# Log file

open(F, ">>$fulls");		# Open the file for Fulls
open(P, ">>$parts");		# Open the file for Partials
open(L, ">>$log");		# Open log file of data retreived

while (<>)			# While there is input left....
{			       
    ($Fld1,$Fld2) = split(/[\#\n]/, $_, 9999);

    $Fld2 =~ s/\\//g;

# Separate the partials and fulls into their respective files.

    if ($Fld1 eq "par$ID") {
	if ($go_p) {
	    print P "\n";
	}
	$go_p = 1;
	print P $Fld2;
    }
    if ($Fld1 eq "par${ID}c") {
	print P $Fld2;
    }
			    
    if ($Fld1 eq "ppr$ID") {
	if ($go_p) {
	    print P "\n";
	}
	$go_p = 1;
	print P $Fld2;
    }
    if ($Fld1 eq "ppr${ID}c") {
	print P $Fld2;
    }
			    
    if ($Fld1 eq "ful$ID") {
        if ($go_f) {
	    print F "\n";
        }			 
        $go_f = 1;		 
	print F $Fld2;
    }
    if ($Fld1 eq "ful${ID}c") {
	print F $Fld2;
    }

# Add the log information, so we know what data we've received.

    ($Type,$Num,$From,$FromID) = split(' ', $Fld1);
    if ($From eq "from") {
	printf(L "%s %d from $FromID\n", $Type, $Num, $FromID);
    }
	
}
# Cleanup... Add trailing newlines if needed.			  
if ($go_p) {
    print P "\n";
}

if ($go_f) {
    print F "\n";
}

# Close the files.
close(F);
close(P);
close(L);
