#!/usr/bin/perl
# isp2tiny.pl (C)2008 Jan-Piet Mens

use strict;
use DBI;

my %NSERV = (
		0	=> '192.168.100.1',
		1	=> '192.168.100.32',
		2	=> '192.168.100.42',
		3	=> '192.168.100.44',
	);

my $dbdsn = 'DBI:mysql:tiny:localhost';
my $dbh = DBI->connect($dbdsn, 'dnsadmin', 'hah!')
	or die "Can't connect to $dbdsn";

$dbh->{RaiseError} = 1;

my $q = "SELECT id,cust,name,ns1,ns2,UNIX_TIMESTAMP(modif) AS secs \
		FROM zones ORDER BY cust";
my $sth = $dbh->prepare($q);

$sth->execute() or die $dbh->errmsg;

# Bind Perl variables to columns:
my ($id, $cust, $name, $ns1, $ns2, $modif);
$sth->bind_columns(\($id, $cust, $name, $ns1, $ns2, $modif));

while ($sth->fetch) {
	print "# cust=$cust. Modified " . localtime($modif) . "\n";

	my $mname = "dns${ns1}.isp.net";
	my $dns2  = "dns${ns2}.isp2.net";
	my $ser = $modif;
	print "Z${name}:${mname}:hostmaster.isp.net:${ser}:10800:900:604800:3600\n";
	print ".${name}:" . $NSERV{$ns1} . ":$mname:\n";
	print ".${name}:" . $NSERV{$ns2} . ":$dns2:\n";
	print "+${name}:192.168.1.20:\n";
	print "Cwww.${name}:${name}:\n";
	print "\@${name}:192.168.9.20:mail.${name}:10:\n";
	print "\n";
}

$sth->finish;
$dbh->disconnect;
exit 0;

