Index: /branches/cleanup/ippTools/scripts/regtest.sh
===================================================================
--- /branches/cleanup/ippTools/scripts/regtest.sh	(revision 17862)
+++ /branches/cleanup/ippTools/scripts/regtest.sh	(revision 17863)
@@ -33,5 +33,5 @@
 $regtool -pendingexp|| exit 1
 
-$regtool -addprocessedexp -exp_id $exp_id1 -exp_name t10 -inst gpc -telescope ps1 -exp_tag batman.t10 -filelevel OTA -filter r -airmass 10 -ra 1 -decl 2 -exp_type bias -exp_time 0 -bg 10 -bg_stdev 1 -bg_mean_stdev 10 -alt 10 -az 10 -ccd_temp 45 -posang 10 -object dog -dateobs "2006-10-20T10:10:10Z" -label foobar $* || exit 1
+$regtool -addprocessedexp -exp_id $exp_id1 -exp_name t10 -inst gpc -telescope ps1 -exp_tag batman.t10 -filelevel OTA -filter r -airmass 10 -ra 1 -decl 2 -exp_type object -exp_time 0 -bg 10 -bg_stdev 1 -bg_mean_stdev 10 -alt 10 -az 10 -ccd_temp 45 -posang 10 -object dog -dateobs "2006-10-20T10:10:10Z" -label foobar $* || exit 1
 
 $regtool -pendingimfile || exit 1
@@ -43,3 +43,3 @@
 $regtool -pendingexp|| exit 1
 
-$regtool -addprocessedexp -exp_id $exp_id2 -exp_name t11 -inst gpc -telescope ps1 -exp_tag batman.t11 -filelevel OTA -filter r -airmass 11 -ra 1 -decl 2 -exp_type bias -exp_time 0 -bg 11 -bg_stdev 1 -bg_mean_stdev 11 -alt 11 -az 11 -ccd_temp 45 -posang 11 -object dog -dateobs 2006-10-20T10:10:10Z -label foobar $* || exit 1
+$regtool -addprocessedexp -exp_id $exp_id2 -exp_name t11 -inst gpc -telescope ps1 -exp_tag batman.t11 -filelevel OTA -filter r -airmass 11 -ra 1 -decl 2 -exp_type object -exp_time 0 -bg 11 -bg_stdev 1 -bg_mean_stdev 11 -alt 11 -az 11 -ccd_temp 45 -posang 11 -object dog -dateobs 2006-10-20T10:10:10Z -label foobar $* || exit 1
Index: /branches/cleanup/ippTools/share/Makefile.am
===================================================================
--- /branches/cleanup/ippTools/share/Makefile.am	(revision 17862)
+++ /branches/cleanup/ippTools/share/Makefile.am	(revision 17863)
@@ -14,4 +14,5 @@
 	chiptool_pendingimfile.sql \
 	chiptool_processedimfile.sql \
+	chiptool_run.sql \
 	chiptool_revertprocessedimfile.sql \
 	chiptool_unmasked.sql \
Index: /branches/cleanup/ippTools/share/camtool_find_chip_id.sql
===================================================================
--- /branches/cleanup/ippTools/share/camtool_find_chip_id.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/camtool_find_chip_id.sql	(revision 17863)
@@ -29,3 +29,3 @@
         using(exp_id)
     WHERE
-        chipRun.state = 'stop') as Foo
+        chipRun.state = 'full') as Foo
Index: /branches/cleanup/ippTools/share/camtool_find_pendingexp.sql
===================================================================
--- /branches/cleanup/ippTools/share/camtool_find_pendingexp.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/camtool_find_pendingexp.sql	(revision 17863)
@@ -24,5 +24,5 @@
         ON camRun.label = camMask.label
     WHERE
-        chipRun.state = 'stop'
+        chipRun.state = 'full'
         AND camRun.state = 'run'
         AND camMask.label IS NULL
Index: /branches/cleanup/ippTools/share/camtool_queue_chip_id.sql
===================================================================
--- /branches/cleanup/ippTools/share/camtool_queue_chip_id.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/camtool_queue_chip_id.sql	(revision 17863)
@@ -16,4 +16,4 @@
     FROM chipRun
     WHERE
-        chipRun.state = 'stop'
+        chipRun.state = 'full'
         AND chipRun.chip_id = %lld
Index: /branches/cleanup/ippTools/share/chiptool_pendingcleanup.sql
===================================================================
--- /branches/cleanup/ippTools/share/chiptool_pendingcleanup.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/chiptool_pendingcleanup.sql	(revision 17863)
@@ -3,3 +3,3 @@
 FROM chipRun
 WHERE
-    chipRun.state = 'goto_clean'
+    chipRun.state = 'goto_cleaned'
Index: /branches/cleanup/ippTools/share/chiptool_pendingimfile.sql
===================================================================
--- /branches/cleanup/ippTools/share/chiptool_pendingimfile.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/chiptool_pendingimfile.sql	(revision 17863)
@@ -20,5 +20,5 @@
     ON chipRun.label = chipMask.label
 WHERE
