#!/usr/bin/perl -w
#
# 2003-04-09 Dobrica Pavlinusic <dpavlin@pliva.hr>
#
# take a directory of inboxes and re-mail them through sendmail
# (to check for viruses, expand aliases/forwards etc etc)
#

$|=1;

#my @dir = ( '/var/mail' );
my @dir = ( '/home/dpavlin/x/mail' );

#my $sendmail = "| cat >> /tmp/sendmail";
my $sendmail = "| /usr/sbin/sendmail -t";

use File::Find;
find(\&remail, @dir);
sub remail { 
	return if (! -r $_ || -z $_ || m/^\./);
	my $inbox = $_;
	print "inbox: $_ ";
	open(INBOX,$_) || warn "can't open inbox $_: $!";

	my $mails = 0;

	open(SENDMAIL, $sendmail) || die "sendmail: $!";
	while(<INBOX>) {
		if (/^From /) {
			close(SENDMAIL);
			open(SENDMAIL, $sendmail) || die "sendmail: $!";
			$mails++;
			print ".";
		}
		print SENDMAIL $_;
	}
	close(SENDMAIL);

	print " $mails mails\n";
}

