#!/usr/bin/perl -w

use strict;
use Socket;
use DBI;
use Tie::File;

my $zone_filename = 'zone.file';
my ($query, $sth, $rv);
my $zone = 'test-zone.info';


my $db = DBI->connect("dbi:Pg:dbname=named","named","", {PrintError => 0});

if (defined($DBI::err)) 
{
    print $DBI::errstr . "\n";
    exit($DBI::err);
}

open (ZONE, $zone_filename) || die "Can't open $zone_filename: $!";

while(<ZONE>)
{
    if(!/^([a-zA-Z0-9-\.\*]+)\s+in\s+(\w+)\s+(.+?)[\$\s;]/i) { next; };
    $query = "INSERT INTO dns_records (zone, host, type, data) VALUES ('$zone', '".lc($1)."', '".uc($2)."', '$3')";

    $sth = $db->prepare($query);
    $rv = $sth->execute();
    if (!defined $rv) 
    {
        print "ERROR executing query '$query' : " . $db->errstr . "\n";
        <STDIN>;
        next;
    }
    $sth->finish();

    print "QUERY OK: $query\n";
}


#Disconnect from DB
$db->disconnect();

