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

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') || die "\nTry: $script --help for more information.\n\n";

if ($opt_help) { 
 
    print <<"EndOfHelp";
Usage: $script [OPTION] host server
 
  --help   This page.
  --f      Force the addition of the record overriding warnings.

  host     The DNS zone you want to add an NS record for.
  server   The host which acts as primary for the zone.  This should be
           a fully qualified name and end in '.'
 
EndOfHelp
    
    exit;

} # End if - help

$name = $ARGV[0];

if (! $name) {

    die "\nError.  Try: $script --help for more information.\n\n";

} else {

    $name = &zone_or_subnet ($name);

}

$server = $ARGV[1];

if (! $server) {

    die "\nError: There are illegal chars in the server argument or it wasn't entered.\n\n";

} else {

    $server = &verify_name ($server);

}

if ((($x = &verify_zone ($name)) eq 'PRIMARY') || ($x eq 'SEC_W_GLUE')) {

    $host = '@';
    $zone = $name;

} else {

    die "Error: Cannot add NS for $name -- no .primary file.\nTo create use: new_primary --nosoa --zone=$name --f\n";

}

&rcs_co ($zone, '.primary');
@file = &read_file ($zone, '.primary');
@recs = &find_record (&build_record ($host, 'NS', '', ''), @file);
@crecs = &find_record (&build_record ($host, 'CNAME', '', ''), @file);

if ($#crecs >= 0) {

    $output = "\nExisting CNAME Record: $name already has the following CNAME record:\n\n";
    
    foreach $rec (@crecs) {

	$output .= "$rec";

    }

    $output .= "\nThe CNAME will have to be removed before an MX record can be added.\n";

} elsif (($#recs >= 0) && (! defined ($opt_f))) {

    $output = "\nExisting NS Record(s)\n$name already has the following NS records:\n\n";

    foreach $rec (@recs) {

	$output .= "$rec";

    }

    $output .= "\nUse the --f option to add new NS records.\n";

} else {

    if ($#recs >= 0) {
	
	foreach $rec (@recs) {

	    if ($rec =~ m/^$host\s+IN\s+NS\s+$server\s+/) {

		$duplicate = 1;
		$output = "\nDuplicate record.\n";

	    }

	}

    }

    if (! defined ($duplicate)) {

	$record = &build_record ($host, 'NS', $server, '');
	($result, @file) = &add_record ($record, @file);
    
	if ($result eq 'ADDED') {

	    &write_file ($zone, '.primary', @file);
	    $output = "\nRecord Added: NS added for $host in $zone\n";
	    $change = 1;
	
	} elsif ($result eq 'DUPLICATE') {

	    $output = "\nDuplicate Record: Identical NS record for $host in $zone\n";

	} else {

	    $output = "\nError: This isn't supposed to happen...\n";

	}

    }

}

&rcs_ci ($zone, '.primary', "Script $script: added NS record for $host", $change);
print "$output\n";

exit;

