Index: /trunk/ippTools/src/pzgetexp.c
===================================================================
--- /trunk/ippTools/src/pzgetexp.c	(revision 6879)
+++ /trunk/ippTools/src/pzgetexp.c	(revision 6880)
@@ -18,4 +18,7 @@
     bool haveLastFileSet = false;
     psString lastFileSet = NULL;
+    // XXX this is a very ugly and brute force way of doing things need to add
+    // a metadatadb function to retrun the last entry without removing it from
+    // the database
     psArray *summitExps = summitExpSelectRowObjects(config->dbh, NULL, 0); 
     if (summitExps) {
@@ -23,48 +26,67 @@
         summitExpRow *lastExps = summitExps->data[psArrayLength(summitExps) - 1];
         lastFileSet = psMemIncrRefCounter(lastExps->exp_id);
-        psFree(summitExps);
     }
 
     // invoke dsproductls
     // dsproductls --uri <> --last_fileset <>  
-    {
-        bool status = false;
-        psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-        psString cmd = NULL;
-        if (haveLastFileSet) {
-            psStringAppend(&cmd, "dsproductls --uri %s --last_fileset %s",
-                uri, lastFileSet);
-        } else {
-            psStringAppend(&cmd, "dsproductls --uri %s", uri);
-        }
-
-        FILE *output = popen(cmd, "r");
-        psFree(cmd);
-
-        if (!output) {
-            psError(PS_ERR_UNKNOWN, true, "popen() failed");
-            goto FAIL;
-        }
-        psString cmdOutput = fslurp(output);
-        pclose(output);
-        parseFileSets(config, cmdOutput);
+    bool status = false;
+    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
+    psString cmd = NULL;
+    if (haveLastFileSet) {
+        psStringAppend(&cmd, "dsproductls --uri %s --last_fileset %s",
+            uri, lastFileSet);
+        psFree(lastFileSet);
+    } else {
+        psStringAppend(&cmd, "dsproductls --uri %s", uri);
     }
 
-    psFree(lastFileSet);
+    fprintf(stderr, "cmd is: %s\n", cmd);
 
-    /*
-    psArray *summitExps = parseInput(config);
-    if (!summitExps) {
+    FILE *output = popen(cmd, "r");
+    psFree(cmd);
+
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, true, "popen() failed");
+        goto FAIL;
+    }
+    psString cmdOutput = fslurp(output);
+    pclose(output);
+
+    psArray *newSummitExps = parseFileSets(config, cmdOutput);
+    psFree(cmdOutput);
+    if (!newSummitExps) {
+        // XXX not nessicarily an error
+        psError(PS_ERR_UNKNOWN, true, "no new fileSet/exp IDs");
         goto FAIL;
     }
 
+    // try not to insert duplicate summitExp entries
+    // XXX this will become very expensive as the number of summitExp entry
+    // grows -- is just blindly ignoring database errors the best thing to do?
+    if (summitExps) {
+        for (long i = 0; i < psArrayLength(newSummitExps); i++) {
+            summitExpRow *newSummitExp = newSummitExps->data[i];
+            for (long j = 0; j < psArrayLength(summitExps); j++) {
+                summitExpRow *summitExp = summitExps->data[j];
+                if (strcmp(newSummitExp->exp_id,
+                           summitExp->exp_id) == 0) {
+                    psArrayRemove(newSummitExps, newSummitExp);
+                    // dec the counter as the array just got shorter
+                    // and we don't want to skip elemnts
+                    i--;
+                    break;
+                }
+            }
+        }
+        psFree(summitExps);
+    }
+
     // insert new entries into the database
-    for (long i = 0; i < psArrayLength(summitExps); i++) {
-        if (!summitExpInsertObject(config->dbh, summitExps->data[i])) {
+    for (long i = 0; i < psArrayLength(newSummitExps); i++) {
+        if (!summitExpInsertObject(config->dbh, newSummitExps->data[i])) {
             psError(PS_ERR_UNKNOWN, false, "dbh access failed");
             goto FAIL;
         }
     }
-    */
 
     psFree(config);
@@ -74,4 +96,5 @@
 FAIL:
     psFree(config);
+
     exit(EXIT_FAILURE);
 }
@@ -79,4 +102,7 @@
 static psArray *parseFileSets(pxConfig *config, const char *str)
 {
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+    PS_ASSERT_PTR_NON_NULL(str, NULL);
+
     psList *lines = psStringSplit(str, "\n");
 
