Index: /trunk/DNSDB/ExportBIND.pm
===================================================================
--- /trunk/DNSDB/ExportBIND.pm	(revision 879)
+++ /trunk/DNSDB/ExportBIND.pm	(revision 880)
@@ -58,8 +58,4 @@
   while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) {
     my $cidr = NetAddr::IP->new($revzone);
-    my $zfile = $cidr->network->addr."-".$cidr->masklen;
-#    my $cachefile = "$dnsdb->{exportcache}/$zfile";
-#    my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
-    my $tmpcache = "tmp.$zfile.$$";      # safety net.  don't overwrite a previous known-good file
 
 ##fixme:  convert logical revzone into .arpa name?  maybe take a slice of showrev_arpa?
@@ -85,4 +81,8 @@
 
       my $arpazone = DNSDB::_ZONE($cidr, 'ZONE', 'r', '.').($cidr->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
+      my $zfile = $cidr->network->addr."-".$cidr->masklen;
+#      my $cachefile = "$dnsdb->{exportcache}/$zfile";
+#      my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$";
+      my $tmpcache = "tmp.$zfile.$$";      # safety net.  don't overwrite a previous known-good file
 
 ##fixme:  need to open separate zone files for aggregated metazones eg /22 or /14
@@ -90,5 +90,5 @@
         my $zfilepath = $dnsdb->{bind_export_reverse_zone_path};
         $zfilepath =~ s/\%view/$loc/;
-        $zfilepath =~ s/\%zone/$revzone/;
+        $zfilepath =~ s/\%zone/$zfile/;
         $zfilepath =~ s/\%arpazone/$arpazone/;
 
@@ -119,5 +119,5 @@
           my (@zsoa) = $soasth->fetchrow_array();
 ##fixme: do we even need @loclist passed in?
-          publishrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
+          printrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,
             $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, '');
         } # if force_refresh etc
@@ -158,5 +158,5 @@
         }
 
-        publishrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
+        printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,
 		$host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
 
@@ -390,5 +390,5 @@
     open BINDCONF, ">", $tmpconf;
 
