Index: trunk/pstamp/src/pstampparse.c
===================================================================
--- trunk/pstamp/src/pstampparse.c	(revision 16239)
+++ trunk/pstamp/src/pstampparse.c	(revision 16278)
@@ -1,3 +1,4 @@
-// pstampparse  - read a fits table describing a pstamp request and prepares it for processing
+// pstampparse  - read a fits table describing a postage stamp request and prepare the request
+// for processing
 
 #include <pslib.h>
@@ -13,5 +14,7 @@
     psString    roiString;
     pspMode     mode;
+    psS64       req_id;
     bool        verbose;
+    bool        simple;
 } pspOptions;
 
@@ -60,5 +63,4 @@
 
     if ((argnum = psArgumentGet(argc, argv, "-mode"))) {
-        options->verbose = true;
         psArgumentRemove(argnum, &argc, argv);
         if (argnum == argc) {
@@ -69,7 +71,21 @@
         psArgumentRemove(argnum, &argc, argv);
     }
+    if ((argnum = psArgumentGet(argc, argv, "-req_id"))) {
+        psArgumentRemove(argnum, &argc, argv);
+        if (argnum == argc) {
+            fprintf(stderr, "value required for mode\n");
+            usage();
+        }
+        options->req_id = atol(argv[argnum]);
+        psArgumentRemove(argnum, &argc, argv);
+    }
 
     if ((argnum = psArgumentGet(argc, argv, "-verbose"))) {
         options->verbose = true;
+        psArgumentRemove(argnum, &argc, argv);
+    }
+
+    if ((argnum = psArgumentGet(argc, argv, "-simple"))) {
+        options->simple = true;
         psArgumentRemove(argnum, &argc, argv);
     }
@@ -262,7 +278,7 @@
     }
 
-    // XXX: Hmm if we're doing last(runQuery(....)) we need to free the
-    // result array, however freeing this in here seems like a dangerous API
-    // but hey it's a static function
+    // XXX: Hmm if we're doing lastID(runQuery(....)) we need to free the
+    // result array, however freeing this in here seems like a potentially dangerous API
+    // but hey we're a static function
 
     psFree(array);
@@ -571,4 +587,55 @@
 
 static bool
+queueOneJob (pspOptions *options, psString uri, psString outputBase, psString commandArgs)
+{
+    psString cmd = NULL;
+
+    psStringAppend(&cmd, "pstamptool -addjob -req_id %ld -uri %s -outputBase %s -args '%s' >/dev/null",
+                                options->req_id, uri, outputBase, commandArgs);
+    if (options->verbose) {
+        fprintf(stderr, "excuting system(%s)\n", cmd);
+    }
+
+    int rstatus = system(cmd);
+
+    if (rstatus) {
+        fprintf(stderr, "%s failed with status: %d\n", cmd, rstatus);
+    }
+    psFree(cmd);
+
+    // TODO: should we be returning the exit status so that it can be reported?
+    return !rstatus;
+}
+
+static bool
+queueJobs (pspOptions *options, psArray *uris, psString user_tag, psString commandArgs)
+{
+    int numURIs = psArrayLength(uris);
+    bool status = false;
+
+    if (numURIs == 1) {
+        // use the user tag as the outputBase
+        status = queueOneJob(options, uris->data[0], user_tag, commandArgs);
+    } else {
+        for (int i = 0; i< numURIs; i++) {
+            psString outputBase = NULL;
+            
+            // append an integer to the user_tag to get a uniqueu outputBase
+            psStringAppend(&outputBase, "%s_%d", user_tag, i);
+
+            status = queueOneJob(options, uris->data[i], outputBase, commandArgs);
+            psFree(outputBase);
+
+            if (!status) {
+                break;
+            }
+        }
+    }
+
+    return status;
+}
+
+
+static bool
 parseRequestTable(pspOptions *options)
 {
@@ -581,6 +648,12 @@
             options->mode = parseMode(cmd_mode);
         } else {
+            // default to listing the parameters of the job's that the request would queue
             options->mode = PSP_MODE_LIST_JOB;
         }
+    }
+
+    if ((options->mode == PSP_MODE_QUEUE_JOB) && (options->req_id <= 0)) {
+        fprintf(stderr, "need request id in order to queue jobs\n");
+        return false;
     }
 
@@ -597,6 +670,4 @@
     }
 
-    // XXX switch this to have the various parsing functions return the list of uris which 
-    // we will process here
     psArray *uris = NULL;
     if (!strcmp(req_type, "byid")) {
@@ -626,8 +697,10 @@
             fprintf(stdout, "%s\n", (psString) uris->data[i]);
         }
-    } else if (options->mode == PSP_MODE_LIST_JOB) {
+    } else {
         psString commandArgs = roiString;
         psString class_id = psMetadataLookupStr(&status, options->md, "CLASS_ID");
         if (class_id) {
+            // TODO: the ppstamp argument needs to change to class_id
+            // perhaps allow chip as a synonym
             psStringAppend(&commandArgs, " -chip %s", class_id); 
         }
@@ -639,12 +712,25 @@
         }
 
-        for (int i = 0; i < numURIs; i++) {
-            printf("%s %s %s\n", (psString) uris->data[i], user_tag, commandArgs);
-        }
-    } else {
-        fprintf(stderr, "PSP_MODE_QUEUE_JOB not implemented yet\n");
-        return false;
-    }
-
+        if (options->mode == PSP_MODE_LIST_JOB) {
+            if (numURIs == 1) {
+                printf("%s %s %s\n", (psString) uris->data[0], user_tag, commandArgs);
+            } else {
+                // add an integer to the user_tag
+                for (int i = 0; i < numURIs; i++) {
+                    printf("%s %s_%d %s\n", (psString) uris->data[i], user_tag, i, commandArgs);
+                }
+            }
+        } else if (options->mode == PSP_MODE_QUEUE_JOB) {
+
+            if (!queueJobs(options, uris, user_tag, commandArgs)) {
+                return false;
+            }
+
+        } else {
+            // this error is actually caught by parseMode but we might as well check
+            fprintf(stderr, "unknown command mode found: %d\n", options->mode);
+            exit(1);
+        }
+    }
 
     return true;
