#!/usr/local/bin/perl
#
#
# Convert FIDOGATE hosts list to DNS MX record
#

use strict;
use Getopt::Std;
use FileHandle;


my $PROGRAM = 'hosts2dns';
my $VERSION = '0.0 $Revision: 5.2 $ ';

#my $DEF_MX  = "10\tmorannon.fido.de;100\tmonster.informatik.uni-oldenburg.de";
my $DEF_MX  = "10\tmorannon.fido.de";
my $DEF_EXCL= "berlin\$";

# Common configuration for perl scripts 
##############################################################################
#
#
# Perl functions to read FIDOGATE config file,
# included by <INCLUDE config.pl> when running subst.pl
#

my %CONFIG;

# specials for DosDrive and Zone
my %CONFIG_dosdrive;
my %CONFIG_zone;



my %CONFIG_default =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"toss_toss", "%S/toss/toss",
	"newslibdir", "/usr/local/news",
	"lock_dbc", "dbc",
	"seq_pkt", "%V/seq/pkt",
	"routing", "%C/routing",
	"seq_msgid", "%V/seq/msgid",
	"btbasedir", "/var/spool/fido/bt",
	"outpkt_mail", "%S/outpkt/mail",
	"netmaildir", "/var/spool/fido/bt/netmail",
	"vardir", "/var/db/fidogate",
	"tick_hold", "%B/tick",
	"toss_bad", "%S/toss/bad",
	"lock_tic_hist", "tic_hist",
	"spyes", "%C/spyes",
	"history", "%V/history",
	"seq_pack", "%V/seq/pack",
	"libexecdir", "/usr/local/libexec/fidogate",
	"lockdir", "/var/run/fidogate",
	"seq_mail", "%V/seq/mail",
	"toss_pack", "%S/toss/pack",
	"config_gate", "%C/fidogate.conf",
	"config_main", "%C/fidogate.conf",
	"seq_news", "%V/seq/news",
	"uuinbound", "/var/spool/fido/bt/uuin",
	"areas", "%C/areas",
	"inn_batchdir", "/usr/local/news/spool/outgoing",
	"newsbindir", "/usr/local/news/bin",
	"charsetmap", "%L/charset.bin",
	"logfile", "%G/log",
	"outrfc_mail", "%S/outrfc/mail",
	"seq_tick", "%V/seq/tick",
	"seq_toss", "%V/seq/toss",
	"lock_history", "history",
	"outpkt", "%S/outpkt",
	"uplinks", "%C/uplinks",
	"logdir", "/var/log/fido/gate",
	"aliases", "%C/aliases",
	"passwd", "%C/passwd",
	"hubroutedb", "%V/route",
	"inbound", "/var/spool/fido/bt/in",
	"configdir", "/usr/local/etc/fido/gate",
	"newsetcdir", "/usr/local/news/etc",
	"acl", "%C/acl",
	"seq_split", "%V/seq/split",
	"outpkt_news", "%S/outpkt/news",
	"hosts", "%C/hosts",
	"newsvardir", "/usr/local/news/db",
	"ftpinbound", "/var/spool/fido/bt/ftpin",
	"spooldir", "/var/spool/fido/gate",
	"outrfc_news", "%S/outrfc/news",
	"seq_ff", "%V/seq/ff",
	"bindir", "/usr/local/bin",
	"dbc_history", "%V/bdc",
	"newsspooldir", "/usr/local/news/spool/articles",
	"tic_history", "%V/tic_hist",
	"packing", "%C/packing",
	"toss_route", "%S/toss/route",
	"pinbound", "/var/spool/fido/bt/pin",
	"ftnacl", "%C/ftnacl",
     );
my %CONFIG_abbrev =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"G", "logdir",
	"K", "lockdir",
	"S", "spooldir",
	"C", "configdir",
	"P", "pinbound",
	"I", "inbound",
	"V", "vardir",
	"L", "libexecdir",
	"B", "btbasedir",
	"N", "bindir",
     );



