Index: trunk/ippTools/src/pzgetimfiles.c
===================================================================
--- trunk/ippTools/src/pzgetimfiles.c	(revision 10014)
+++ trunk/ippTools/src/pzgetimfiles.c	(revision 10021)
@@ -36,4 +36,6 @@
 int main(int argc, char **argv)
 {
+    psLibInit(NULL);
+
     pxConfig *config = pzgetimfilesConfig(NULL, argc, argv);
 
@@ -43,13 +45,18 @@
 
     psFree(config);
+    pmConfigDone();
+    psLibFinalize();
+
     exit(EXIT_SUCCESS);
 
 FAIL:
     psFree(config);
+    pmConfigDone();
+    psLibFinalize();
 
     exit(EXIT_FAILURE);
 }
 
-static bool go (pxConfig *config)
+static bool go(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, NULL);
@@ -74,4 +81,24 @@
         psError(PS_ERR_UNKNOWN, true, "-filesetid is required");
         return false;
+    }
+
+    psString camera = psMetadataLookupStr(&status, config->args, "-inst");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst");
+        return NULL;
+    }
+    if (!camera) {
+        psError(PS_ERR_UNKNOWN, true, "-inst is required");
+        return NULL;
+    }
+
+    psString telescope = psMetadataLookupStr(&status, config->args, "-telescope");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope");
+        return NULL;
+    }
+    if (!telescope) {
+        psError(PS_ERR_UNKNOWN, true, "-telescope is required");
+        return NULL;
     }
 
@@ -80,6 +107,5 @@
     psStringAppend(&cmd, "%s --uri %s", FILESET_LS_CMD, uri);
 
-    // XXX debugging
-    fprintf(stderr, "cmd is: %s\n", cmd);
+    psTrace("pzgetimfiles", PS_LOG_INFO, "cmd is: %s\n", cmd);
 
     FILE *output = popen(cmd, "r");
@@ -108,7 +134,25 @@
         return false;
     }
-
     psFree(cmdOutput);
- 
+    
+    // save the number of new Imfiles;
+    long imfiles = psArrayLength(newImfiles);
+
+    // if the fileset was empty (no files) then we can bail out early
+    if (imfiles == 0) {
+        char *query = 
+            "UPDATE summitExp"
+            " SET imfiles = %d"
+            " WHERE exp_id = '%s'"
+            " AND camera = '%s'"
+            " AND telescope = '%s'";
+        if (!p_psDBRunQuery(config->dbh, query, imfiles, filesetid, camera, telescope)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+        psFree(newImfiles);
+        return true;
+    } 
+
     // start a transaction so it's all rows or nothing
     if (!psDBTransaction(config->dbh)) {
@@ -125,4 +169,8 @@
 
         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");
             psFree(newImfiles);
@@ -136,4 +184,8 @@
         long inserted = p_psDBRunQueryPrepared(config->dbh, newImfiles, query);
         if (inserted < 0) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
             psError(PS_ERR_UNKNOWN, false, "database error");
             psFree(newImfiles);
@@ -142,4 +194,8 @@
         // sanity check that we actually inserted something
         if (inserted == 0) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
             psError(PS_ERR_UNKNOWN, false, "database error -- we should have inserted at least one row");
             psFree(newImfiles);
@@ -148,7 +204,6 @@
     }
 
-    // save the number of new Imfiles;
-    long imfiles = psArrayLength(newImfiles);
     psFree(newImfiles);
+
     {
         char *query = 
@@ -174,4 +229,8 @@
 
         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;
@@ -203,4 +262,8 @@
 
         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;
@@ -210,6 +273,15 @@
     // update summitExp.imfiles
     {
-        char *query = "UPDATE summitExp SET imfiles = %d";
-        if (!p_psDBRunQuery(config->dbh, query, imfiles)) {
+        char *query = 
+            "UPDATE summitExp"
+            " SET imfiles = (SELECT COUNT(*) FROM summitImfile"
+            "   WHERE exp_id = '%s'"
+            "   AND camera = '%s'"
+            "   AND telescope = '%s'"
+            ")" 
+            " WHERE exp_id = '%s'"
+            " AND camera = '%s'"
+            " AND telescope = '%s'";
+        if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope, filesetid, camera, telescope)) {
             psError(PS_ERR_UNKNOWN, false, "database error");
             return false;
@@ -271,4 +343,6 @@
     psString line;
     while ((line = psListGetAndIncrement(lineCursor))) {
+        psTrace("pzgetimfiles", PS_LOG_INFO, "parsing line: %s\n", line);
+
         // split line into tokens
         psList *tokens = psStringSplit(line, " ", false);
@@ -355,9 +429,18 @@
         }
 
+        // must be freed after the new metadata is built -- holds the strings
         psFree(tokenCursor);
         psFree(tokens);
 
+        // debugging
+        if (psTraceGetLevel("pzgetimfiles") == PS_LOG_INFO) {
+            psString doc = psMetadataConfigFormat(md);
+            psTrace("pzgetimfiles", PS_LOG_INFO, "parsed line as:\n %s\n", doc);
+            psFree(doc);
+        }
+
         psArrayAdd(pzPendingImfiles, 0, md);
-        psMetadataConfigPrint(stdout, md);
+
+        psFree(md);
     }
 
