#!/usr/bin/perl

sub prefix_dns {
    print DNSFILE "; Begin of prefix\n";
    print DNSFILE "@               IN      SOA     riverotte.fr. ns.riverotte.fr. (\n";
    print DNSFILE "                                299656302       ; Serial\n";
    print DNSFILE "                                600   ; Refresh\n";
    print DNSFILE "                                30    ; Retry\n";
    print DNSFILE "                                20000  ; Expire\n";
    print DNSFILE "                                6048 ) ; Minimum TTL\n";
    print DNSFILE ";\n";

    print DNSFILE "@               IN      NS      moby.riverotte.fr.\n";
    print DNSFILE "@               IN      MX      10 woodbox\n";
    print DNSFILE "woodbox         IN      MX      10 woodbox\n";
    print DNSFILE "; End of prefix\n\n";
}

sub prefix_reverse {
print REVFILE "@               IN      SOA     riverotte.fr. ns.riverotte.fr. (
                                  14394302       ; Serial
                                  600   ; Refresh
                                  30    ; Retry
                                  20000  ; Expire
                                  6048 )  ; Minimum TTL
;
@               IN      NS      moby.riverotte.fr.
@               IN      MX      woodbox.riverotte.fr.\n\n"
}

if ($#ARGV != 0)
{
    print "Et le fichier ?";
    exit;
}

open(INFILE, $ARGV[0]);
open(DNSFILE, ">riv.fr");
open(REVFILE, ">riv.rev");

prefix_dns();
prefix_reverse();

my $line = 0;

LINE: while(<INFILE>)
{
    $line++;
    next LINE if /^#/;

    chomp;
    s/(\t)+/\t/;

    my ($name, $ip, $aliases) = split("\t");

    if(!$name || !$ip)
    {
	print "Erreur de nom ou d'IP @ ligne $line\n";
	exit;
    }
    
    print DNSFILE "$name\t\tA\t\t$ip\n";

    my @alias = split(",", $aliases);

    foreach my $a (@alias) {
	print DNSFILE "$a\t\tCNAME\t\t$name\n";
    }

    $ip =~ /.*\.(.*)\.(.*)/;
    print REVFILE "$2.$1\t\tPTR\t\t$name.riverotte.fr.\n";
}

close(INFILE);
close(DNSFILE);
close(REVFILE);
