#!/usr/bin/perl
$username=$ARGV[0];
if ($username eq '') {
  print "need a user name!\n";
  exit 1;
}
$spoolfile="/var/spool/mail/$username";

if (! open(RCFROMAB, "> /home/$username/Procmail/rc.fromaddressbook")) {
  print "cannot open /home/$username/Procmail/rc.fromaddressbook!\n";
  exit 1;
}

print FROMAB "\n";

if (open(ABCSV, "< /home/procmail/$username.csv")) {
    $count=0;
    print RCFROMAB ':0:'."\n";
    print RCFROMAB 
        '* ^(((Resent-)?(From|Sender)|X-Envelope-From):|>?From ).*(\\'."\n";
    
    while ( $line = <ABCSV> ) {
      if ($line =~ /.*?,"([\w\.\-]*?@[\w\.\-]*)",/) {
          print RCFROMAB '('.$1."|\\\n";
        if ($count++ > 256) {
          print RCFROMAB '(thisshouldnotmatchanything))'."\n";
          print RCFROMAB "$spoolfile"."\n\n";
          $count=0;
          print RCFROMAB ':0:'."\n";
          print RCFROMAB 
            '* ^(((Resent-)?(From|Sender)|X-Envelope-From):|>?From ).*(\\'."\n";
        }
      }
    }
    print RCFROMAB '(thisshouldnotmatchanything))'."\n";
    print RCFROMAB "$spoolfile"."\n\n";
}

if (! open(RCEXEMPT, "< /home/procmail/Procmail/rc.exempt")) {
  print "cannot open /home/procmail/Procmail/rc.exempt!\n";
  exit 2;
}

while ( $line = <RCEXEMPT> ) {
  if ($line !~ m|/var/spool/mail/sauer|) {
      print RCFROMAB $line;
  } else {
      print RCFROMAB "$spoolfile"."\n\n";
  }
}

print FROMAB "\n";