-    chipRun.state = 'run'
+    chipRun.state = 'new'
     AND chipProcessedImfile.chip_id IS NULL
     AND chipProcessedImfile.exp_id IS NULL
Index: /branches/cleanup/ippTools/share/chiptool_run.sql
===================================================================
--- /branches/cleanup/ippTools/share/chiptool_run.sql	(revision 17863)
+++ /branches/cleanup/ippTools/share/chiptool_run.sql	(revision 17863)
@@ -0,0 +1,3 @@
+SELECT
+    chipRun.*
+FROM chipRun
Index: /branches/cleanup/ippTools/share/flatcorr_completely_processed_chiprun.sql
===================================================================
--- /branches/cleanup/ippTools/share/flatcorr_completely_processed_chiprun.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/flatcorr_completely_processed_chiprun.sql	(revision 17863)
@@ -26,5 +26,5 @@
             AND rawImfile.class_id = chipProcessedImfile.class_id
         WHERE
-            chipRun.state = 'stop'
+            chipRun.state = 'full'
         GROUP BY
             chipRun.chip_id,
Index: /branches/cleanup/ippTools/share/flatcorr_find_processedimfiles.sql
===================================================================
--- /branches/cleanup/ippTools/share/flatcorr_find_processedimfiles.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/flatcorr_find_processedimfiles.sql	(revision 17863)
@@ -11,4 +11,4 @@
 WHERE
     flatcorrRun.state = 'run'
-    AND chipRun.state = 'stop'
+    AND chipRun.state = 'new'
 
Index: /branches/cleanup/ippTools/share/flatcorr_pending.sql
===================================================================
--- /branches/cleanup/ippTools/share/flatcorr_pending.sql	(revision 17862)
+++ /branches/cleanup/ippTools/share/flatcorr_pending.sql	(revision 17863)
@@ -15,5 +15,5 @@
 WHERE
     flatcorrRun.state = 'run'
-    AND chipRun.state = 'stop'
+    AND chipRun.state = 'full'
 GROUP BY
     chipRun.chip_id,
Index: /branches/cleanup/ippTools/src/Makefile.am
===================================================================
--- /branches/cleanup/ippTools/src/Makefile.am	(revision 17862)
+++ /branches/cleanup/ippTools/src/Makefile.am	(revision 17863)
@@ -69,4 +69,5 @@
 	pxregister.c \
 	pxtag.c \
+	pxtools.c \
 	pxtoolsErrorCodes.c \
 	pxtree.c \
Index: /branches/cleanup/ippTools/src/chiptool.c
===================================================================
--- /branches/cleanup/ippTools/src/chiptool.c	(revision 17862)
+++ /branches/cleanup/ippTools/src/chiptool.c	(revision 17863)
@@ -47,4 +47,5 @@
 static bool pendingcleanupMode(pxConfig *config);
 static bool donecleanupMode(pxConfig *config);
+static bool runMode(pxConfig *config);
 
 static bool chipProcessedCompleteExp(pxConfig *config);
@@ -80,4 +81,5 @@
         MODECASE(CHIPTOOL_MODE_PENDINGCLEANUP,          pendingcleanupMode);
         MODECASE(CHIPTOOL_MODE_DONECLEANUP,             donecleanupMode);
