#!/usr/bin/perl -w

use strict;
my $id = time() + ".$$";

my $bsmtpfile = "/home/aehlig/tmp/reminders/R-$id.bsmtp";

open (BSMTP, ">", $bsmtpfile) or die "Failed to open $bsmtpfile ($!)";

print BSMTP "HELO reminder.aehlig.linta.de\n";
print BSMTP "MAIL FROM: aehlig-direct\@linta.local\n";
print BSMTP "RCPT TO: aehlig-notify-and-direct\@linta.local\n";
print BSMTP "DATA\n";

while(<STDIN>) {
 s/^\./\.\./;
 print BSMTP $_;
};

print BSMTP ".\n";
print BSMTP "QUIT\n";

close(BSMTP) or die "Failed to cloase $bsmtpfile ($!)";

if ($ENV{'EXT4'} =~ /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/ ) {
 open (AT, "|-", "at", "-t", $ENV{'EXT4'}) or die "Failed to open at";
} else {
 open (AT, "|-", "batch") or die "Failed to open at";
}

print AT "/usr/local/bin/rsmtp < $bsmtpfile && rm $bsmtpfile";

close(AT) or die "Failed to close at";
