Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 19769)
+++ trunk/ippTools/src/dettool.c	(revision 19771)
@@ -722,8 +722,26 @@
     
     // XXX this mode is not well-tested: probably need to specify iteration here
-    // XXX pass the supplied det_id and iteration as ref_det_id, or get from command line?
     PXOPT_LOOKUP_S64(det_id, config->args, "-det_id", true, false); // required
     PXOPT_LOOKUP_STR(det_type, config->args, "-set_det_type", false, false); // optional
+
     PXOPT_LOOKUP_STR(mode, config->args, "-set_mode", false, false); // optional
+    if (!isValidMode(config, mode)) {
+        psError(PS_ERR_UNKNOWN, false, "invalid mode");
+        return false;
+    }
+
+    // get -ref_det_id and -ref_iter : required for 'verify' mode / disallowed otherwise
+    PXOPT_LOOKUP_S64(ref_det_id, config->args, "-ref_det_id", false, false);
+    PXOPT_LOOKUP_S32(ref_iter, config->args, "-ref_iter", false, false);
+    if (!strncmp(mode, "verify", 7) && ((ref_det_id == 0) || (ref_iter == -1))) {
+        psError(PS_ERR_UNKNOWN, false, "verify mode requires both -ref_det_id and -ref_iter");
+        return false;
+    } 
+    if (strncmp(mode, "verify", 7) && ((ref_det_id != 0) || (ref_iter != -1))) {
+        psError(PS_ERR_UNKNOWN, false, "master mode cannot have -ref_det_id or -ref_iter set");
+        return false;
+    } 
+
+    // the new detRun may have different values for these limits:
     PXOPT_LOOKUP_STR(exp_type, config->args, "-set_exp_type", false, false);
     PXOPT_LOOKUP_STR(filelevel, config->args, "-set_filelevel", false, false);
@@ -742,16 +760,13 @@
     PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false);
     PXOPT_LOOKUP_TIME(time_begin, config->args, "-set_time_begin", false, false);
-    PXOPT_LOOKUP_TIME(time_end, config->args, "-set_end", false, false);
-    PXOPT_LOOKUP_TIME(use_begin, config->args, "-use_begin", false, false);
-    PXOPT_LOOKUP_TIME(use_end, config->args, "-use_end", false, false);
+    PXOPT_LOOKUP_TIME(time_end, config->args, "-set_time_end", false, false);
+    PXOPT_LOOKUP_TIME(use_begin, config->args, "-set_use_begin", false, false);
+    PXOPT_LOOKUP_TIME(use_end, config->args, "-set_use_end", false, false);
     PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
     PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); // optional
 
-    // check mode
-    if (mode && !isValidMode(config, mode)) {
-        psError(PS_ERR_UNKNOWN, false, "invalud mode");
-        return false;
-    }
+    // reference iteration to use (otherwise last iteration)
+    PXOPT_LOOKUP_S32(iteration, config->args, "-iteration", false, false); // for specifying input exp restrictions
 
     // lookup the detRun that we will be basing this one on
@@ -764,15 +779,14 @@
     }
     if (!detRuns) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
+        psError(PS_ERR_UNKNOWN, false, "database error: no matching det_id found");
+        return false;
+    }
+
     // sanity check the result... we should have only found one det_id
     if (psArrayLength(detRuns) != 1) {
         psAbort("found more then one detRun matching det_id %" PRId64 " (this should not happen)", det_id);
-        return false;                   // unreachable
     }
 
     // pull the detRun object out the result array
-    // XXX need to assign or re-assign the ref_det_id and ref_iter values
     detRunRow *detRun = psMemIncrRefCounter(detRuns->data[0]);
 
@@ -780,9 +794,13 @@
     psFree(detRuns);
 
-    // set the det_id to 0/NULL so the database can assign it
-    detRun->det_id = 0;
-
-    // reset the iteration to 0
-    detRun->iteration = 0;
+    if (iteration < 0) {
+	iteration = detRun->iteration;
+    }
+
+    detRun->det_id = 0; // set the det_id to 0/NULL so the database can assign it
+    detRun->iteration = 0; // reset the iteration to 0
+
+    detRun->ref_det_id = ref_det_id; // not inherited: may only be set for 'verify' run
+    detRun->ref_iter = ref_iter;  // not inherited: may only be set for 'verify' run
 
     // reset the state to "run"
@@ -790,5 +808,6 @@
     detRun->state = psStringCopy("run");
 
-    // walk through the optional values and update the detRun as required
+    // walk through the optional values and update the detRun as required.  Otherwise, these
+    // value are inherited from the specified detRun
     if (det_type) {
         psFree(detRun->det_type);
@@ -898,15 +917,16 @@
     // create a metadata to restrict detInputExp's be in in the specified range
 
-    psMetadata *time_filter = psMetadataAlloc();
-
-    // XXX: possible mem leak: PXOPT_COPY* will free time_filter but NOTHING
-    // that has previously been allocated
-    PXOPT_COPY_TIME(config->args, time_filter, "-set_input_begin", "dateobs", ">=");
-    PXOPT_COPY_TIME(config->args, time_filter, "-set_input_end", "dateobs", "<");
+    psMetadata *input_filter = psMetadataAlloc();
+
+    // additional restriction on the detInputExp's to be selected
+    PXOPT_COPY_TIME(config->args, input_filter, "-set_input_begin", "dateobs", ">=");
+    PXOPT_COPY_TIME(config->args, input_filter, "-set_input_end", "dateobs", "<");
+
+    PXOPT_LOOKUP_BOOL(only_accepted, config->args, "-only_accepted", false); // optional
 
     // start a transaction so we don't end up with childlessed det_ids
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(time_filter);
+        psFree(input_filter);
         psFree(detRun);
         return false;
@@ -919,5 +939,5 @@
             psError(PS_ERR_UNKNOWN, false, "database error");
         }
-        psFree(time_filter);
+        psFree(input_filter);
         psFree(detRun);
         return false;
@@ -934,12 +954,19 @@
     }
 
-    if (time_filter->list->n) {
-        psString whereClause = psDBGenerateWhereConditionSQL(time_filter, "rawExp");
+    if (input_filter->list->n) {
+        psString whereClause = psDBGenerateWhereConditionSQL(input_filter, "rawExp");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
-    psFree(time_filter);
-
-    if (!p_psDBRunQuery(config->dbh, query, (long long) newDet_id, (long long) det_id)) {
+    psFree(input_filter);
+
+    if (only_accepted) {
+        psString whereClause = NULL;
+	psStringAppend(&whereClause, " AND accept = 1");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query, (long long) newDet_id, (long long) det_id, iteration)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
