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

use Getopt::Long;
$Getopt::Long::autoabbrev=1;    # Allow keyword abbreviations (to uniqueness).
$Getopt::Long::getopt_compat=1; # Allow both "--foo=bar" and "+foo=bar" style.
#$Getopt::Long::option_start='(--|-|\+)';       # [Use the defaults]
#$Getopt::Long::order=$Getopt::Long::REQUIRE_ORDER; # Options can't intermingle
$Getopt::Long::order=$Getopt::Long::PERMUTE;    # or options may intermingle.
$Getopt::Long::ignorecase=1;    # Don't consider case in options.
$Getopt::Long::debug=0;         ### Debug ###

$opt_help = ($#ARGV < 0);

&GetOptions ('help', 'f', 'server=s@', 'zone=s') || die "Try $script --help for more information.\n";

if (($opt_help) || ($#ARGV >= 0)) { 
 
    print <<"EndOfHelp";
Usage: $script [OPTION]
 
  --help      This info
  --f         Force the action
  --zone=x    Zone to be secondary for
  --server=y  IP address of authoritive server - Repeatable Option
 
EndOfHelp
    
    exit;

} # End if - help

die "\nNot enough arguments.\n\nTry: $script --help for more information.\n\n" unless (defined ($opt_zone) && defined (@opt_server));

foreach $ip (@opt_server) {

    if ($ip !~ m/(\d{1,3}\.){3}\d{1,3}/o) {

	die "\nServer values need to be IP addresses.\n\nTry: $script --help for more information.\n\n";

    }

}

$zone = $opt_zone;

if ($zone =~ m/^(\d+\.)*\d+(\.in-addr.arpa\.?)?$/o) {

    $zone = &reverse_ip ($zone) unless $zone =~ m/\.in-addr\.arpa\.?$/o;
    $zone = &verify_subnet ($zone, '');

} else {

    $zone = &verify_name ($zone, '');

}

@dirs = reverse (split ('\.', lc($zone)));
$path = '/';
$rebuild = 0;

&rcs_co ('', '.zoneinfo');
open (ZONEINFO, ">>$tree_root/.zoneinfo") || die "Can't open $tree_root/.zoneinfo: $!\n";

foreach $dir (@dirs) {

    $path .= "$dir/";
    
    if (-e $tree_root.$path) {
	
	if (! -d _) {

	    die "$tree_root$path is not a directory.\n";

	}

    } elsif (mkdir ($tree_root.$path, 0770)) {

	mkdir ($tree_root.$path.'RCS', 0770) || warn "Couldn't create RCS directory in $dir.  $!\n";
	print ZONEINFO join ('.', reverse (split ('/', $path))),"\n";
	$rebuild = 1;

    } else {

	die "$tree_root$path is not a directory and could not be created.\n";
   
    }

}

close (ZONEINFO);
$path = $tree_root.$path;
system "$build_class" if $rebuild;
&rcs_ci ('', '.zoneinfo', "Script $script: added $zone");

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

    die "We are already a secondary for $zone\n";

} elsif ((-e "$path.soa") && (! defined ($opt_f))) {

    die "We are currently primary for $zone.\n\nUse the --f flag to force the zone to a secondary\n";

} elsif ((-e "$path.primary") && (! defined ($opt_f))) {

    die "We primary for $zone (SOA is in a parent zone).\n\nUse the --f flag to force the zone to a secondary\n";

} else {

    open (SECONDARY, ">$path.secondary") || die "Cannot create $path.secondary: $!\n";

    foreach $item (@opt_server) {

	print SECONDARY "$item\n";

    }

    close SECONDARY;
    rcs_new_ci ("$path.secondary", "Script $script: Secondary data for $zone");
    open (REBUILD, ">$tree_root/.rebuild") || die "Cannot create .rebuild file: $!\n";
    close (REBUILD);
    print "We are now secondary for $zone\n";

}

exit;