+        MODECASE(CHIPTOOL_MODE_RUN,                     runMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -1074,4 +1076,79 @@
 
 
+static bool runMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    // make sure that the state string is valid
+    if (!pxIsValidState(state)) {
+        psError(PXTOOLS_ERR_DATA, false, "%s is not a valid state", state);
+        return false;
+    }
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
+
+    psString query = pxDataGet("chiptool_run.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (where && psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereSQL(where, NULL);
+        psStringAppend(&query, " %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    // 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 *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("chiptool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    if (!convertIdToStr(output)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to convert id fields into a strings");
+        psFree(output);
+        return false;
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "chipDoneCleanup", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
+    return true;
+}
+
+
 static bool chipProcessedCompleteExp(pxConfig *config)
 {
@@ -1109,5 +1186,5 @@
         chipRunRow *chipRun = chipRunObjectFromMetadata(row);
         // set chipRun.state to 'stop'
-        if (!pxchipRunSetState(config, chipRun->chip_id, "stop")) {
+        if (!pxchipRunSetState(config, chipRun->chip_id, "full")) {
             psError(PS_ERR_UNKNOWN, false, "failed to change chipRun.state for chip_id: %" PRId64, chipRun->chip_id);
             psFree(chipRun);
Index: /branches/cleanup/ippTools/src/chiptool.h
===================================================================
--- /branches/cleanup/ippTools/src/chiptool.h	(revision 17862)
+++ /branches/cleanup/ippTools/src/chiptool.h	(revision 17863)
@@ -39,4 +39,5 @@
     CHIPTOOL_MODE_PENDINGCLEANUP,
     CHIPTOOL_MODE_DONECLEANUP,
+    CHIPTOOL_MODE_RUN
 } chiptoolMode;
 
Index: /branches/cleanup/ippTools/src/chiptoolConfig.c
===================================================================
--- /branches/cleanup/ippTools/src/chiptoolConfig.c	(revision 17862)
+++ /branches/cleanup/ippTools/src/chiptoolConfig.c	(revision 17863)
@@ -283,4 +283,11 @@
     psMetadataAddU64(donecleanupArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
 
+    // -run
+    psMetadata *runArgs = psMetadataAlloc();
+    psMetadataAddStr(runArgs, PS_LIST_TAIL, "-label",  0,       "search by label", NULL);
+    psMetadataAddBool(runArgs, PS_LIST_TAIL, "-simple",  0,     "use the simple output format", false);
+    psMetadataAddU64(runArgs, PS_LIST_TAIL, "-limit",  0,       "limit result set to N items", 0);
+    psMetadataAddStr(runArgs, PS_LIST_TAIL, "-state", 0,        "search by state (required)", NULL);
+
     psMetadata *argSets = psMetadataAlloc();
     psMetadata *modes = psMetadataAlloc();
@@ -312,4 +319,6 @@
     PXOPT_ADD_MODE("-donecleanup",          "show runs that have been cleaned",
         CHIPTOOL_MODE_DONECLEANUP,          donecleanupArgs);
+    PXOPT_ADD_MODE("-run",                  "show runs",
+        CHIPTOOL_MODE_RUN,                  runArgs);
 
     if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
Index: /branches/cleanup/ippTools/src/pxchip.c
===================================================================
--- /branches/cleanup/ippTools/src/pxchip.c	(revision 17862)
+++ /branches/cleanup/ippTools/src/pxchip.c	(revision 17863)
@@ -35,12 +35,6 @@
 
     // check that state is a valid string value
-    if (!(
-            (strncmp(state, "run", 4) == 0)
-            || (strncmp(state, "stop", 5) == 0)
-            || (strncmp(state, "reg", 4) == 0)
-        )
-    ) {
-        psError(PS_ERR_UNKNOWN, false,
-                "invalid chipRun state: %s", state);
+    if (!pxIsValidState(state)) {
+        psError(PS_ERR_UNKNOWN, false, "invalid chipRun state: %s", state);
         return false;
     }
@@ -63,14 +57,9 @@
 
     // check that state is a valid string value
-    if (!(
-            (strncmp(state, "run", 4) == 0)
-            || (strncmp(state, "stop", 5) == 0)
-            || (strncmp(state, "reg", 4) == 0)
-        )
-    ) {
-        psError(PS_ERR_UNKNOWN, false,
-                "invalid chipRun state: %s", state);
+    if (!pxIsValidState(state)) {
+        psError(PS_ERR_UNKNOWN, false, "invalid chipRun state: %s", state);
         return false;
     }
+
 
     psString query = psStringCopy("UPDATE chipRun JOIN rawExp USING(exp_id) SET state = '%s'");
@@ -151,5 +140,5 @@
             0x0, // chip_id
             exp_id,
-            "run",      // state                
+            "new",      // state                
             workdir,
             "dirty",    // workdir_state
@@ -167,2 +156,3 @@
     return psDBLastInsertID(config->dbh);
 }
+
Index: /branches/cleanup/ippTools/src/pxchip.h
===================================================================
--- /branches/cleanup/ippTools/src/pxchip.h	(revision 17862)
+++ /branches/cleanup/ippTools/src/pxchip.h	(revision 17863)
@@ -40,3 +40,4 @@
                          const char *end_stage);
 
+
 #endif // PXCHIP_H
Index: /branches/cleanup/ippTools/src/pxtools.c
===================================================================
--- /branches/cleanup/ippTools/src/pxtools.c	(revision 17863)
+++ /branches/cleanup/ippTools/src/pxtools.c	(revision 17863)
@@ -0,0 +1,43 @@
+/*
+ * pxtool.c
+ *
+ * Copyright (C) 2008  Joshua Hoblitt
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * program; see the file COPYING. If not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+
+#include "pxtools.h"
+
+bool pxIsValidState(const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(state, false);
+
+    if (!((strncmp(state, "new", 4) == 0)
+    || (strncmp(state, "full", 5) == 0)
+    || (strncmp(state, "goto_cleaned", 8) == 0)
+    || (strncmp(state, "cleaned", 8) == 0)
+    || (strncmp(state, "update", 7) == 0)
+    || (strncmp(state, "purged", 7) == 0)
+    || (strncmp(state, "goto_purged", 12) == 0))) {
+        return false;
+    }
+
+    return true;
+}
Index: /branches/cleanup/ippTools/src/pxtools.h
===================================================================
--- /branches/cleanup/ippTools/src/pxtools.h	(revision 17862)
+++ /branches/cleanup/ippTools/src/pxtools.h	(revision 17863)
@@ -41,6 +41,5 @@
 # define PXTOOL_MODE_NONE 0x0
 
-bool pxCreateTables (pxConfig *config);
-bool pxDeleteTables (pxConfig *config);
+bool pxIsValidState(const char *state);
 
 bool pxSetFaultCode(psDB *dbh, const char *tableName, psMetadata *where, psS16 code);
