#!/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';

umask 0113;

%soa = ();
%primary = ();

open (ZONEINFO, $zoneinfo) || die "Can't open $zoneinfo: $!\n";

ZONE_INFO_READ: while (<ZONEINFO>) {

    chomp;
    ($zone, $class) = split;
    $path = $tree_root.'/'.join ('/', reverse (split ('\.', $zone))).'/';
    die "No zone.  Something is wrong with .zoneinfo.\n" unless $zone;

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

	$soa{$zone} = [0, -M _, []];

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

	    push (@{$soa{$zone}->[2]}, $zone);

	    if (-M _ < $soa{$zone}->[1]) {

		$soa{$zone}->[0] = 1;

	    }

	}
	    
    } elsif (-e "$path.primary") {

	$primary{$zone} = -M _;

    }

}


close (ZONEINFO);

DEFERED_ZONE: foreach $zone (keys (%primary)) {

    $z = $zone;

    while ($z =~ s/^[^.]+\.//o) {

	last if defined ($soa{$z});

    }

    if (!$z) {

	print "Data for $zone not included in any db file!\n";
	next DEFERED_ZONE;

    }

    push (@{$soa{$z}->[2]}, $zone);
	    
    if ($primary{$zone} < $soa{$z}->[1]) {

	$soa{$z}->[0] = 1;

    }

}

undef %primary;

open (RMZONES, ">>$tree_root/.rmzones") || die "Cannot open $tree_root/.rmzones: $!\n";

SOA_LOOP: foreach $zone (keys (%soa)) {

    $path = join ('/', $tree_root, reverse (split ('\.', $zone)), '');

    if ($soa{$zone}->[0]) {

	print "BUMP SN: Incrementing the serial number for $zone\n";
	$co_ok = system "$co", '-l', '-q', "$path.soa";
	print "RCS ERROR: Couldn't check out $path.soa\n", next SOA_LOOP if $co_ok;
        open (SOA, "+<$path.soa") || die "Can't open $path.soa: $!";

	$soa = '';

	while (<SOA>) {

	    $soa .=$_;

	}

	$barf = $soa =~ s/^([ \t]*(;.*)?\n)*(\@[ \t]+IN[ \t]+SOA[ \t]+\S+[ \t]+\S+[ \t]+[\(\s]+)([ \t]*(;.*)?\n)*([ \t]*)(\d+)(\s+)/"$1$3$4$6".($7+1)."$8"/es;

	print "SERIAL NUMBER for $zone was not incremented.\n" unless $barf;
	seek (SOA, 0, 0);
	print SOA $soa;
	close (SOA);
	system "$ci", '-u', '-mScript build_db: Incremented Serial Number', '-q', "$path.soa";
	print RMZONES "${zone}bak\n";

    } else {

	next SOA_LOOP if ((-e "$dbfiles/${zone}db") && (-M _ < $soa{$zone}->[1]));

    }

    # print "Building: $dbfiles/${zone}db\n";
    open (DB, ">$dbfiles/${zone}db") || die "Can't open $dbfiles/${zone}db: $!\n";
    $erase = 0;
    open (SOA, "$path.soa") || die "Can't open $path.soa: $!";

    while (<SOA>) {

	print DB $_;

    }
    
    close SOA;

    foreach $primary (@{$soa{$zone}->[2]}) {

	# print "ZONE: $zone -- PRIMARY: $primary\n"; 
	$path = join ('/', $tree_root, reverse (split ('\.', $primary)), '');
	open (PRIMARY, "$path.primary") || die "Can't open $path.primary: $!";

	print DB "\$ORIGIN $primary\n" unless $primary eq $zone;
	
	while (<PRIMARY>) {

	    print DB $_;

	}

	close (PRIMARY);

    }

    close (DB);

}

close RMZONES;

if (-z "$tree_root/.rmzones") {

    unlink "$tree_root/.rmzones";

}
    
exit;
