#!/usr/bin/perl
# cname v4.2.0  (c) 20.2.2013 by Andreas Ley  (u) 27.10.2025
# Handle CNAME 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 fqdn cname\n";
	print  STDERR  "Or:    $image -c fqdn cname\n";
	print  STDERR  "Or:    $image -d fqdn [...]\n";
	print  STDERR  "Or:    $image -l [-v] fqdn [...]\n";
	print  STDERR  "Or:    $image -t [-v] cname [...]\n";
	print  STDERR  "-c, --change		Change CNAME record\n";
	print  STDERR  "-d, --delete		Delete CNAME record\n";
	print  STDERR  "-l, --list		List CNAME record\n";
	print  STDERR  "-t, --list-target	List CNAME record(s) for target\n";
	print  STDERR  "-v, --verbose		Verbose mode\n";
	print  STDERR  "fqdn is the alias, cname is the canonical name (which must already exist)\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','list-target|t');
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'})) {
	&usage() if (@ARGV != 2);
	$webapi->update_cname(@ARGV);
}

# Delete
elsif (defined($opt{'delete'})) {
	for my $fqdn (@ARGV) {
		$webapi->delete_cname($fqdn);
	}
}

# List target
elsif (defined($opt{'list-target'})) {
	&usage() if (@ARGV < 1);
	for my $cname (@ARGV) {
		for my $fqdn ($webapi->list_cname_target($cname)) {
			print $fqdn;
			print " => $cname" if (defined($opt{'verbose'}));
			print "\n";
		}
	}
}

# List
elsif (defined($opt{'list'}) || @ARGV==1) {
	&usage() if (@ARGV < 1);
	for my $fqdn (@ARGV) {
		for my $cname ($webapi->list_cname($fqdn)) {
			print "$fqdn => " if (defined($opt{'verbose'}));
			print $cname,"\n";
		}
	}
}

# Create
else {
	&usage() if (@ARGV != 2);
	$webapi->create_cname(@ARGV);
}
