Index: trunk/ippTools/src/pzgetimfiles.c
===================================================================
--- trunk/ippTools/src/pzgetimfiles.c	(revision 16170)
+++ trunk/ippTools/src/pzgetimfiles.c	(revision 16588)
@@ -226,35 +226,9 @@
     psFree(newImfiles);
 
-    // queue the imfiles (files) dest. for pzPendingImfile in pending -- must
-    // be done before the imfile is insterted into summitImfile (after that
-    // happens when don't know which imfiles are new anymore (well thats not
-    // exactly true but it may be someday if we have to beable to resync to
-    // modified datastore entries)
-    {
-        char *query = 
-            "INSERT IGNORE INTO pzPendingImfile" 
-            "   SElECT"
-            "       incoming.exp_name,"
-            "       incoming.camera,"
-            "       incoming.telescope,"
-            "       incoming.class,"
-            "       incoming.class_id"
-            "   FROM incoming";
-
-        if (!p_psDBRunQuery(config->dbh, query)) {
-            // rollback
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            return false;
-        }
-    }
-
     // copy imfiles (files) from the temp table into summitImfiles
     {
         char *query = 
             "INSERT IGNORE INTO summitImfile" 
-            "   SElECT"
+            "   SELECT"
             "       incoming.exp_name,"
             "       incoming.camera,"
@@ -313,61 +287,32 @@
     }
 
-    // cp the pzPendingExp entry to pzDoneExp
+    // add new exps to pzDownloadExp -- must be done before the new exps are
+    // added to summitExp because of the SQL logic
     {
         char *query = 
-            "INSERT INTO pzDoneExp"
+            "INSERT INTO pzDownloadExp" 
             "   SELECT"
-            "       pzPendingExp.*"
-            "   FROM pzPendingExp"
+            "       incoming.exp_name,"
+            "       incoming.camera,"
+            "       incoming.telescope,"
+            "       \"run\""
+            "   FROM incoming"
+            "   LEFT JOIN pzDownloadExp"
+            "       USING(exp_name, camera, telescope)"
             "   WHERE"
-            "       pzPendingExp.exp_name = '%s'"
-            "       AND pzPendingExp.camera = '%s'"
-            "       AND pzPendingExp.telescope = '%s'";
-        if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope)) {
-            // 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;
-        }
-    }
- 
-    // remove the pzPendingExp entry for this exp (fileset)
-    {
-        char *query = 
-            "DELETE FROM pzPendingExp"
-            " WHERE"
-            "   exp_name = '%s'"
-            "   AND camera = '%s'"
-            "   AND telescope = '%s'";
-        if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope)) {
-            // 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 removed 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);
+            "       pzDownloadExp.exp_name is NULL"
+            "       AND pzDownloadExp.camera is NULL"
+            "       AND pzDownloadExp.telescope is NULL"
+            "   GROUP BY"
+            "       incoming.exp_name,"
+            "       incoming.camera,"
+            "       incoming.telescope";
+
+        if (!p_psDBRunQuery(config->dbh, query)) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
             return false;
         }
