#!/usr/bin/perl

open (STDERR, ">&STDOUT");
select ((select(STDOUT), $| = 1)[0]); # Syncronize 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';

@dirs = ();
push (@dirs, $tree_root);

while ($current = pop (@dirs)) {

    $zone = $current;
    $zone =~ s/^$tree_root\/*//o;
    $zone = join ('.', reverse (split ('/', $zone)));

    opendir (DIR, $current);
    @files = readdir (DIR);
    closedir (DIR);

    foreach $file (@files) {

	next if $file eq 'RCS';
	next if $file =~ m/^\.\.?$/;

	if (-d "$current/$file") {

	    push (@dirs, "$current/$file");

	} elsif ("$file" eq '.secondary') {

	    $zone = &path_to_zone ($current);
	    # print "$zone -- ";

	    $delegated = 0;

	    for ($soa = $current; $soa ne $tree_root; $soa =~ s/\/[^\/]+$//) {

		if (-e "$soa/.soa") {

		    $delegated = 1;
		    last;

		}

	    }

	    if ($delegated) {

		# print "Delegated\n";
		$delegated_count++;

	    } else {

		# print "Not delegated\n";
		$not_delegated_count++;

	    }

	} elsif ("$file" eq '.soa') {

	    $soa_count++;

	} elsif ("$file" eq '.primary') {

	    $primary_count++;

	}
	    
    }

}

$sec_count = $not_delegated_count + $delegated_count;

print "\n";
print "SOA Records: $soa_count\n";
print "Primary Files/Domains: $primary_count\n";
print "Secondary Zones: $sec_count\n";
print " - Under one of our SOA's: $delegated_count\n";
print " - External to our SOA's: $not_delegated_count\n";
print "\n";

exit;
