Index: trunk/ippTools/src/pztool.c
===================================================================
--- trunk/ippTools/src/pztool.c	(revision 14023)
+++ trunk/ippTools/src/pztool.c	(revision 14600)
@@ -35,4 +35,6 @@
 static bool copydoneMode(pxConfig *config);
 
+static bool copydoneCompleteExp(pxConfig *config);
+
 # define MODECASE(caseName, func) \
     case caseName: \
@@ -196,6 +198,5 @@
     psString query = psStringCopy(
             "SELECT"
-            "   summitImfile.*,"
-            "   pzPendingImfile.exp_id"
+            "   summitImfile.*"
             " FROM pzPendingImfile"
             " JOIN summitImfile"
@@ -324,42 +325,4 @@
     }
   
-    // insert new imfile into newImfile
-    {
-        char *query =
-            "INSERT INTO newImfile"
-            "   SElECT"
-            "       pzPendingImfile.exp_id,"
-            "       pzPendingImfile.class,"
-            "       pzPendingImfile.class_id,"
-            "       '%s'," // uri of downloaded file
-            "       0"     // error flags
-            "   FROM pzPendingImfile"
-            "   WHERE"
-            "       pzPendingImfile.exp_name = '%s'"
-            "       AND pzPendingImfile.camera = '%s'"
-            "       AND pzPendingImfile.telescope = '%s'"
-            "       AND pzPendingImfile.class = '%s'"
-            "       AND pzPendingImfile.class_id = '%s'";
-
-        if (!p_psDBRunQuery(config->dbh, query, uri, exp_name, camera, telescope, class, class_id)) {
-            // rollback
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            return false;
-        } 
-
-        // sanity check: we should have inserted only one row
-        psU64 affected = psDBAffectedRows(config->dbh);
-        if (psDBAffectedRows(config->dbh) != 1) {
-            // rollback
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
-            return false;
-        }
-    }
 
     // cp the imfile into pzDoneImfile
@@ -441,4 +404,127 @@
     }
 
+    if (!copydoneCompleteExp(config)) {
+        psError(PS_ERR_UNKNOWN, false, "copydoneCompleteExp() failed");
+        return false;
+    }
+
     return true;
 }
+
+static bool copydoneCompleteExp(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // find all exposures that have had all of their imfiles downloaded but do
+    // not appear in newExp
+    psString query = pxDataGet("pztool_find_completed_exp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("pztool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    // start a transaction so all newExp's can start off with a state of run
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(output);
+        return false;
+    }
+
+
+   for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *row = output->data[i];
+
+        pzDoneExpRow *doneExp = pzDoneExpObjectFromMetadata(row);
+
+        if (!newExpInsert(config->dbh,
+                    0x0,                // exp_id
+                    doneExp->exp_name,  // tmp_exp_name
+                    doneExp->camera,    // tmp_camera
+                    doneExp->telescope, // tmp_telescope
+                    "run",              // state
+                    NULL,               // workdir
+                    "dirty",            // workdir state
+                    NULL                // reduction class
+                )
+        ) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(doneExp);
+            psFree(output);
+            return false;
+        }
+
+        psS64 exp_id = psDBLastInsertID(config->dbh);
+
+        // insert new pzDoneImfile into newImfile
+        {
+            char *query =
+                "INSERT INTO newImfile"
+                "   SElECT"
+                "       %" PRId64 "," // exp_id
+                "       pzDoneImfile.class_id," // tmp_class_id
+                "       pzDoneImfile.uri" // uri
+                "   FROM pzDoneImfile"
+                "   WHERE"
+                "       pzDoneImfile.exp_name = '%s'"
+                "       AND pzDoneImfile.camera = '%s'"
+                "       AND pzDoneImfile.telescope = '%s'";
+
+            if (!p_psDBRunQuery(config->dbh, query, exp_id, doneExp->exp_name, doneExp->camera, doneExp->telescope)) {
+                // rollback
+                if (!psDBRollback(config->dbh)) {
+                    psError(PS_ERR_UNKNOWN, false, "database error");
+                }
+                psError(PS_ERR_UNKNOWN, false, "database error");
+                psFree(doneExp);
+                psFree(output);
+                return false;
+            } 
+
+            // sanity check: we should have inserted only one row
+            psU64 affected = psDBAffectedRows(config->dbh);
+            if (psDBAffectedRows(config->dbh) != 1) {
+                // rollback
+                if (!psDBRollback(config->dbh)) {
+                    psError(PS_ERR_UNKNOWN, false, "database error");
+                }
+                psError(PS_ERR_UNKNOWN, false, "should have affected 1 row but %" PRIu64 " rows were modified", affected);
+                psFree(doneExp);
+                psFree(output);
+                return false;
+            }
+        }
+
+        psFree(doneExp);
+    }
+
+    psFree(output);
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    return true;
+}