-    foreach my $view (@{$viewlist}, 'common', { location => 'common', iplist => '' }) {
+    foreach my $view (@{$viewlist}, { location => 'common', iplist => '' }) {
 #print Dumper($view);
       print BINDCONF "view $view->{location} {\n";
@@ -420,5 +420,5 @@
 
 # Print individual records in BIND format
-sub publishrec_bind {
+sub printrec_bind {
   my $dnsdb = shift;
 
@@ -433,5 +433,5 @@
   if ($loc eq '') {
     foreach my $subloc (@{$loclist}) {
-      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val,
+      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val,
         $distance, $weight, $port, $ttl, $subloc, $stamp, $expires, $stampactive);
     }
@@ -466,6 +466,17 @@
 #    print *{$zonefiles->{$loc}} "$zone	$ttl	IN	SOA	$primary	$email	( $serial $refresh $retry $expire $min_ttl )\n"
 #       or die "couldn't write $zone SOA: $!";
-    my $recdata = "$zone.	$ttl	IN	SOA	$primary	$email	( $serial $refresh $retry $expire $min_ttl )\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+
+    # Prepare the body of the record
+    my $recdata = "$ttl        IN      SOA     $primary        $email  ( $serial $refresh $retry $expire $min_ttl )\n";
+
+    # ... and prepend the zone name FQDN
+    if ($revrec eq 'y') {
+      my $zone2 = DNSDB::_ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
+      $recdata = "$zone2.      $recdata";
+    } else {
+      $recdata = "$zone.       $recdata";
+    }
+
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # SOA
 
@@ -475,5 +486,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      A       $val\n" or die $!;
     my $recdata = "$host.	$ttl	IN	A	$val\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # A
 
@@ -487,10 +498,11 @@
 
       my $val2 = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');
+      $host .= "." if $host !~ /\.$/;
       my $recdata = "$val2.	$ttl	IN	NS	$host\n";
-      recprint($zonefiles, $loclist, $loc, $recdata);
+      __recprint($zonefiles, $loclist, $loc, $recdata);
 
     } else {
       my $recdata = "$host.	$ttl	IN	NS	$val.\n";
-      recprint($zonefiles, $loclist, $loc, $recdata);
+      __recprint($zonefiles, $loclist, $loc, $recdata);
     }
   } # NS
@@ -500,5 +512,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      AAAA    $val\n" or die $!;
     my $recdata = "$host.	$ttl	IN	AAAA	$val\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # AAAA
 
@@ -508,5 +520,5 @@
 # should arguably swap host and val first, but MX records really don't make any sense in reverse zones, so any silliness that results from finding one doesn't much matter.
     my $recdata = "$host.	$ttl	IN	MX	$distance $val.\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # MX
 
@@ -515,5 +527,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      TXT     \"$val\"\n" or die $!;
     my $recdata = "$host.	$ttl	IN	TXT	\"$val\"\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # TXT
 
@@ -522,5 +534,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      CNAME   $val\n" or die $!;
     my $recdata = "$host.	$ttl	IN	CNAME	$val.\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # CNAME
 
@@ -529,5 +541,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      SRV     $distance   $weight $port   $val\n" or die $!;
     my $recdata = "$host	$ttl	IN	SRV	$distance	$weight $port	$val.\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # SRV
 
@@ -536,5 +548,5 @@
 #    print {$zonefiles->{$loc}} "$host  $ttl    IN      RP      $val\n" or die $!;
     my $recdata = "$host.	$ttl	IN	RP	$val\n";
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # RP
 
@@ -550,5 +562,5 @@
 #        print {$zonefiles->{$loc}} "$val	$ttl	IN	PTR	$host\n" or die $!;
         my $recdata = "$val.	$ttl	IN	PTR	$host.\n";
-        recprint($zonefiles, $loclist, $loc, $recdata);
+        __recprint($zonefiles, $loclist, $loc, $recdata);
       } else {
         $zone = NetAddr::IP->new($zone);
@@ -560,5 +572,5 @@
 #            or die $!;
           my $recdata = "$val.".DNSDB::_ZONE($zone, 'ZONE', 'r', '.').".in-addr.arpa.	$ttl	IN	PTR	$host.\n";
-          recprint($zonefiles, $loclist, $loc, $recdata);
+          __recprint($zonefiles, $loclist, $loc, $recdata);
         } else {
           # not going to care about strange results if $val is not an IP value and is resolveable in DNS
@@ -569,5 +581,5 @@
           my $recdata = DNSDB::_ZONE($val, 'ZONE', 'r', '.').($val->{isv6} ? '.ip6.arpa' : '.in-addr.arpa').
 		".	$ttl	IN	PTR	$host.\n";
-          recprint($zonefiles, $loclist, $loc, $recdata);
+          __recprint($zonefiles, $loclist, $loc, $recdata);
         }
       } # non-".arpa" $val
@@ -579,5 +591,5 @@
 #      print {$zonefiles->{$loc}} "$host	$ttl	IN	PTR	$val\n" or die $!;
       my $recdata = "$host.	$ttl	IN	PTR	$val.\n";
-      recprint($zonefiles, $loclist, $loc, $recdata);
+      __recprint($zonefiles, $loclist, $loc, $recdata);
     }
   } # PTR
@@ -589,15 +601,15 @@
 #    $$recflags{$val}++;
     if ($revrec eq 'y') {
-      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val,
+      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val,
        $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
 # ... but we need to tweak it for this case?  so the A record gets published...
 #$$recflags{$val} = 'a+ptr';
 #print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!;
-#          publishrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone,
+#          printrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone,
 #            $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
 #  my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,
 #        $loc, $stamp, $expires, $stampactive) = @_;
     } else {
-      publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val,
+      printrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val,
         $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive);
       # set a unique flag to skip template expansion for this IP in forward zones
@@ -617,5 +629,5 @@
       }
     } else {
-      __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
+      __publish_template_bind($dnsdb, $val, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     }
   } # PTR template
@@ -631,5 +643,5 @@
       }
     } else {
-      __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
+      __publish_template_bind($dnsdb, $val, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);
     }
   } # A+PTR template
@@ -641,5 +653,5 @@
   } # AAAA+PTR template
 
-} # publishrec_bind()
+} # printrec_bind()
 
 
@@ -729,5 +741,5 @@
     }
     # and finally 
-    recprint($zonefiles, $loclist, $loc, $recdata);
+    __recprint($zonefiles, $loclist, $loc, $recdata);
   } # foreach (@iplist)
 } # __publish_template_bind()
@@ -736,5 +748,5 @@
 # actual record printing sub
 # loop on the locations here so we don't end up with a huge pot of copypasta
-sub recprint {
+sub __recprint {
   my ($zonefiles, $loclist, $loc, $recdata) = @_;
   if ($loc eq '') {
