Index: branches/secondaryzones/DNSDB.pm
===================================================================
--- branches/secondaryzones/DNSDB.pm	(revision 932)
+++ branches/secondaryzones/DNSDB.pm	(revision 934)
@@ -3077,5 +3077,5 @@
 ## DNSDB::delZone()
 # Delete a forward or reverse zone.
-# Takes a database handle, zone ID, and forward/reverse flag.
+# Takes a database handle, zone ID, forward/reverse flag, and optional secondary flag.
 # for now, just delete the records, then the domain.
 # later we may want to archive it in some way instead (status code 2, for example?)
@@ -3083,6 +3083,8 @@
   my $self = shift;
   my $dbh = $self->{dbh};
+
   my $zoneid = shift;
   my $revrec = shift;
+  my $secondary = shift || 'n';
 
   # Allow transactions, and raise an exception on errors so we can catch it later.
@@ -3092,18 +3094,25 @@
 
   return ('FAIL', 'Need a zone identifier to look up') if !$zoneid;
+  return ('FAIL', 'Reverse zone flag missing') if !$revrec;
 
   my $msg = '';
   my $failmsg = '';
-  my $zone = ($revrec eq 'n' ? $self->domainName($zoneid) : $self->revName($zoneid));
-  my $zonestatus = $self->zoneStatus(zoneid => $zoneid, revrec => $revrec);
-
-  return ('FAIL', ($revrec eq 'n' ? 'Domain' : 'Reverse zone')." ID $zoneid doesn't exist") if !$zone;
+  my $zone = ($secondary eq 'n' ? ($revrec eq 'n' ? $self->domainName($zoneid) : $self->revName($zoneid)) : $self->secondaryName($zoneid));
+  my $zonestatus = $self->zoneStatus(zoneid => $zoneid, revrec => $revrec, secondary => $secondary);
+
+  return ('FAIL', ($secondary eq 'n' ? ($revrec eq 'n' ? 'Domain' : 'Reverse zone') : 'Secondary zone')." ID $zoneid doesn't exist")
+	if !$zone;
 
   # Set this up here since we may use if if $self->{log_failures} is enabled
   my %loghash;
-  $loghash{domain_id} = $zoneid if $revrec eq 'n';
-  $loghash{rdns_id} = $zoneid if $revrec eq 'y';
+  if ($secondary eq 'n') {
+    $loghash{domain_id} = $zoneid if $revrec eq 'n';
+    $loghash{rdns_id} = $zoneid if $revrec eq 'y';
+  } else {
+    $loghash{secondary_id} = $zoneid;
+  }
+
   $loghash{group_id} = $self->parentID(
-	id => $zoneid, type => ($revrec eq 'n' ? 'domain' : 'revzone'), revrec => $revrec);
+	id => $zoneid, type => ($secondary eq 'n' ? ($revrec eq 'n' ? 'domain' : 'revzone') : 'secondaryzone'), revrec => $revrec);
 
   # Wrap all the SQL in a transaction
@@ -3111,30 +3120,36 @@
     # Disentangle custom record types before removing the
     # ones that are only in the zone to be deleted
-    if ($revrec eq 'n') {
-      my $sth = $dbh->prepare("UPDATE records SET type=?,domain_id=0 WHERE domain_id=? AND type=?");
-      $failmsg = "Failure converting multizone types to single-zone";
-      $sth->execute($reverse_typemap{PTR}, $zoneid, 65280);
-      $sth->execute($reverse_typemap{PTR}, $zoneid, 65281);
-      $sth->execute(65282, $zoneid, 65283);
-      $sth->execute(65282, $zoneid, 65284);
-      $failmsg = "Failure removing domain records";
-      $dbh->do("DELETE FROM records WHERE domain_id=?", undef, ($zoneid));
-      $failmsg = "Failure removing domain";
-      $dbh->do("DELETE FROM domains WHERE domain_id=?", undef, ($zoneid));
+    if ($secondary eq 'n') {
+      if ($revrec eq 'n') {
+        my $sth = $dbh->prepare("UPDATE records SET type=?,domain_id=0 WHERE domain_id=? AND type=?");
+        $failmsg = "Failure converting multizone types to single-zone";
+        $sth->execute($reverse_typemap{PTR}, $zoneid, 65280);
+        $sth->execute($reverse_typemap{PTR}, $zoneid, 65281);
+        $sth->execute(65282, $zoneid, 65283);
+        $sth->execute(65282, $zoneid, 65284);
+        $failmsg = "Failure removing domain records";
+        $dbh->do("DELETE FROM records WHERE domain_id=?", undef, ($zoneid));
+        $failmsg = "Failure removing domain";
+        $dbh->do("DELETE FROM domains WHERE domain_id=?", undef, ($zoneid));
+      } else {
+        my $sth = $dbh->prepare("UPDATE records SET type=?,rdns_id=0 WHERE rdns_id=? AND type=?");
+        $failmsg = "Failure converting multizone types to single-zone";
+        $sth->execute($reverse_typemap{A}, $zoneid, 65280);
+        $sth->execute($reverse_typemap{AAAA}, $zoneid, 65281);
+# We don't have an "A template" or "AAAA template" type, although it might be useful for symmetry.
+#        $sth->execute(65286?, $zoneid, 65283);
+#        $sth->execute(65286?, $zoneid, 65284);
+        $failmsg = "Failure removing reverse records";
+        $dbh->do("DELETE FROM records WHERE rdns_id=?", undef, ($zoneid));
+        $failmsg = "Failure removing reverse zone";
+        $dbh->do("DELETE FROM revzones WHERE rdns_id=?", undef, ($zoneid));
+      }
     } else {
-      my $sth = $dbh->prepare("UPDATE records SET type=?,rdns_id=0 WHERE rdns_id=? AND type=?");
-      $failmsg = "Failure converting multizone types to single-zone";
-      $sth->execute($reverse_typemap{A}, $zoneid, 65280);
-      $sth->execute($reverse_typemap{AAAA}, $zoneid, 65281);
-# We don't have an "A template" or "AAAA template" type, although it might be useful for symmetry.
-#      $sth->execute(65286?, $zoneid, 65283);
-#      $sth->execute(65286?, $zoneid, 65284);
-      $failmsg = "Failure removing reverse records";
-      $dbh->do("DELETE FROM records WHERE rdns_id=?", undef, ($zoneid));
-      $failmsg = "Failure removing reverse zone";
-      $dbh->do("DELETE FROM revzones WHERE rdns_id=?", undef, ($zoneid));
-    }
-
-    $msg = "Deleted ".($zonestatus ? '' : 'inactive ').($revrec eq 'n' ? 'domain' : 'reverse zone')." $zone";
+      $failmsg = "Failure removing secondary zone";
+      $dbh->do("DELETE FROM secondary_zones WHERE secondary_id=?", undef, $zoneid);
+    }
+
+    $msg = "Deleted ".($zonestatus ? '' : 'inactive ').
+	($secondary eq 'n' ? ($revrec eq 'n' ? 'domain' : 'reverse zone') : 'secondary zone')." $zone";
     $loghash{entry} = $msg;
     $self->_log(%loghash);
