Index: trunk/ippTools/src/pztool.c
===================================================================
--- trunk/ippTools/src/pztool.c	(revision 15108)
+++ trunk/ippTools/src/pztool.c	(revision 15149)
@@ -37,4 +37,6 @@
 
 static bool copydoneCompleteExp(pxConfig *config);
+static psArray *pzGetKnownCameras(pxConfig *config);
+static psArray *pzArrayAddArray(psArray *array, psArray *input);
 
 # define MODECASE(caseName, func) \
@@ -218,20 +220,12 @@
     }
 
-    // get a list of cameras we've seen exps for
-    if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM summitExp")) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    psArray *cameras = p_psDBFetchResult(config->dbh);
+    psArray *cameras = pzGetKnownCameras(config);
     if (!cameras) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(cameras)) {
-        psTrace("pztool", PS_LOG_INFO, "no rows found");
-        psFree(cameras);
-        return true;
-    }
+        psError(PXTOOLS_ERR_DATA, false, "failed to find any cameras");
+        return false;
+    }
+
+    // array to hold the aggregate results
+    psArray *output = psArrayAlloc(0);
 
     for (long i = 0; i < psArrayLength(cameras); i++) {
@@ -239,4 +233,5 @@
         if (!query) {
             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            psFree(output);
             return false;
         }
@@ -251,9 +246,24 @@
         }
 
+        long camLimit = 0;
+        // avoid possible div by zero
+        if (limit) {
+            // in the case where the limit asked for is less then the number of
+            // cameras we have, return 1 imfile per camera
+            if (limit < psArrayLength(cameras)) {
+                camLimit = 1;
+            } else {
+                camLimit = limit / psArrayLength(cameras);
+                // add the modulous to the first camera in the list
+                if (i == 0) {
+                    camLimit += limit % psArrayLength(cameras);
+                }
+            }
+        }
 
         // treat limit == 0 as "no limit"
-        if (limit) {
+        if (camLimit) {
             // divide limit by the number of cameras
-            psString limitString = psDBGenerateLimitSQL((limit / psArrayLength(cameras) || 1));
+            psString limitString = psDBGenerateLimitSQL(camLimit);
             psStringAppend(&query, " %s", limitString);
             psFree(limitString);
@@ -263,39 +273,46 @@
             psError(PS_ERR_UNKNOWN, false, "database error");
             psFree(query);
+            psFree(output);
             return false;
         }
         psFree(query);
 
-        psArray *output = p_psDBFetchResult(config->dbh);
-        if (!output) {
+        psArray *result = p_psDBFetchResult(config->dbh);
+        if (!result) {
             psError(PS_ERR_UNKNOWN, false, "database error");
-            return false;
-        }
-        if (!psArrayLength(output)) {
+            psFree(output);
+            return false;
+        }
+        if (!psArrayLength(result)) {
             psTrace("pztool", PS_LOG_INFO, "no rows found");
+            psFree(result);
             psFree(output);
             return true;
         }
 
-        bool simple = false;
-        {
-            bool status = false;
-            simple = psMetadataLookupBool(&status, config->args, "-simple");
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-                psFree(output);
-                return false;
-            }
-        }
-
-        // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "pzPendingImfile", !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        // merge this query into the result set
+        pzArrayAddArray(output, result);
+        psFree(result);
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
             psFree(output);
             return false;
         }
-
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "pzPendingImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
         psFree(output);
-    }
+        return false;
+    }
+
+    psFree(output);
 
     return true;
@@ -580,2 +597,33 @@
     return true;
 }
+
+static psArray *pzGetKnownCameras(pxConfig *config)
+{
+    // get a list of cameras we've seen exps for
+    if (!p_psDBRunQuery(config->dbh, "SELECT DISTINCT camera FROM summitExp")) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    psArray *cameras = p_psDBFetchResult(config->dbh);
+    if (!cameras) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return NULL;
+    }
+    if (!psArrayLength(cameras)) {
+        psTrace("pztool", PS_LOG_INFO, "no rows found");
+        psFree(cameras);
+        return psArrayAlloc(0);
+    }
+
+    return cameras;
+}
+
+static psArray *pzArrayAddArray(psArray *array, psArray *input)
+{
+    for (long i = 0; i < psArrayLength(input); i++) {
+        psArrayAdd(array, psArrayLength(input), input->data[i]);
+    }
+
+    return array;
+}