sub CONFIG_read {
    my($file) = @_;
    my($key, $arg);
    local *C;

    $file = CONFIG_expand($file);

    open(C,"$file") || die "config.pl: can't open config file $file\n";
    while(<C>) {
	chop;
	next if( /^\s*\#/ );	# comments
	next if( /^\s*$/  );	# empty
	s/\s*$//;		# remove trailing white space
	s/^\s*//;		# remove leading white space
	($key,$arg) = split(' ', $_, 2);
	$key =~ tr/A-Z/a-z/;
	if($key eq "include") {
	    CONFIG_read($arg);
	    next;
	}
	if($key eq "dosdrive") {
	    my ($d, $path) = split(' ', $arg);
	    $CONFIG_dosdrive{lc($d)} = $path;
	    next;
	}
	if($key eq "zone") {
	    my ($z, $rest) = split(' ', $arg, 2);
	    $CONFIG_zone{$z} = $rest;
	    next;
	}
	$CONFIG{$key} = $arg if(!$CONFIG{$key});
    }
    close(C);
}


sub CONFIG_get1 {
    my($key) = @_;
    my($ukey);

    $ukey = $key;
    $ukey =~ tr/a-z/A-Z/;
    return $ENV{"FIDOGATE_$ukey"} if($ENV{"FIDOGATE_$ukey"});

    return $CONFIG{$key} if($CONFIG{$key});
    return $CONFIG_default{$key};
}


sub CONFIG_get {
    my($key) = @_;
    my($ret);
    my($exp);

    $key =~ tr/A-Z/a-z/;
    return CONFIG_expand( CONFIG_get1($key) );
}


sub CONFIG_expand {
    my($v) = @_;
    my($exp);

    if($v =~ /^%([A-Z])/) {
	$exp = CONFIG_get1($CONFIG_abbrev{$1});
	$v =~ s/^%./$exp/;
    }

    return $v;
}


sub CONFIG_debug {    
    my($key);

    for $key (keys %CONFIG) {
	print "$key = $CONFIG{$key} -> ", CONFIG_get($key), "\n";
    }
}

##############################################################################



##### Command line ###########################################################
use vars qw($opt_v $opt_h $opt_c $opt_H $opt_M $opt_X);
getopts('vhc:H:M:X:');

if($opt_h) {
    print STDERR
      "\n",
      "$PROGRAM --- Convert FIDOGATE hosts list to DNS MX records\n",
      "\n",
      "usage:  $PROGRAM [-vh]\n",
      "          -v               verbose\n",
      "          -h               this help\n",
      "          -c CONFIG        FIDOGATE config file\n",
      "          -H HOSTS         FIDOGATE hosts file\n",
      "          -M \"P MX;P MX\"   list of MX\n",
      "          -X PATTERN       exclude hosts matching PATTERN\n",
      "\n";

    exit 1;
}

# read config
my $CONFIG = $opt_c ? $opt_c : "%C/fidogate.conf";
CONFIG_read($CONFIG);
my $HOSTS  = $opt_H ? $opt_H : CONFIG_get("Hosts");
my $MX     = $opt_M ? $opt_M : $DEF_MX;
my $XPAT   = $opt_X ? $opt_X : $DEF_EXCL;



##### Main ###################################################################

my %hosts;
my $h;

print "Reading host list from $HOSTS ...\n" if($opt_v);
open(F, "<$HOSTS") || die "$PROGRAM: can't open $HOSTS: $!\n";
while(<F>) {
    chop;
    s/\s*\#.*$//;
    next if(/^\s*$/);
    (undef,$h) = split(' ');
    next if($h =~ /$XPAT/);
    $hosts{$h} = 1;
#    print "  host=$h\n" if($opt_v);
}
close(F);
print "Hosts: ", join(' ', sort keys %hosts), "\n" if($opt_v);

my @mx;
my $i;

@mx = split(/[;,]/, $MX);

for $h (sort keys %hosts) {
    for $i (0..$#mx) {
	$mx[$i] .= "." unless($mx[$i] =~ /\.$/);
	if($i == 0) {
	    print "$h\t";
	    print "\t" if(length($h) < 8);
	    print "IN\tMX\t", $mx[$i], "\n";
	}
	else {
	    print "\t\tIN\tMX\t", $mx[$i], "\n";
	}
    }
}



exit 0;
