use Mail::Box::Manager;
use Digest::MD5 qw(md5_base64);

use strict;

my $mgr= Mail::Box::Manager->new;
my $folder= $mgr->open(folder=>$ARGV[0], access=>'rw') || die "Cannot open $ARGV[0] as $!";
print "Processing: ".$folder->name."\n";

my %mid;

my $c=0;
my $dupe=0;

foreach($folder->messages)
{
    $c++;
    print "$c\n" if(($c % 100) == 0);
#    print $_;
    my $mid= $_->messageId();
#    my $t= $_->string();
#    print "$digest\n";
#    if(exists($md5{md5_base64($t)})) {
#	print "MD5 Duplicate - deleting\n";
#	$_->delete();
#    }
    if(exists($mid{$mid}))
    {
	next if($mid =~ /bug-/);
	print "Is a duplicate- deleting\n";
	$dupe++;
	$_->delete();
    }
    $mid{$mid} = defined;
 #   $md5{md5_base64($t)}= defined;
#    $dbh->do("INSERT INTO message (md5,message) values (".$dbh->quote($digest).
#	",".$dbh->quote($_->string).")\n");
#    $dbh->

}

#print "\n\nLeft ".(scalar keys %md5)." md5 unique messages.\n";
print "\n\nLeft ".(scalar keys %mid)." message-id unique messages.\n";
print "Removed $dupe\n\n";

$folder->close();
