#!/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 0022;
$SIG{'INT'} = 'bail';

die "$tree_root doesn't exist or is not writable.\n" unless (-e $tree_root && -d _ && -w _);

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

ZONE_READ_LOOP: while (<ZONEINFO>) {

    chomp;
    s/^\s*//o;			# Kill leading whitespace
    split;

    $current_zone = $tree_root.'/'.join ('/', reverse (split ('\.', $_[0]))).'/';
    
    if (-e "$current_zone.soa") {

	checkin ("$current_zone.soa", "SOA Record for $_[0]");

    }

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

	checkin ("$current_zone.primary", "Primary Zone Data for $_[0]");

    }

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

	checkin ("$current_zone.secondary", "Secondary Data for $_[0]");

    }

}

close (ZONEINFO);

exit;


sub checkin {

    local ($file, $msg) = @_;

    $status = system $ci, '-u', '-t-$msg', $file;
    warn $!, " Couldn't check in $file: $!\n" if $status;

    1;

}


sub bail {

    close (ZONEINFO);
    exit;

}
