Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 7423)
+++ /trunk/ippTools/src/dettool.c	(revision 7424)
@@ -101,4 +101,6 @@
         return false;
     }
+
+    // grab the 'position' column's value as the new det_id
     detRunInsert(config->dbh, det_type);
     long det_id = psDBLastInsertID(config->dbh);
@@ -106,4 +108,9 @@
     // we have to support multipe exp_ids
     psMetadataItem *item = psMetadataLookup(config->args, "-exp_id");
+    if (!item) {
+        // this shouldn't actually happen when using psArgs
+        psError(PS_ERR_UNKNOWN, true, "-exp_id is required");
+        return false;
+    }
     psMetadata *where = psMetadataAlloc();
 
@@ -115,5 +122,6 @@
         while ((mItem = psListGetAndIncrement(iter))) {
             psString exp_id = mItem->data.V;
-            // if exp_id is NULL then it means that -exp_id has not specified
+            // if exp_id is NULL then it means that -exp_id has not been
+            // specified
             if (!exp_id) {
                 psError(PS_ERR_UNKNOWN, true,
@@ -139,4 +147,5 @@
             }
         }
+        psFree(iter);
     } else {
         // rollback
@@ -1792,4 +1801,71 @@
     }
 
-    return true;
-}
+    // we have to support multipe exp_ids
+    psMetadataItem *item = psMetadataLookup(config->args, "-exp_id");
+    if (!item) {
+        // this shouldn't actually happen when using psArgs
+        psError(PS_ERR_UNKNOWN, true, "-exp_id is required");
+        return false;
+    }
+    psMetadata *where = psMetadataAlloc();
+
+    // make sure that -exp_id was parsed correctly
+    // XXX this can be removed someday
+    if (item->type == PS_DATA_METADATA_MULTI) {
+        psListIterator *iter = psListIteratorAlloc(item->data.list, 0, false);
+        psMetadataItem *mItem = NULL;
+        while ((mItem = psListGetAndIncrement(iter))) {
+            psString exp_id = mItem->data.V;
+            // if exp_id is NULL then it means that -exp_id has not been
+            // specified
+            if (!exp_id) {
+                psError(PS_ERR_UNKNOWN, true,
+                        "at least one -exp_id is required");
+                psFree(where);
+                return false;
+            }
+
+            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id",
+                        PS_META_DUPLICATE_OK, "==", exp_id)) {
+                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
+                psFree(iter);
+                psFree(where);
+                return false;
+            }
+        }
+        psFree(iter);
+    } else {
+        psAbort(config->argv[0],
+                "-exp_id was not parsed correctly (this should not happen");
+    }
+
+    if (where->list->n < 1) {
+        psFree(where);
+        where = NULL;
+    }
+
+    // check that the specified exp_ids actually exist in the iteration zero
+    // detInputExp set
+
+    // add the det_id & iteration == 0 to the where claus
+    if (!psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==",
+                (psS32)atol(det_id))) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
+        psFree(where);
+        return false;
+    }
+    if (!psMetadataAddS32(where, PS_LIST_TAIL, "iteration", 0, "==", 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item iteration");
+        psFree(where);
+        return false;
+    }
+
+    psArray *detrendExps = detInputExpSelectRowObjects(config->dbh, where, 0);
+    psFree(where);
+    if (!detrendExps) {
+        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
+        return false;
+    }
+
+    return true;
+}
