#!/usr/bin/perl

# by simon@josefsson.org 2001-05-13

use strict;
use Net::DNS;

my ($res) = new Net::DNS::Resolver;
my ($out) = shift;

die "`$out' does not exist" if ! -d $out;

sub get_ns (@_)
{
    my ($domain) = shift;
    my (@out);
    my ($query, $rr);

    $query = $res->query($domain, "NS");
    if ($query) {
	foreach $rr ($query->answer) {
	    next unless $rr->type eq "NS";
	    push @out, $rr->nsdname;
	}
    }
    else {
	print STDERR "query failed: ", $res->errorstring, "\n";
    }

    return @out;
}

sub get_axfr (@_)
{
    my ($zone) = shift;
    my ($ns) = shift;

    $res->nameservers($ns);
    return $res->axfr($zone);
}

# main

my ($domain, @ns, $ns);
my ($date) = `date +%Y%m%d`; chop $date;


foreach $domain (@ARGV) {
    @ns = get_ns($domain);

    foreach $ns (@ns) {
	my ($cmd) = "test -f $out/dont.${domain}$ns || dig axfr $domain \@$ns > $out/dns.${domain}$date.$ns.soa";
	system("logger \"Running $cmd...\"");
	system($cmd);
    }
}
