#!/usr/bin/perl

use strict;

# this is the main loop. Get the questions from QUESTIONS
# and prepare them to feed them to dig. This could later
# even be folded into this script with Net::DNS.

# answer are stored in $number.$$.current
# then compared with stuff in: $number.valid
# any diffs from that are stored in $number.$$.diff
# if there is no diff than nothing happens

my $question;
my $number;
my $pid=$$;
my $dig="/usr/bin/dig -p 5353 \@localhost ";
my $diff="/usr/bin/diff -u";
my $q;
my @answer;
my @diff;

while (<>) {
	chomp;
	if ( /^#/ ) { next;} # comments
	($number,$question) = split /:/;

	$q=$dig . $question;
	print "Q $number: $q\n";
	@answer = `$q | ./stripdig.pl`;
	if ( $? == -1 ) { 
		print "Failed to query\n";
		exit 1;
	}

	# write the answer to a tmp file
	open ANSWER, ">$number.$pid.current";
		print ANSWER @answer;
	close ANSWER;

	if ( ! -f "$number.valid" ) {
		print "\t- no comparsion answer found, SKIP\n";
		next;
	}

	# diff the SOB
	@diff = `$diff "$number.$pid.current" "$number.valid"`;

	if ( @diff == () ) {
		# empty no diff
		print "\t- no diff, OK\n";
		# remove the .current file
		unlink "$number.$pid.current";
	} else {
		print "\t- diff, FAILURE (see $number.$pid.diff)\n";
		open DIFF, ">$number.$pid.diff";
			print DIFF @diff;
		close DIFF;
	}	
}
