#!/usr/bin/perl -w
# sample PowerDNS Coprocess backend

use strict;

$|=1;					# no buffering

my $domain="your.host.name";
my $ipfile="/home/ddnsuser/myip";
my $ip01="";

my $line=<>;
chomp($line);

unless($line eq "HELO\t1") {
	print "FAIL\n";
	print STDERR "Recevied '$line'\n";
	<>;
	exit;
}
print "OK	Sample backend firing up\n";	# print our banner

while(<>)
{
	print STDERR "$$ Received: $_";
	chomp();
	my @arr=split(/\t/);
	if(@arr<6) {
		print "LOG	PowerDNS sent unparseable line\n";
		print "FAIL\n";
		next;
	}

	my ($type,$qname,$qclass,$qtype,$id,$ip)=split(/\t/);

	if(($qtype eq "A" || $qtype eq "ANY") && $qname eq "$domain") {
		$ip01=`cat $ipfile`;
		chomp($ip01);
		print STDERR "$$ Sent A records\n";
		print "DATA	$qname	$qclass	A	3600	-1	$ip01\n";
	}

	print STDERR "$$ End of data\n";
	print "END\n";

}

