#! /usr/bin/perl -w
#
# vw-config: script for generating $mtab and $rlay from $cf
# by Jon Kuroda 
# EECS CUSG, 2003
#
use Fcntl qw(:DEFAULT :flock);
# 
# Jon should remember to actually get around to using flock/fcntl ...

$mtab="/etc/mail/mailertable";
$rlay="/etc/mail/relay-domains";
$cf="where-is-your-vw-config.cf";
$mkmap="/usr/sbin/makemap";
$mptyp="hash";

# initialize some variables
#
$server="";
$comments="";

open(MTAB, ">$mtab") || die "Can't open $mtab: $!";;
open(RLAY, "> $rlay") || die "Can't open $rlay: $!";
open(MKMAP, "|$mkmap $mptyp $mtab") || die "Can't open $mkmap program: $!";
open(CF, "$cf");

print MTAB "# vw mailertable file\n";
print RLAY "# vw relay-domains file\n";

while () {
    chomp;
    next if /^#/;
    next if /^$/;
    if (m/^server\s+(\S+)\s+(.*)/i) {
        $server = $1;
        $comments = $2;
        # insert DNS sanity check via Net::DNS
        print MTAB "\n# *********DO NOT EDIT THIS FILE***********.\n";
        print MTAB "# STOP sendmail with /etc/init.d/sendmail stop\n";
        print MTAB "# EDIT vw.cf\n";
        print MTAB "# RUN vw-config\n";
        print MTAB "# READ over /etc/mail/mailertable for obvious problems\n";
        print MTAB "# FIX any problems via GOTO EDIT\n";
        print MTAB "# DO NOT START sendmail UNLESS NO PROBLEMS via /etc/init.d/
sendmail start\n";
        print MTAB "\n# $comments\n#\n";

        print RLAY "\n# *********DO NOT EDIT THIS FILE***********.\n";
        print RLAY "# STOP sendmail with /etc/init.d/sendmail stop\n";
        print RLAY "# EDIT vw.cf\n";
        print RLAY "# RUN vw-config\n";
        print RLAY "# READ over /etc/mail/mailertable for obvious problems\n";
        print RLAY "# FIX any problems via GOTO EDIT\n";
        print RLAY "# DO NOT START sendmail UNLESS NO PROBLEMS via /etc/init.d/
sendmail start\n";
        print RLAY "\n# $comments\n#\n";
        }
    else {
        s/^\s+//;
        # insert DNS sanity check via Net::DNS
        print MTAB "$_\tesmtp:[$server]\n";
        print MKMAP "$_\tesmtp:[$server]\n";
        print RLAY "$_\n";
    }
}

close MTAB;
close RLAY;
close MKMAP;