#!/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', 'first', 'min=i') || die "\nTry: $script --help for more information.\n\n";

if ($opt_help) { 
 
    print <<"EndOfHelp";
Usage: $script [OPTION] host ip
 
  --help   This page.
  --first  Add host to first avaliable number in the allocated piece
           of the subnet.  This is checked against the .subnets file.
  --min=i  The lowest number you want to assign automatically when using --first.

  host     The fully qualified DNS name of the host you want to add.
  ip       The IP address of the host you want to add or the subnet
           you want to add to if --first is used.
 
EndOfHelp
    
    exit;

} # End if - help

$host = $ARGV[0];

if (! $host) {

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

} else {

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

}

$ip = $ARGV[1];

if (! $ip) {

    die "\nYou must enter an IP address.  Try: $script --help for more information.\n\n";

} elsif (defined ($opt_first)) {

    die "\nWith --first you must enter a subnet.\nTry: $script --help for more information.\n\n" unless &verify_subnet ($ip);

} else {

    $ip = &verify_ip ($ip, '');

}

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

if (! $opt_first) {

    ($host_num, $zone) = split ('\.', $zone, 2);
    $ip =~ s/\.\d+$//;

}

die "\nWe are not primary for $zone.\nUse new_primary to create the zone.\n\n" unless (($x = &verify_zone ($zone)) eq 'PRIMARY') || ($x eq 'SEC_W_GLUE');

%free_num = &allocated_subnet_hash ($zone);
%free_num = &free_subnet_hosts ($zone, %free_num);

if ($opt_first) {

    @free_num = sort numb (keys %free_num);

    foreach $num (@free_num) {

	$host_num = $num if $num >= $opt_min;
	last if defined ($host_num);

    }

    if (! defined ($host_num)) {

	die "\nNo avaliable numbers above $opt_min.\n\n";

    }

} elsif (! defined ($free_num{$host_num})) {

    die "\n$host_num is already in use in $zone.\n\n";

}

&rcs_co ($zone, '.primary');
@file = &read_file ($zone, '.primary');
$record = &build_record ($host_num, 'PTR', $host, '');
($result, @file) = &add_record ($record, @file);

if ($result eq 'ADDED') {
    
    &write_file ($zone, '.primary', @file);
    $output = "\nRecord Added: $host_num [$host] added to $zone\n";
    $change = 1;

} elsif ($result eq 'DUPLICATE') {

    $output = "\nDuplicate Record: $host_num [$host] is already in $zone\n";

} else {

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

}

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

exit;

