#!/usr/local/bin/perl
# load MailTable.src into mailertab
#
# $Id: loadmail.pl,v 1.4 1999/02/11 16:15:59 ptitz Exp $
# $Log: loadmail.pl,v $
# Revision 1.4  1999/02/11 16:15:59  ptitz
# *** empty log message ***
#
# Revision 1.3  1999/02/11 16:08:32  ptitz
# *** empty log message ***
#
# Revision 1.2  1999/02/11 16:03:58  ptitz
# *** empty log message ***
#
# Revision 1.1  1999/02/11 15:49:55  ptitz
# *** empty log message ***
#


use Pg;

$connect_args = 'dbname=ptitz';    
$indir = "./";

$conn = Pg::connectdb($connect_args);
die "Pg::connectdb: $connect_args: ", $conn->errorMessage, "\n"
   unless PGRES_CONNECTION_OK eq $conn->status;

$result = $conn->exec("BEGIN");
die $conn->errorMessage, "\n" 
    unless PGRES_COMMAND_OK eq $result->resultStatus;


if ($ARGV[0]) 
{
   &load_mail_table($ARGV[0]);
   &load_domain_table($ARGV[0]);
} 
else 
{
# got the hosts list
  local (@hosts);
  $result = $conn->exec("SELECT value FROM validation WHERE typ = 'h'");
  die $conn->errorMessage, "\n" unless PGRES_TUPLES_OK eq $result->resultStatus;
  push(@hosts, $row[0]) while (@row = $result->fetchrow);
  foreach $host (@hosts) 
  {
     &load_mail_table($host);
     &load_domain_table($host);
  }
}

$result = $conn->exec("commit");
die $conn->errorMessage, "\n" 
    unless PGRES_COMMAND_OK eq $result->resultStatus;

exit(0);

sub load_mail_table 
{
   local($host) = @_;
   
   my $infile;
   $infile = $indir . "mailertab.src." . $host;

   open(IN, $infile) || do {
     print STDERR "Can't open $infile: $!\n";
     return;
   };

   $result = $conn->exec("DELETE FROM mailertab WHERE host = '$host'");
   die "delete: ",$conn->errorMessage,"\n"
      unless PGRES_COMMAND_OK eq $result->resultStatus;
 
   local ($domain, $target, $other, $method);
   local ($lineno);

   while (<IN>)
   {
    $lineno++;
    s/\#.*$//;
    ($domain, $target, $other) = split;
    print "Bad line $lineno\n", next unless $target;
    ($method, $target) = split(/:/, $target, 2);
    $result = $conn->exec("INSERT INTO mailertab " .
                     "(host, domain, method, target, who) VALUES " .
                     "('$host', '$domain', '$method', '$target', 'tabload')");
    die "$infile:$lineno: ",$conn->errorMessage,"\n" 
       unless PGRES_COMMAND_OK eq $result->resultStatus;
   } 
   close(IN);
   print STDERR "$infile: OK\n";
   return;
}

sub load_domain_table 
{
   local($host) = @_;
   
   my $infile;
   $infile = $indir . "domaintab.src." . $host;

   open(IN, $infile) || do {
     print STDERR "Can't open $infile: $!\n";
     return;
   };

   $result = $conn->exec("DELETE FROM domaintab WHERE host = '$host'");
   die "delete: ",$conn->errorMessage,"\n"
      unless PGRES_COMMAND_OK eq $result->resultStatus;
 
   local ($domain, $target, $other, $method);
   local ($lineno);

   while (<IN>)
   {
     $lineno++;
     s/\#.*$//;
     ($domain, $target, $other) = split;
     print "Bad line $lineno\n", next unless $target;
     next if $domain =~ /\.UUCP$/i;
     ($method, $target) = split(/:/, $target, 2);
     $result = $conn->exec("INSERT INTO domaintab " .
                     "(host, domain, target, who) VALUES " .
                     "('$host', '$domain', '$target', 'tabload')");
     die "$infile:$lineno: ",$conn->errorMessage,"\n" 
       unless PGRES_COMMAND_OK eq $result->resultStatus;
   } 
   close(IN);
   print STDERR "$infile: OK\n";
   return;
}
