#!/usr/bin/perl

open (STDERR, ">&STDOUT");
select ((select(STDOUT), $| = 1)[0]); # Synchronize STDOUT
select ((select(STDERR), $| = 1)[0]); # with STDERR
 
($dns_lib = $0) =~ s:(^|/)[^/]*$::;
($script=$0) =~ s:^.*/([^/]*)$:$1:;     # Get the simple name of this script.
 
push (@INC, $dns_lib);
 
require 5.0;
require 'dns_lib.perl';

open (ZONEINFO, "$tree_root/.zoneinfo");

ZONEINFO_LOOP: while (<ZONEINFO>) {

    ($zone, $class) = split;
    next ZONEINFO_LOOP if $zone =~ m/\.in-addr\.arpa\.$/;
    $path = &zone_to_path ($zone);
    next ZONEINFO_LOOP unless -e "$path.primary";
    open (PRIMARY, "$path.primary");


    PRIMARY_LOOP: while (<PRIMARY>) {

	next PRIMARY_LOOP unless m/^(\S+)\s+IN\s+(MX|A)\s+.+$/;
	$name = $1;
	$path = zone_to_path("$name.$zone");

	if (-e "$path.secondary") {

	    print "$name (in $zone) needs to be moved to the server with $name.$zone:\n$_";
	    print "Possible servers:";
	    open (SECONDARY, "$path.secondary");

	    while (<SECONDARY>) {

		chomp;
		print " $_";

	    }

	    close (SECONDARY);
	    print "\n\n";
    
	} elsif (-e "$path.soa") {

	    print "$name (in $zone) needs to be moved to $name.$zone:\n$_\n";

	}

    }

    close (PRIMARY);

}

close (ZONEINFO);

exit;

