#!/usr/bin/perl

#
# ls -1 . | /usr/local/scripts/find_headers_dupes_stdin.pl | perl -n -e 'print $1."\n" if (/^(\S+): /)' | xargs rm
#

use strict;

my(%headers);

while (my $fname=<>) {
    $fname =~ s/\n$//;
    unless (open(MSG, $fname)) {
	print(STDERR "couldn't open file '$fname'\n");
    } else {
	my($exit) = 0;
	my($msg_id, $date, $date_delivery, $from, $to, $subject);
	while ((my $line = <MSG>) and (!$exit)) {
	    chomp($line);
	    if ($line =~ /^Message-ID: <[^>]+>$/i) {
		$msg_id .= $line;
	    } elsif ($line =~ /^Delivery-date:/i) {
		$date_delivery .= $line;
	    } elsif ($line =~ /^Date:/i) {
		$date .= $line;
	    } elsif ($line =~ /^From:/i) {
		$from .= $line;
	    } elsif ($line =~ /^To:/i) {
		$to .= $line;
	    } elsif ($line =~ /^Subject:/i) {
		$subject .= $line;
	    }
	    $exit = 1 if ($line =~ /^[\r\n]*$/);
	}
	close(MSG);
	print ("$fname: ".$headers{"$msg_id\t$date\t$date_delivery\t$from\t$to\t$subject"}."\n") if ($headers{"$msg_id\t$date\t$date_delivery\t$from\t$to\t$subject"});
	$headers{"$msg_id\t$date\t$date_delivery\t$from\t$to\t$subject"} .= ":$fname";
    }
}
