#!/usr/local/bin/perl
#
# forwardmail.pl
# 2008.02.16 by OGAWA Noriaki
# 2008.02.20 last updated by OGAWA Noriaki
#

$HOME = $ENV{'HOME'};
$addresslistfile = "$HOME/.forwardlist";
$rmailpath = "/usr/bin/rmail";

if(!open(AL, "<$addresslistfile")){
  exit -1;
}
flock(AL, 1);
@addresslist = <AL>;
close AL;

$addresses = join(" ", @addresslist);
$addresses =~ s/#.*?(\n|\r|$)+/\n/g;   # remove comments
$addresses =~ s/(\s|,)+/ /g;

if(!open(RMAIL, "| $rmailpath $addresses")){
  exit -1;
}
while(<>) {
  print RMAIL;
}
close RMAIL;
