#! /usr/bin/perl -w
use strict;

# les inn nettnavn
my %netnames = ();
my %netmasks = ();
my @nets = ();
open NAMES, "switches.txt"
or die "switches.txt: $!";
while (<NAMES>) {
	chomp;
	/81\.162\.(\d+\.\d+)\s+(\d+)\s+(\S+)/ or next;
	$netmasks{$1} = $2;
	$netnames{$1} = $3;
	push @nets, $1;
}

print <<"EOF";
// Autogenerated by make-named.pl. Do not edit manually! 

options {
        directory "/etc/bind";
	allow-query { any; };
	allow-transfer { 194.19.3.20; 81.162.254.76; }; 
	auth-nxdomain no;
	recursion yes;

	forwarders { 194.19.2.11; 194.19.3.11; }; 
	forward only;
};

zone "." { type hint; file "db.root"; };

zone "0.0.127.in-addr.arpa" {
	type master;
	file "0.0.127.in-addr.arpa";
	notify no;
};

zone "tg05.gathering.org" {
	type master;
	file "tg05.gathering.org";
	notify yes;
	allow-transfer { 81.162.254.76; };
};
zone "www.gathering.org" {
	type master;
	file "www.gathering.org";
	notify yes;
	allow-transfer { 81.162.254.76; };
};

// serversubnett
zone "254.162.81.in-addr.arpa" {
	type master;
	file "254.162.81.in-addr.arpa";
	notify yes;
	allow-transfer { 81.162.254.76; 193.0.0.0/22; };
};

zone "162.81.in-addr.arpa" {
	type master;
	allow-update { key DHCP_UPDATER; };
	notify yes;
	file "dynamic/162.81.in-addr.arpa";	
	allow-transfer { 194.19.3.20; 81.162.254.76; 193.0.0.0/22; };
};

key DHCP_UPDATER {
	algorithm HMAC-MD5.SIG-ALG.REG.INT;
	secret removed;
};

EOF

for my $net (@nets) {
	my $domain = $netnames{$net};

       my ($netmask, $numpc);
       if ($netmasks{$net} == 24) {
	       $netmask = "255.255.255.0";
	       $numpc = 256;
       } elsif ($netmasks{$net} == 25) {
	       $netmask = "255.255.255.128";
	       $numpc = 128;
       } elsif ($netmasks{$net} == 26) {
	       $netmask = "255.255.255.192";
	       $numpc = 64;
       } else {
	       die "Unknown netmask /$netmasks{$net}";
       }

       $net =~ /(\d+)\.(\d+)/ or die "Unknown net $net";
       my ($majorsubnet,$minorsubnet) = ($1,$2);

	my $gw = "82.162.$majorsubnet." . ($minorsubnet + 1);

	print <<"EOF";
zone "$domain.tg05.gathering.org" {
	type master;
	allow-update { key DHCP_UPDATER; };
	notify yes;
	file "dynamic/$domain.tg05.gathering.org";
	allow-transfer { 194.19.3.20; 81.162.254.76; };
};
EOF
}
