#!/usr/local/cpanel/3rdparty/bin/perl

use strict;
use Cpanel::AdminBin::Serializer ();
use Cpanel::Logger               ();
use Cpanel::PwCache              ();

my $stdin = <STDIN>;
chomp $stdin;
my ( $uid, $function, $data ) = split( / /, $stdin, 3 );

# sanitize the input; in this case, only alphanumeric, underscore, space, period, and exclamation are allowed
$data =~ s/![\w \.\!]//g;

# make a note in the logs!
my $user   = ( Cpanel::PwCache::getpwuid($uid) )[0];
my $logger = Cpanel::Logger->new();
$logger->warn("DNSmeapiAdmin called by user $user with function: $function");

if ( $function eq 'addzone' ) {
    print $data;
    &_addzone($data);

    exit(0);
}


1;


sub _addzone{
	my($domain)=@_;
	$domain='quasinhnhatbe.com';

	print "\nYour zone:$domain";

	#	parse the template with this domain
#	use Cpanel::Template;
#	my $domain_zone= Cpanel::Template::process_template(
#        'cpanel',
#        {
#            'template_file' => '/usr/local/cpanel/base/frontend/x3/dnsmeapi/named_domain_zone.tmpl',
##            'data'          => { 'domain' => $domain },
#	    'domain'	=> $domain,
#        },
#    );	
	#	
	#	get the /etc/named.conf out to see if the domain is there
	#	if it is not there add that section to
	## my $db_file = "/etc/named.conf";
	### backup
	use Cpanel::NameServer::Conf ();
	use IO::Handle ();
	use Cpanel::NameServer::Utils::BIND ();
	use Cpanel::SafetyBits::Chown ();
	use Cpanel::SafeFile ();
	
	my $new_zone = "";
	my $fh;
	my $ulock = Cpanel::SafeFile::safeopen($fh, "<",'/usr/local/cpanel/base/frontend/x3/dnsmeapi/named_domain_zone.tmpl');
	{
		local $/;
		$new_zone = <$fh>;
	}
	Cpanel::SafeFile::safeclose($fh, $ulock );
	### replace the domain
	print "\nNew zone: $new_zone";
	$new_zone =~ s#\[%\s*(\S+)\s*%\]#$domain#sg;
	print "\nNew zone: $new_zone";

	my $bind = Cpanel::NameServer::Conf->new();
 	$bind->initialize();	

	#$bind->addzone('myxxxxx.com');

##	my $bind = Cpanel::NameServer::Conf::BIND->new();
	my $fh_io = IO::Handle->new();

	 $ulock = Cpanel::SafeFile::safeopen($fh_io, "+<",$bind->{namedconffile});
	unless($ulock){
		&_debug2([$bind]);
		$logger->die("Could not open $bind->{namedconffile} for writing. Reason:" . $! );
		
		return;
	}

	my $zonelen = 10;##length($$domain_zone);

	### only done to external view
	#
	


	$bind->checkcache($fh_io);

	&_debug2(["nameconf:", $bind]);
	my $view = 'external';
	my $zone_len = length($new_zone);
	my($start,$end,$file);
	if(exists $bind->{config}->{z}->{$domain}->{v}->{$view}){
		$start = $bind->{config}->{z}->{$domain}->{v}->{$view}->[0];
		$end = $bind->{config}->{z}->{$domain}->{v}->{$view}->[1];
		seek($fh_io,$end,0);
		
			&_debug2(["Moved to: $end while start=$start"]);	

		

	}else{
	### add new one??
		$start = $bind->{config}->{v}->{$view}->{le};

		seek($fh_io,$start,0);
		
		&_debug2(["Moved to: $start while start=$start"]);	
	

	}
	{
		local $/;
	
	$file = readline($fh_io);
		truncate($fh_io,tell($fh_io));
	seek($fh_io,$start,0);
		print $fh_io $new_zone . $file;
		
		truncate($fh_io,tell($fh_io));

	}
		$bind->renumber_map( $start, $zone_len, 0 );
		$bind->{config}->{z}->{$domain}->{v}->{$view}->[0] = $start;
	 	$bind->{config}->{z}->{$domain}->{v}->{$view}->[1] = $start+$zone_len;	

	$bind->writecache();


	Cpanel::SafeFile::safeclose($fh_io, $ulock );
}
sub _debug2{
	use Data::Dumper;


	open(my $fh,">/tmp/dnsmeapi.log") or die "Could open /tmp/dnsmeapi.error file for writing. Reason:" . $!;

	
		print $fh Data::Dumper->Dump([@_]);
		print "\n";
	close($fh);

		
}

