#!/usr/bin/perl -w
##Jason Thomas
##Last Modified 20090819
 
use strict;
use lib '/home/jthomas/lib/perl5/site_perl/5.8.8/';
use File::Copy;
use IO::File;
use Text::Template;
use File::Grep qw( fgrep );
 
 
##my $repository = "/home/jthomas/projects/qsp_dns_migration/qsp.us.zone/";
my $repository = "/home/jthomas/projects/dns/master/";
my $dnsData = "data";
 
##currently designed to read from file. however if fqdn is not before fqhn, it will not be able write
##the entry until db.fqdn is created
 
 
open (DATA, $dnsData), or die $!;
my @dnsLine = <DATA>;
 
foreach my $entry (@dnsLine){
 
	chomp ($entry);
 
	my ($hostName, $ipAddress) = split(' ', $entry);
	my ($domainName, $subDomain) = findDomainName($hostName);
	checkZoneExist($domainName, $subDomain, $ipAddress);
}
 
close (DATA);
exit (0);
 
sub findDomainName{
 
	my ($hostName) = @_;
	my $domainName;
	my $subDomain;
	my $size;
	my $counter;
	my @tmp;
 
	@tmp = split('\.', $hostName);
	$size = @tmp - 2; 
 
		if ( @tmp == 2){
 
			return ($hostName, undef);
		}
 
		else{
 
			$domainName = "$tmp[@tmp-2].$tmp[@tmp-1]";
 
			for ($counter = 0; $counter < $size; $counter++){
 
				if ( ! defined($subDomain) ){
 
					$subDomain=$tmp[$counter];
 
				}
 
				else{
 
					$subDomain="$subDomain.$tmp[$counter]";
 
				}
			}
 
			return ($domainName, $subDomain);
		}
}
 
sub checkZoneExist{
 
	my ($domainName, $subDomain, $ipAddress) = @_;
	my $dbZoneFile = "db.$domainName";
	my $userName = "jthomas";
	my $soa = "2009081801    ";
 
		if ( -e $repository.$dbZoneFile ){
 
		##	write subdomain to file if file exists;	
		addToZone($dbZoneFile, $subDomain, $ipAddress);
 
		}
 
		elsif ( (! defined($subDomain)) ||  $subDomain eq ""){
 
			my ($tabs) = tabLength($domainName.".");
			makeZone("customArecord.tmpl", $dbZoneFile,  {soa => \$soa, userName => \$userName, domain => \"$domainName.", tabs => \$tabs, CUSTOMIP => $ipAddress });
 
		}
 
}
 
sub makeZone{
 
	my ($templateSource, $dbZoneFile, $fillInHash) = @_;
	my $template = Text::Template->new(SOURCE => $templateSource) or die "Couldn't construct template: $Text::Template::ERROR";
	my $result = $template->fill_in(HASH => $fillInHash);
 
	    if (! defined $result) {
        die "Couldn't fill in template: $Text::Template::ERROR";
    	}
 
	open(DBFILE, ">$repository/$dbZoneFile") , or die $! ;
	print DBFILE $result; 
	close (DBFILE);
 
	print "\[Info\] Zone file $dbZoneFile created.\n";
 
}
 
sub addToZone{
 
 
	my ($dbZoneFile,  $subDomain, $ipAddress) = @_ ; 
 
	if ( defined($subDomain) &&  $subDomain ne "www"){
 
		if ( fgrep {/^$subDomain\t/} $repository.$dbZoneFile ){
 
			print "Record $subDomain is already in $dbZoneFile \n";
		}
 
		else {
 
			my ($tabs) = tabLength($subDomain);
			open(DBFILE, ">>$repository/$dbZoneFile") , or die $!;
			print DBFILE "$subDomain$tabs"."IN\tA\t$ipAddress\n";
			close (DBFILE);
 
		}
	}	
}
 
sub tabLength{
 
	my ($word) = @_; 
	my $tabs = "\t" x (((32 - length($word) + 6) / 8));
	if (length($tabs) == 0){
		$tabs = "\t";
	}
	return $tabs;
}
 
 
###########################################################################
# Help, info and correctional information.
###########################################################################
sub subUsage {
 
    print "\nUsage: addZone.pl \[-Dn?\] <username> <hostname> <ipaddress>\n";
    print "       newZone.pl <username> <domain> <IP address>\n";
    print "addZone will create a new zone file, based on domain name. It will also add a new zone to the\n";
    print "list of zones governed by named-zones.conf. This file will be created and sent, if needed, by dnsPush.\n";
    print "Only use this to add a new zone and make sure you have the correct type (virtual, www or blank).\n\n";
    print "-D          Show debug information while running.\n";
    print "-n          Dryrun mode, make no changes, displays final zone file on screen.\n";
    print "-?          Help page, displays this page.\n\n";
    exit(0);
}