#!/usr/bin/perl
# ns v4.2.0  (c) 20.2.2013 by Andreas Ley  (u) 27.10.2025
# Handle NS records through NET-WebAPI

use warnings;
use strict;
no warnings 'uninitialized';
use sigtrap;
#use diagnostics;

# The script itself may use utf-8 encoded identifiers and literals
use utf8;
# Latin-1 codepoints are considered characters
use feature 'unicode_strings';
use locale;
# Enable UTF-8 encoding for all files (but not already open handles)
use open ':encoding(utf8)';

use Getopt::Long;
use File::Basename;
use File::Spec::Functions;
use NetAddr::IP ':lower';

use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
#warn Data::Dumper->Dump([\%hash],['*']) if ($opt_debug>0);
#warn Data::Dumper->new([\%hash],['*'])->Indent(0)->Dump if ($opt_debug>0);

my $config = catfile($ENV{'HOME'}||(getpwuid($<))[7],'.config','netdb_client.ini');

sub usage
{
	my $image = $0;
	$image =~ s!.*/!!;
	print  STDERR  "Usage: $image [-v] [-l [-r]] fqdn\n";
	print  STDERR  "-v, --verbose	Verbose mode\n";
	print  STDERR  "-l, --list	List NS record(s)\n";
	print  STDERR  "-r, --recursive	Strip subdomain(s) until SOA found\n";
	print  STDERR  "Your NET-WebAPI-Key and -Certificate locations must be specified in environment variables NET_WEBAPI_KEY and NET_WEBAPI_CERT or your NetVS-Token must be readable from $config\n" unless (-r $config || defined($ENV{'NET_WEBAPI_KEY'}) && -r $ENV{'NET_WEBAPI_KEY'} && defined($ENV{'NET_WEBAPI_CERT'}) && -r $ENV{'NET_WEBAPI_CERT'});
	exit(1);
}

my %opt;
@_ = @ARGV;
$Getopt::Long::ignorecase = 0;
GetOptions (\%opt,'debug|D:s','help|h','trace|x','verbose|v','dry-run|n','devel|N',
	'change|c','delete|d','list|l','recursive|r');
exec($^X,'-d:Trace',$0,@_) if (defined($opt{'trace'}) && !defined($Devel::Trace::TRACE));

&usage() if (defined($opt{'help'}) ||
	(defined($opt{'change'}) && (defined($opt{'delete'}) || defined($opt{'list'})) || defined($opt{'delete'}) && defined($opt{'list'})) ||
	(defined($opt{'dry-run'}) && defined($opt{'devel'})) ||
	@ARGV < (defined($opt{'change'}) ? 1 : 0));
&usage() unless (-r $config || defined($ENV{'NET_WEBAPI_KEY'}) && -r $ENV{'NET_WEBAPI_KEY'} && defined($ENV{'NET_WEBAPI_CERT'}) && -r $ENV{'NET_WEBAPI_CERT'});

if (defined($opt{'verbose'}) || defined($opt{'debug'})) {
	$|=1;
	select((select(STDERR),$|=1)[0]);
}

my (%debug,$on,$off);

sub debug
{
	my $file = shift;
	my $line = shift;
	my $prefix = defined($debug{'path'})?$file:defined($debug{'file'})?basename($file):'';
	if (defined($debug{'line'})) {
		$prefix .= ':' if (length($prefix));
		$prefix .= $line;
	}
	$prefix .= '  ' if (length($prefix));
	warn $prefix.(scalar(@_)>1 ? join(', ',map("\"$_\"",@_))."\n" : "@_\n");
}
$debug{undef} = \&debug;

if (defined($opt{'debug'})) {
	for (split(',',$opt{'debug'})) {
		if (/=/) {
			$debug{$`} = $';
		}
		else {
			$debug{$_} = 1;
		}
	}
	&debug(__FILE__,__LINE__,'%debug = '.Data::Dumper->Dump([\%debug],['*debug'])) if ($debug{'debug'}>0);
	if (defined($ENV{'TERM'})) {
		use Term::Cap;
		my $term = Tgetent Term::Cap { 'OSPEED'=>9600 };
		$on = $term->Tputs('md');
		$off = $term->Tputs('me');
	}
}

#$IO::Socket::SSL::DEBUG = $debug{'ssl'} if (defined($debug{'ssl'}));

# Enable UTF-8 encoding for already open handles
# ":utf8" would enable perl native coding which happens to be UTF-8 only on ASCII platforms
binmode(STDIN,':encoding(utf8)');
binmode(STDOUT,':encoding(utf8)');
binmode(STDERR,':encoding(utf8)') unless (defined($Devel::Trace::TRACE));

####################################################################################################################################

if (defined($opt{'devel'})) {
	use lib '.';
}
use NET::WebAPI ('ip','is_ip');

my $opt = NET::WebAPI->read_config($config) if (-s $config);
$opt->{'key'} = $ENV{'NET_WEBAPI_KEY'} if (defined($ENV{'NET_WEBAPI_KEY'}));
$opt->{'cert'} = $ENV{'NET_WEBAPI_CERT'} if (defined($ENV{'NET_WEBAPI_CERT'}));
$opt->{'debug'} = \%debug;
$opt->{'url'} = 'test' if (defined($opt{'dry-run'}));
$opt->{'url'} = 'devel' if (defined($opt{'devel'}));

my $webapi = NET::WebAPI->new($opt) or die "Can't connect to WebAPI\n";

# Change
if (defined($opt{'change'})) {
	die "Not yet implemented.\n";
}

# Delete
elsif (defined($opt{'delete'})) {
	die "Not yet implemented.\n";
}

# List
elsif (defined($opt{'list'}) || @ARGV==1) {
	for my $fqdn (@ARGV) {
		while (1) {
			my @ns = $webapi->list_ns($fqdn);
			for my $ns (@ns) {
				print "$fqdn => " if (defined($opt{'verbose'}));
				print "$ns\n";
			}
			last if (@ns || !defined($opt{'recursive'}) || $fqdn !~ /\./);
			$fqdn = &domain($fqdn);
		}
	}
}

# Create
else {
	die "Not yet implemented.\n";
}

sub domain
{
	my ($fqdn) = @_;

	$fqdn =~ s/^[^.]+\.//;

	return($fqdn);
}
