Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 16504)
+++ trunk/ippTools/src/dettool.c	(revision 16613)
@@ -1011,5 +1011,68 @@
 
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
+    PXOPT_LOOKUP_BOOL(active, config->args, "-active", false);
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+
+    PXOPT_COPY_STR(config->args, config->where, "-inst", "camera", "==");
+    PXOPT_COPY_STR(config->args, config->where, "-telescope", "telescope", "==");
+    PXOPT_COPY_STR(config->args, config->where, "-filter", "filter", "==");
+
+    // airmass_min  < airmass  < airmass_max
+    PXOPT_COPY_F32(config->args, config->where, "-airmass", "airmass_min", "<=");
+    PXOPT_COPY_F32(config->args, config->where, "-airmass", "airmass_max", ">=");
+
+    // exp_time_min < exp_time < exp_time_max
+    PXOPT_COPY_F32(config->args, config->where, "-exp_time", "exp_time_min", "<=");
+    PXOPT_COPY_F32(config->args, config->where, "-exp_time", "exp_time_max", ">=");
+
+    // ccd_temp_min < ccd_temp < ccd_temp_max
+    PXOPT_COPY_F32(config->args, config->where, "-ccd_temp", "ccd_temp_min", "<=");
+    PXOPT_COPY_F32(config->args, config->where, "-ccd_temp", "ccd_temp_max", ">=");
+
+    PXOPT_COPY_F64(config->args, config->where, "-posang", "posang_min", "<=");
+    PXOPT_COPY_F64(config->args, config->where, "-posang", "posang_max", ">=");
+
+    psString query = pxDataGet("dettool_runs.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+    
+    if (config->where && active) {
+        psString whereClause = psDBGenerateWhereSQL(config->where, "detRun");
+        psStringAppend(&query, " %s AND (detRun.state = 'run' OR detRun.state = 'stop' OR detRun.state = 'register')", whereClause);
+        psFree(whereClause);
+    }
+    if (config->where && !active) {
+        psString whereClause = psDBGenerateWhereSQL(config->where, "detRun");
+        psStringAppend(&query, " %s", whereClause);
+        psFree(whereClause);
+    }
+    if (!config->where && active) {
+        psStringAppend(&query, " WHERE (detRun.state = 'run' OR detRun.state = 'stop' OR detRun.state = 'register')");
+    }
+
+    // treat limit == 0 as "no limit"
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *runs = p_psDBFetchResult(config->dbh);
+    if (!runs) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+// XXX old version
+# if 0
     // XXX fix the hard coding of the table name
     psArray *runs = psDBSelectRows(config->dbh, "detRun", config->where, 0);
@@ -1018,4 +1081,5 @@
         return false;
     }
+# endif
 
     if (!psArrayLength(runs)) {
