#!/usr/athena/bin/perl 
#
# 01/16/97: pgreene: derived from jik's merge-babyl.pl
#           but designed to split a packf file instead of an
#           rmail-stype BABYL file
#
# 01/20/02: ngb: fixed the location of perl
#

($whoami = $0) =~ s,.*/,,;

$keep_dups = 0;

#$header_exp = "BABYL OPTIONS:.*
#Version:.*
#Labels:.*
#(Note:.*
#)*";#"
#
#$header = "BABYL OPTIONS: -*- rmail -*-
#Version: 5
#Labels:
#Note:   This is the header of an rmail file.
#Note:   If you are seeing it in rmail,
#Note:    it means the file has no messages in it.
#";#"

#$/ = "";
$/ = "\n";

$outfile = "xaa";
$max = 500000;

$usage = "Usage: $whoami [ -nomax | -max num ] [ -out file ]\n\t[ -keep_dups | +keep_dups]";

while ($ARGV[0] =~ /^[-+]/) {
    $_ = shift @ARGV;
    if (/^-nomax$/) {
	$max = 0;
    }
    elsif (/^-max$/) {
	die "Missing argument to -max.\n$usage\n" if (! @ARGV);
	$max = shift;
    }
    elsif (/^-out$/) {
	die "Missing argument to -out.\n$usage\n" if (! @ARGV);
	$outfile = shift;
    }
    elsif (/^-keep_dups$/) {
	$keep_dups = 0;
    }
    elsif (/^\+keep_dups$/) {
	$keep_dups = 1;
    }
    else {
	die "Unknown option $_.\n$usage\n";
    }
}

open(OUT, "/dev/null");	# so that it can be closed first time through

outfile: while (1) {
    close(OUT) || die "$whoami: closing $outfile: $!\n";
    open(OUT, ">$outfile") || die "$whoami: opening $outfile: $!\n";
#    print OUT $header || die "$whoami: writing to $outfile: $!\n";
    $size = length($header);
    $outfile++;

    while (<>) {
#	next if (/^\n/ || /$header_exp/);
	next if (/^\n/);
	if (/\nmessage-id:\s*([^\s].*[^\s])\s*\n/i) {
	    if (($ids{$1}++) &&	(! $keep_dups)) {
		warn "Removing duplicate $1.\n";
		next;
	    }
	}
	print OUT $_ || die "$whoami: writing to $outfile: $!\n";
	$size += length($_);
	next outfile if ($max && ($size > $max));
    }

    last;
}
