Index: lib/style/skins/obfs/obfs.cfg
===================================================================
--- lib/style/skins/obfs/obfs.cfg	(revision 1954)
+++ lib/style/skins/obfs/obfs.cfg	(revision 1955)
@@ -22,6 +22,7 @@
 sender = OBFS Data Registry <obfsadmin@lternet.edu>
 recipient = tmaddux@lternet.edu
 adminname = Troy Maddux
+debug = 0
 #
 # These are the sites and their coordinates. Coordinates are in
 # degrees:minutes:seconds:direction format
Index: src/perl/register-dataset.cgi
===================================================================
--- src/perl/register-dataset.cgi	(revision 1954)
+++ src/perl/register-dataset.cgi	(revision 1955)
@@ -82,6 +82,7 @@
 $config->define("guideTemplate", { DEFAULT => 'crap.tmpl'} );
 $config->define("confirmDataTemplate", { DEFAULT => 'crap.tmpl'} );
 $config->define("deleteDataTemplate", { DEFAULT => 'crap.tmpl'} );
+$config->define("debug", { DEFAULT => '0'} );
 $config->define("lat", { ARGCOUNT => ARGCOUNT_HASH} );
 $config->define("lon", { ARGCOUNT => ARGCOUNT_HASH} );
 
@@ -116,6 +117,7 @@
 my $sender = $config->sender();
 my $recipient = $config->recipient();
 my $adminname = $config->adminname();
+my $debug = $config->debug();
 my $lat = $config->get('lat');
 my $lon = $config->get('lon');
 
@@ -286,7 +288,9 @@
         push(@errorMessages, "Failed during login.\n");
     }
 
-    if($FORM::docid eq ""){
+debug( "Registry: A");
+    if ($FORM::docid eq "") {
+        debug( "Registry: B1");
 	    # document is being inserted 
 	    my $notunique = "NOT_UNIQUE";
 	    while ($notunique eq "NOT_UNIQUE") {
@@ -307,10 +311,19 @@
 	        #print TFILE $newdoc;
 	        #close(TFILE);
 	        #   }
+
+            # The id wasn't unique, so update our lastid file
+	        if ($notunique eq "NOT_UNIQUE") {
+                updateLastId($defaultScope);
+            }
 	    }
-	    if (! ($notunique eq "SUCCESS")) {
+        debug("Registry: B2");
+	    if ($notunique ne "SUCCESS") {
+            debug("Registry: NO SUCCESS");
+            debug("Message is: $notunique");
             push(@errorMessages, $notunique);
 	    }
+        debug("Registry: B3");
     } else {
 	    # document is being modified
 	    $docid = $FORM::docid;
@@ -330,19 +343,18 @@
 	    if (! $response) {
 	        push(@errorMessages, $metacat->getMessage());
 	        push(@errorMessages, "Failed while updating.\n");  
-	    } else {
-	        #deleteData(0);
 	    }
 
 	    if (scalar(@errorMessages)) {
+            debug("Registry: ErrorMessages defined in modify.");
 	        $$templateVars{'status'} = 'failure';
 	        $$templateVars{'errorMessages'} = \@errorMessages;
 	        $error = 1;
 	    }
 
-#if (! $error) {
-#sendNotification($docid, $mailhost, $sender, $recipient);
-#}
+        #if (! $error) {
+            #sendNotification($docid, $mailhost, $sender, $recipient);
+        #}
 	
         # Create our HTML response and send it back
 	    $$templateVars{'function'} = "modified";
@@ -353,8 +365,10 @@
     }
 }
 
+debug("Registry: C");
 
 if (scalar(@errorMessages)) {
+    debug("Registry: ErrorMessages defined.");
     $$templateVars{'status'} = 'failure';
     $$templateVars{'errorMessages'} = \@errorMessages;
     $error = 1;
@@ -374,6 +388,35 @@
 
 ################################################################################
 #
+# Subroutine for updating a metacat id for a given scope to the highest value
+#
+################################################################################
+sub updateLastId {
+  my $scope = shift;
+
+  my $errormsg = 0;
+  my $docid = $metacat->getLastId($scope);
+
+  if ($docid =~ /null/) {
+      # No docids with this scope present, so do nothing
+  } elsif ($docid) {
+      # Update the lastid file for this scope
+      (my $foundScope, my $id, my $rev) = split(/\./, $docid);
+      debug("Docid is: $docid\n");
+      debug("Lastid is: $id");
+      my $scopeFile = $cfgdir . "/" . $FORM::cfg . "/" . $scope . ".lastid";
+      open(LASTID, ">$scopeFile") or 
+          die "Failed to open lastid file for writing!";
+      print LASTID $id, "\n";
+      close(LASTID);
+  } else {
+    $errormsg = $metacat->getMessage();
+    debug("Error in getLastId: $errormsg");
+  }
+}
+
+################################################################################
+#
 # Subroutine for inserting a document to metacat
 #
 ################################################################################
@@ -2426,3 +2469,15 @@
 #}
 
 
+################################################################################
+# 
+# print debugging messages to stderr
+#
+################################################################################
+sub debug {
+    my $msg = shift;
+    
+    if ($debug) {
+        print STDERR "$msg\n";
+    }
+}
Index: src/perl/Metacat/Metacat.pm
===================================================================
--- src/perl/Metacat/Metacat.pm	(revision 1954)
+++ src/perl/Metacat/Metacat.pm	(revision 1955)
@@ -96,6 +96,7 @@
   my $self = shift;
   my %postData = ( @_ );
 
+  $self->{'message'} = '';
   my $userAgent = new LWP::UserAgent;
   $userAgent->agent("MetacatClient/1.0");
   
