Index: trunk/tiny-import.pl
===================================================================
--- trunk/tiny-import.pl	(revision 363)
+++ trunk/tiny-import.pl	(revision 372)
@@ -68,6 +68,6 @@
   open FLAT, "<$flatfile";
 
-  our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl) ".
-	" VALUES (?,?,?,?,?,?,?,?,?)");
+  our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location) ".
+	" VALUES (?,?,?,?,?,?,?,?,?,?)");
 
   my %deleg;
@@ -77,4 +77,5 @@
     next if /^\s*$/;
     chomp;
+    s/\s*$//;
     recslurp($_);
   }
@@ -187,5 +188,5 @@
       my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
       if ($fparent && $rparent) {
-	$recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl);
+	$recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl, $loc);
       } else {
 	push @deferred, $rec unless $nodefer;
@@ -207,5 +208,5 @@
 	($code,$msg) = DNSDB::_zone2cidr($host);
 	my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
-	$recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl);
+	$recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl, $loc);
 
 ##fixme:  automagically convert manually maintained sub-/24 delegations
@@ -218,5 +219,5 @@
 	my $fparent = DNSDB::_hostparent($dbh, $host);
 	if ($fparent) {
-	  $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl);
+	  $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl, $loc);
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -245,5 +246,5 @@
 #	if !$rparent;
 	if ($rparent) {
-	  $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl);
+	  $recsth->execute(0, $rparent, $ns, 2, $msg, 0, 0, 0, $ttl, $loc);
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -252,6 +253,6 @@
 	my $fparent = DNSDB::_hostparent($dbh, $zone);
 	if ($fparent) {
-	  $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl);
-	  $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl) if $ip;
+	  $recsth->execute($fparent, 0, $zone, 2, $ns, 0, 0, 0, $ttl, $loc);
+	  $recsth->execute($fparent, 0, $ns, 2, $ip, 0, 0, 0, $ttl, $loc) if $ip;
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -283,5 +284,5 @@
       }
       if ($rparent) {
-	$recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl);
+	$recsth->execute(0, $rparent, $host, 12, $msg->addr, 0, 0, 0, $ttl, $loc);
       } else {
 	push @deferred, $rec unless $nodefer;
@@ -302,5 +303,5 @@
       my $domid = DNSDB::_hostparent($dbh, $host);
       if ($domid) {
-	$recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl);
+	$recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc);
       } else {
 	push @deferred, $rec unless $nodefer;
@@ -323,9 +324,9 @@
 	$dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
 	my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
-        $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
+        $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
       } else {
 	$dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
 	my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
-        $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
+        $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl, $loc);
       }
 
@@ -350,6 +351,6 @@
       my $domid = DNSDB::_hostparent($dbh, $zone);
       if ($domid) {
-        $recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl);
-	$recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl) if $ip;
+	$recsth->execute($domid, 0, $zone, 15, $host, $dist, 0, 0, $ttl, $loc);
+	$recsth->execute($domid, 0, $host, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
       } else {
 	push @deferred, $rec unless $nodefer;
@@ -371,9 +372,9 @@
 	($code,$msg) = DNSDB::_zone2cidr($fqdn);
 	my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
-	$recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl);
+	$recsth->execute(0, $rparent, $rdata, 16, "$msg", 0, 0, 0, $ttl, $loc);
       } else {
 	my $domid = DNSDB::_hostparent($dbh, $fqdn);
 	if ($domid) {
-	  $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl);
+	  $recsth->execute($domid, 0, $fqdn, 16, $rdata, 0, 0, 0, $ttl, $loc);
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -402,7 +403,7 @@
 	  ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
 # this would probably make a lot more sense to do hostmaster.$config{admindomain}
-          $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
-	}
-	$recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
+          $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
+	}
+	$recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl, $loc);
 ##fixme:  (?)  implement full conversion of tinydns . records?
 # -> problem:  A record for NS must be added to the appropriate *forward* zone, not the reverse
@@ -420,8 +421,8 @@
 	  $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
 	  ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
-          $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
-	}
-	$recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
-	$recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
+          $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560", $loc);
+	}
+	$recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl, $loc);
+	$recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl, $loc) if $ip;
       }
 
@@ -429,4 +430,20 @@
     } elsif ($rec =~ /^\%/) {
       $cnt{VIEWS}++;
+
+      # unfortunate that we don't have a guaranteed way to get a description on these.  :/
+      my ($loc,$cnet) = split /:/, $rec, 2;
+      $loc =~ s/^\%//;
+      if (my ($iplist) = $dbh->selectrow_array("SELECT iplist FROM locations WHERE location = ?", undef, ($loc))) {
+	if ($cnet) {
+	  $iplist .= ",$cnet";
+	  $dbh->do("UPDATE locations SET iplist = ? WHERE location = ?", undef, ($iplist, $loc));
+	} else {
+	  # hmm.  spit out a warning?  if we already have entries for $loc, adding a null
+	  # entry will almost certainly Do The Wrong Thing(TM)
+	}
+      } else {
+	$cnet = '' if !$cnet;	# de-nullify
+	$dbh->do("INSERT INTO locations (location,iplist,description) VALUES (?,?,?)", undef, ($loc, $cnet, $loc));
+      }
 
     } elsif ($rec =~ /^:/) {
@@ -471,5 +488,5 @@
 	my $domid = DNSDB::_hostparent($dbh, $fqdn);
 	if ($domid) {
-	  $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl) if $domid;
+	  $recsth->execute($domid, 0, $fqdn, 33, $target, $prio, $weight, $port, $ttl, $loc) if $domid;
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -488,5 +505,5 @@
 	my $fparent = DNSDB::_hostparent($dbh, $fqdn);
 	if ($fparent) {
-	  $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl);
+	  $recsth->execute($fparent, 0, $fqdn, 28, $val->addr, 0, 0, 0, $ttl, $loc);
 	} else {
 	  push @deferred, $rec unless $nodefer;
@@ -501,5 +518,5 @@
 	  my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
 	  if ($rparent) {
-	    $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl);
+	    $recsth->execute(0, $rparent, $txtstring, 16, "$msg", 0, 0, 0, $ttl, $loc);
 	  } else {
 	    push @deferred, $rec unless $nodefer;
@@ -508,5 +525,5 @@
 	  my $domid = DNSDB::_hostparent($dbh, $fqdn);
 	  if ($domid) {
-	    $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl);
+	    $recsth->execute($domid, 0, $fqdn, 16, $txtstring, 0, 0, 0, $ttl, $loc);
 	  } else {
 	    push @deferred, $rec unless $nodefer;
@@ -527,5 +544,5 @@
 	  my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
 	  if ($rparent) {
-	    $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl);
+	    $recsth->execute(0, $rparent, "$email $txtrec", 17, "$msg", 0, 0, 0, $ttl, $loc);
 	  } else {
 	    push @deferred, $rec unless $nodefer;
@@ -534,5 +551,5 @@
 	  my $domid = DNSDB::_hostparent($dbh, $fqdn);
 	  if ($domid) {
-	    $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl);
+	    $recsth->execute($domid, 0, $fqdn, 17, "$email $txtrec", 0, 0, 0, $ttl, $loc);
 	  } else {
 	    push @deferred, $rec unless $nodefer;
@@ -549,5 +566,5 @@
 	my $domid = DNSDB::_hostparent($dbh, $fqdn);
 	if ($domid) {
-	  $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl);
+	  $recsth->execute($domid, 0, $fqdn, 44, $sshfp, 0, 0, 0, $ttl, $loc);
 	} else {
 	  push @deferred, $rec unless $nodefer;
