Index: /trunk/ippTools/src/pzgetexp.c
===================================================================
--- /trunk/ippTools/src/pzgetexp.c	(revision 6875)
+++ /trunk/ippTools/src/pzgetexp.c	(revision 6876)
@@ -1,2 +1,6 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdlib.h>
 #include <stdio.h>
@@ -5,35 +9,75 @@
 #include "pzgetexp.h"
 
-static bool parseInput(pxConfig *config);
+static psArray *parseFileSets(pxConfig *config, const char *str);
 
 int main(int argc, char **argv)
 {
-    pxConfig *config = pzgetexpConfig(config, argc, argv);
+    pxConfig *config = pzgetexpConfig(NULL, argc, argv);
+
+    // find last fileset/exp_id (if we have one)
+    bool haveLastFileSet = false;
+    psString lastFileSet = NULL;
+    psArray *summitExps = summitExpSelectRowObjects(config->dbh, NULL, 0); 
+    if (summitExps) {
+        haveLastFileSet = true;
+        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);
+    }
+
+    psFree(lastFileSet);
 
     /*
-    switch (config->mode) {
-        case PX_MODE_SEEN:
-            if (!seenMode(config)) {
-                goto FAIL;
-            }
-            break;
-        default:
-            psAbort(argv[0], "invalid option (this should not happen)");
+    psArray *summitExps = parseInput(config);
+    if (!summitExps) {
+        goto FAIL;
+    }
+
+    // insert new entries into the database
+    for (long i = 0; i < psArrayLength(summitExps); i++) {
+        if (!summitExpInsertObject(config->dbh, summitExps->data[i])) {
+            psError(PS_ERR_UNKNOWN, false, "dbh access failed");
+            goto FAIL;
+        }
     }
     */
 
-    parseInput(config);
+    psFree(config);
 
     exit(EXIT_SUCCESS);
 
 FAIL:
-//    psFree(config);
+    psFree(config);
     exit(EXIT_FAILURE);
 }
 
-static bool parseInput(pxConfig *config) 
+static psArray *parseFileSets(pxConfig *config, const char *str)
 {
-    psString input = slurp(stdin);
-    psList *lines = psStringSplit(input, "\n");
+    psList *lines = psStringSplit(str, "\n");
 
     psListIterator *lineCursor = psListIteratorAlloc(lines, 0, false);
@@ -47,4 +91,11 @@
         psList *tokens = psStringSplit(item, " ");
 
+        // check to see if this line is a comment (or if the first token is
+        // NULL)
+        if (!psListGet(tokens, 0) || *((char *)psListGet(tokens, 0)) == '#') {
+            psFree(tokens);
+            continue;
+        }
+
         // check that we have the right number of tokens
         // print "# uri fileset datetime type\n";
@@ -55,5 +106,4 @@
 
         psListIterator *tokenCursor = psListIteratorAlloc(tokens, 0, false);
-
         char *uri       = psListGetAndIncrement(tokenCursor);
         char *exp_id    = psListGetAndIncrement(tokenCursor); // fileset
@@ -61,13 +111,21 @@
         char *exp_type  = psListGetAndIncrement(tokenCursor); // type
 
-        psFree(tokenCursor);
+        bool status = false;
+        char *camera_name = psMetadataLookupStr(&status, config->args,
+            "-inst");
+        char *telescope   = psMetadataLookupStr(&status, config->args,
+            "-telescope");
 
         summitExpRow *row = summitExpRowAlloc(
             exp_id,
-            config->camera,
-            config->telescope,
+            camera_name,
+            telescope,
             exp_type,
             uri
         );
+
+        psFree(tokenCursor);
+        psFree(tokens);
+
         psArrayAdd(summitExps, 0, row);
     }
@@ -75,13 +133,5 @@
     psFree(lineCursor);
     psFree(lines);
-    psFree(input);
 
-    for (long i = 0; i < psArrayLength(summitExps); i++) {
-        if (!summitExpInsertObject(config->dbh, summitExps->data[i])) {
-            psError(PS_ERR_UNKNOWN, false, "dbh access failed");
-            return false;
-        }
-    }
-
-    psFree(summitExps);
+    return summitExps;
 }
Index: /trunk/ippTools/src/pzgetexpConfig.c
===================================================================
--- /trunk/ippTools/src/pzgetexpConfig.c	(revision 6875)
+++ /trunk/ippTools/src/pzgetexpConfig.c	(revision 6876)
@@ -21,22 +21,30 @@
         "telescope name", "");
 
-    bool argErr = false;
-    if (! psArgumentParse(args, &argc, argv) || argc != 2) {
-        argErr = true;
-        fprintf (stderr, "error parsing arguments\n");
-    }
-
-    if (argErr) {
+    bool status = false;
+    if (!psArgumentParse(args, &argc, argv)
+        || argc != 1
+        || strcmp(psMetadataLookupStr(&status, args, "-uri"), "") == 0
+        || strcmp(psMetadataLookupStr(&status, args, "-inst"), "") == 0
+        || strcmp(psMetadataLookupStr(&status, args, "-telescope"), "") == 0
+    ) {
+        fprintf(stderr, "error parsing arguments\n");
         printf("\nPan-STARRS Phase Z Search Tool\n");
         printf("Usage: %s -uri -inst -telescope\n\n", argv[0]);
         psArgumentHelp(args);
-        psFree(args);
+        psFree(config);
         exit(EXIT_FAILURE);
     }
 
     config->args = args;
+    // don't free args here as it's silly to increment the ref count then
+    // "free" it
 
     // define Database handle, if used
     config->dbh = pmConfigDB(config->site);
+    if(!config->dbh) {
+        psError(PS_ERR_UNKNOWN, false, "Can't connect to db\n");
+        psFree(config);
+        exit(EXIT_FAILURE);
+    }
 
     return config;
