#!/usr/bin/perl

use Net::FTP;
use Mail::Mailer;

$from_address = "root\@bnsi.net";
$to_address = "alerts\@bnsi.net";
$hostname = "ftp.esinet.net";
$username = "hosting";
$password = "password";
$filename = "/secondary.conf";
$local = "/tmp/secondary.conf";
$named = "/etc/named.conf";
$ftp = Net::FTP->new($hostname);
$ftp->login($username, $password);
$ftp->get($filename, $local) || die "Nothing to do!\n\n";;
$ftp->delete($filename) || die "Can't delete! Fix perms!\n\n";
$ftp->quit;

open(OUT,"$local");
@array = <OUT>;
open(NAMED,"$named");
@conf = <NAMED>;
$string = join(":", @conf);
my $i = 0;
while ($array[$i]) {
	if ($array[$i] =~ /zone/) {
			if ($string !~ /$array[$i]/) { 
			open(NAMED,">>$named");
			print NAMED "\n$array[$i]";
			$list[$i] = $array[$i];
			$i++;
				while($array[$i] !~ /EOF/) {
					print NAMED $array[$i];
					$list[$i] = $array[$i];
					$i++;
				}
			}
	}
	close NAMED;
	$i++;
}

`rm $local`;

$list2 = join("",@list);
if ($list2) {
$subject = "New changes to secondary DNS!";
$body = "Hi,\n\n\tThe named.conf for the secondary DNS has been updated with the following new domain entries:\n\n$list2\n\nRemember to look over the changes and then run \"ndc reload\"";

$mailer = Mail::Mailer->new("sendmail");
$mailer->open({ From    => $from_address,
                To      => $to_address,
                Subject => $subject,
              })
    or die "Can't open: $!\n";
print $mailer $body;
$mailer->close();

}
