Index: /branches/backtrack/dbconfig/cam.md
===================================================================
--- /branches/backtrack/dbconfig/cam.md	(revision 13993)
+++ /branches/backtrack/dbconfig/cam.md	(revision 13994)
@@ -1,8 +1,10 @@
 # imfiles is not here... is it needed?
 
-camPendingExp METADATA
+camRun METADATA
     cam_id      S64         0       # Primary Key AUTO_INCREMENT
-    chip_id     S64         0       # Primary Key fkey(chip_id) ref chipProcessedExp(chip_id)
+    chip_id     S64         0       # Primary Key fkey(chip_id) ref chipRun(chip_id)
+    state       STR         64      # key
     workdir     STR         255 
+    workdir_state STR       64      # key
     label       STR         64      # key
     reduction   STR         64
@@ -14,10 +16,5 @@
 # the camPendingExp row gets deleted so we can not put a fkey on cam_id
     cam_id         S64      0       # Primary Key
-    chip_id        S64      0       # Primary Key fkey(chip_id) ref chipProcessedExp(chip_id)
-    workdir        STR      255 
-    label          STR      64      # key
-    reduction      STR      64
-    expgroup       STR      64      # key
-    dvodb          STR      255
+    chip_id        S64      0       # Primary Key fkey(cam_id, chip_id) ref camRun(cam_id, chip_id)
     uri            STR      255
     bg             F32      0.0
Index: /branches/backtrack/ippTools/scripts/camtest.sh
===================================================================
--- /branches/backtrack/ippTools/scripts/camtest.sh	(revision 13993)
+++ /branches/backtrack/ippTools/scripts/camtest.sh	(revision 13994)
@@ -8,4 +8,4 @@
 camtool -pendingimfile || exit 1
 
-camtool -addprocessedexp -cam_id 1 -chip_id 1 -uri file:///cam -bg 1 -bg_stdev 2 -bg_mean_stdev 3 -sigma_ra 1 -sigma_dec 2 -nastro 42 -path_base file:///foo -zp_mean 1 -zp_stdev 2 || exit 1
-camtool -addprocessedexp -cam_id 2 -chip_id 2 -uri file:///cam -bg 1 -bg_stdev 2 -bg_mean_stdev 3 -sigma_ra 1 -sigma_dec 2 -nastro 42 -path_base file:///foo -zp_mean 1 -zp_stdev 2 || exit 1
+camtool -addprocessedexp -cam_id 1 -uri file:///cam -bg 1 -bg_stdev 2 -bg_mean_stdev 3 -sigma_ra 1 -sigma_dec 2 -zp_mean 10 -zp_stdev 2 -fwhm 42 -fwhm_range 100 -n_stars 2 -n_extended 0 -n_astrom 42 -n_cr 10000000 -path_base file:///foo || exit 1
+camtool -addprocessedexp -cam_id 2 -uri file:///cam -bg 1 -bg_stdev 2 -bg_mean_stdev 3 -sigma_ra 1 -sigma_dec 2 -zp_mean 10 -zp_stdev 2 -fwhm 42 -fwhm_range 100 -n_stars 2 -n_extended 0 -n_astrom 42 -n_cr 10000000 -path_base file:///foo || exit 1
Index: /branches/backtrack/ippTools/share/camtool_find_chip_id.sql
===================================================================
--- /branches/backtrack/ippTools/share/camtool_find_chip_id.sql	(revision 13994)
+++ /branches/backtrack/ippTools/share/camtool_find_chip_id.sql	(revision 13994)
@@ -0,0 +1,13 @@
+-- this query is used by both camtool -pendingexp & camtool -addprocessedexp it
+-- does a little more work then is necessary for -addprocessed but it seems
+-- "cleaner" to use the same query for both cases 
+SELECT
+    *
+FROM
+    (SELECT DISTINCT
+        chipRun.*
+    FROM chipRun
+    JOIN chipProcessedExp
+        USING(chip_id)
+    WHERE
+        chipRun.state = 'stop') as Foo
Index: /branches/backtrack/ippTools/share/camtool_find_pendingexp.sql
===================================================================
--- /branches/backtrack/ippTools/share/camtool_find_pendingexp.sql	(revision 13993)
+++ /branches/backtrack/ippTools/share/camtool_find_pendingexp.sql	(revision 13994)
@@ -3,5 +3,5 @@
 -- "cleaner" to use the same query for both cases 
 SELECT
-    camPendingExp.*,
+    camRun.*,
     rawExp.exp_tag,
     rawExp.exp_id,
@@ -9,14 +9,18 @@
     rawExp.telescope,
     rawExp.filelevel
-FROM camPendingExp
-JOIN chipProcessedExp
+FROM camRun
+JOIN chipRun
+    USING(chip_id)
+JOIN chipProcessedImfile
     USING(chip_id)
 JOIN rawExp
-    ON chipProcessedExp.exp_tag = rawExp.exp_tag
+    ON chipProcessedImfile.exp_tag = rawExp.exp_tag
 LEFT JOIN camProcessedExp
-    ON camPendingExp.cam_id = camProcessedExp.cam_id
+    ON camRun.cam_id = camProcessedExp.cam_id
 LEFT JOIN camMask
-    ON camPendingExp.label = camMask.label
+    ON camRun.label = camMask.label
 WHERE
-    camProcessedExp.cam_id IS NULL
+    chipRun.state = 'stop'
+    AND camRun.state = 'run'
     AND camMask.label IS NULL
+    AND camProcessedExp.cam_id IS NULL
Index: /branches/backtrack/ippTools/share/camtool_queue_chip_id.sql
===================================================================
--- /branches/backtrack/ippTools/share/camtool_queue_chip_id.sql	(revision 13993)
+++ /branches/backtrack/ippTools/share/camtool_queue_chip_id.sql	(revision 13994)
@@ -1,14 +1,17 @@
 -- camtool only operates on exposures so we can safely queue more then one at a
 -- time without worrying about losing the track of the generated cam_id
-INSERT INTO camPendingExp
+INSERT INTO camRun
     SElECT
         0,              -- cam_id
         chip_id,        -- chip_id
+        '%s',           -- state
         '%s',           -- workdir
+        '%s',           -- workdir_state
         '%s',           -- label
-        '%s',           -- recipe
+        '%s',           -- reduction
         '%s',           -- expgroup
         '%s'            -- dvodb 
-    FROM chipProcessedExp
+    FROM chipRun
     WHERE
-        chipProcessedExp.chip_id = %lld
+        chipRun.state = 'stop'
+        AND chipRun.chip_id = %lld
Index: /branches/backtrack/ippTools/share/chiptool_completely_processed_exp.sql
===================================================================
--- /branches/backtrack/ippTools/share/chiptool_completely_processed_exp.sql	(revision 13993)
+++ /branches/backtrack/ippTools/share/chiptool_completely_processed_exp.sql	(revision 13994)
@@ -10,8 +10,7 @@
     dvodb
 FROM
-    (SELECT DISTINCT
+    (SELECT 
         chipRun.*,
         rawExp.imfiles,
-        chipProcessedImfile.exp_tag,
         chipProcessedImfile.class_id
     FROM chipRun
@@ -20,13 +19,13 @@
     JOIN rawExp
         ON chipPendingImfile.exp_tag = rawExp.exp_tag
-    LEFT JOIN chipProcessedImfile
-        USING(chip_id)
+    JOIN chipProcessedImfile
+        ON chipPendingImfile.chip_id = chipProcessedImfile.chip_id
+        AND chipPendingImfile.exp_tag = chipProcessedImfile.exp_tag
+        AND chipPendingImfile.class_id = chipProcessedImfile.class_id
     WHERE
-        chipProcessedImfile.chip_id IS NOT NULL
-        AND chipProcessedImfile.exp_tag IS NOT NULL
-        AND chipProcessedImfile.class_id IS NOT NULL
+        chipRun.state = 'run'
         AND chipProcessedImfile.fault = 0
     GROUP BY
-        chipProcessedImfile.class_id,
-        chipProcessedImfile.exp_tag
+        chipPendingImfile.chip_id,
+        chipPendingImfile.exp_tag
     HAVING rawExp.imfiles = COUNT(chipProcessedImfile.class_id)) as Foo
Index: /branches/backtrack/ippTools/src/Makefile.am
===================================================================
--- /branches/backtrack/ippTools/src/Makefile.am	(revision 13993)
+++ /branches/backtrack/ippTools/src/Makefile.am	(revision 13994)
@@ -22,4 +22,6 @@
 	pxtag.h \
 	pxregister.h \
+	pxchip.h \
+	pxcam.h \
 	pxconfig.h \
 	pxtools.h
@@ -54,4 +56,6 @@
 	pxtag.c \
 	pxregister.c \
+	pxchip.c \
+	pxcam.c \
 	chipqueue.c \
 	camqueue.c
Index: /branches/backtrack/ippTools/src/camqueue.c
===================================================================
--- /branches/backtrack/ippTools/src/camqueue.c	(revision 13993)
+++ /branches/backtrack/ippTools/src/camqueue.c	(revision 13994)
@@ -51,5 +51,7 @@
     // different on 32/64
     if (!p_psDBRunQuery(config->dbh, query,
+                "run", // state
                 workdir  ? workdir  : "NULL",
+                "dirty", //workdir_state
                 label    ? label    : "NULL",
                 recipe   ? recipe   : "NULL",
Index: /branches/backtrack/ippTools/src/camtool.c
===================================================================
--- /branches/backtrack/ippTools/src/camtool.c	(revision 13993)
+++ /branches/backtrack/ippTools/src/camtool.c	(revision 13994)
@@ -203,8 +203,13 @@
 
     // find the chipProcessedExp exposures that we want to queue up.
-    psString query = psStringCopy("SELECT * FROM chipProcessedExp");
+//    psString query = psStringCopy("SELECT * FROM chipRun WHERE chipRun.state = 'stop'");
+    psString query = pxDataGet("camtool_find_chip_id.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     if (where) {
-        psString whereClause = psDBGenerateWhereSQL(where, "chipProcessedExp");
+        psString whereClause = psDBGenerateWhereSQL(where, "");
         psFree(where);
         psStringAppend(&query, " %s", whereClause);
@@ -245,7 +250,7 @@
         psMetadata *md = output->data[i];
 
-        chipProcessedExpRow *row = chipProcessedExpObjectFromMetadata(md);
+        chipRunRow *row = chipRunObjectFromMetadata(md);
         if (!row) {
-            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipProcessedExp");
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
             psFree(output);
             return false;
@@ -601,6 +606,4 @@
     }
 
-    // start a transaction so we don't end up with an exp_tag is both
-    // camPendingExp & camProcessedExp
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -609,5 +612,5 @@
     }
 
-    camPendingExpRow *pendingRow = camPendingExpObjectFromMetadata(output->data[0]);
+    camRunRow *pendingRow = camRunObjectFromMetadata(output->data[0]);
     psFree(output);
     // create a new camProcessedImfile object
@@ -615,9 +618,4 @@
         pendingRow->cam_id,
         pendingRow->chip_id,
-        pendingRow->workdir,
-        pendingRow->label,
-        pendingRow->reduction,
-        pendingRow->expgroup,
-        pendingRow->dvodb,
         uri,
         bg,
@@ -637,4 +635,5 @@
         code
     );
+    psFree(pendingRow);
 
     // insert the new row into the camProcessedImfile table
@@ -646,23 +645,16 @@
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(row);
-        psFree(pendingRow);
-        return false;
-    }
+        return false;
+    }
+
+    // since there is only one exp per 'run' set camRun.state = 'stop'
+    if (!pxcamRunSetState(config, row->cam_id, "stop")) {
+        psError(PS_ERR_UNKNOWN, false, "failed to change camRun.state for cam_id: %" PRId64, row->cam_id);
+        psFree(row);
+        return false;
+    }
+
     psFree(row);
 
-    // delete the camPendingExp row from the database
-    if (!camPendingExpDeleteObject(config->dbh, pendingRow)) {
-        // rollback
-        if (!psDBRollback(config->dbh)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-        }
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(pendingRow);
-        return false;
-    }
-
-    psFree(pendingRow);
-
-    // point of no return for camPendingExp -> camProcessedExp
     if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
Index: /branches/backtrack/ippTools/src/chiptool.c
===================================================================
--- /branches/backtrack/ippTools/src/chiptool.c	(revision 13993)
+++ /branches/backtrack/ippTools/src/chiptool.c	(revision 13994)
@@ -30,4 +30,5 @@
 #include "pxtools.h"
 #include "pxdata.h"
+
 #include "chiptool.h"
 #include "camtool.h"
@@ -553,4 +554,6 @@
     }
 
+
+
     // XXX I've decided to make the transaction cover the Exp migration as
     // well.  Otherwise, if the last imfile in an exp is moved and the exp
@@ -827,13 +830,18 @@
     }
 
-    // insert into camPendingExp
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *row = output->data[i];
 
         chipRunRow *chipRun = chipRunObjectFromMetadata(row);
-
-        // camQueueChipID() can only be run after the chipProcessedExp entry
-        // has been inserted.
-        // queue the chip_id in the camPendingExp table
+        // set chipRun.state to 'stop'
+        if (!pxchipRunSetState(config, chipRun->chip_id, "stop")) {
+            psError(PS_ERR_UNKNOWN, false, "failed to change chipRun.state for chip_id: %" PRId64, chipRun->chip_id);
+            psFree(chipRun);
+            psFree(output);
+            return false;
+        }
+
+        // camQueueChipID() can only be run after chipRun.state has been set to
+        // stop
         if (!camQueueChipID(config,
                     chipRun->chip_id,
Index: /branches/backtrack/ippTools/src/pxchip.c
===================================================================
--- /branches/backtrack/ippTools/src/pxchip.c	(revision 13994)
+++ /branches/backtrack/ippTools/src/pxchip.c	(revision 13994)
@@ -0,0 +1,55 @@
+/*
+ * pxchip.c
+ *
+ * Copyright (C) 2006  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 <stdlib.h>
+#include <ippdb.h>
+#include <string.h>
+
+#include "pxtools.h"
+#include "pxchip.h"
+
+bool pxchipRunSetState(pxConfig *config, psS64 chip_id, const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(state, false);
+
+    // 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);
+        return false;
+    }
+
+    char *query = "UPDATE chipRun SET state = '%s' WHERE chip_id = %" PRId64;
+    if (!p_psDBRunQuery(config->dbh, query, state, chip_id)) {
+        psError(PS_ERR_UNKNOWN, false,
+                "failed to change state for chip_id %" PRId64, chip_id);
+        return false;
+    }
+
+    return true;
+}
Index: /branches/backtrack/ippTools/src/pxchip.h
===================================================================
--- /branches/backtrack/ippTools/src/pxchip.h	(revision 13994)
+++ /branches/backtrack/ippTools/src/pxchip.h	(revision 13994)
@@ -0,0 +1,29 @@
+/*
+ * pxchip.h
+ *
+ * Copyright (C) 2007  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.
+ */
+
+#ifndef PXCHIP_H
+#define PXCHIP_H 1
+
+#include <pslib.h>
+
+#include "pxtools.h"
+
+bool pxchipRunSetState(pxConfig *config, psS64 chip_id, const char *state);
+
+#endif // PXCHIP_H
Index: /branches/backtrack/ippTools/src/pxregister.c
===================================================================
--- /branches/backtrack/ippTools/src/pxregister.c	(revision 13993)
+++ /branches/backtrack/ippTools/src/pxregister.c	(revision 13994)
@@ -27,6 +27,5 @@
 
 #include "pxtools.h"
-#include "pxtag.h"
-#include "pxinject.h"
+#include "pxregister.h"
 
 bool pxnewExpSetState(pxConfig *config, const char *exp_tag, const char *state)
Index: /branches/backtrack/ippTools/src/pxregister.h
===================================================================
--- /branches/backtrack/ippTools/src/pxregister.h	(revision 13993)
+++ /branches/backtrack/ippTools/src/pxregister.h	(revision 13994)
@@ -1,6 +1,6 @@
 /*
- * register.h
+ * pxregister.h
  *
- * Copyright (C) 2006  Joshua Hoblitt
+ * Copyright (C) 2007  Joshua Hoblitt
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -18,6 +18,8 @@
  */
 
-#ifndef REGISTER_H
-#define REGISTER_H 1
+#ifndef PXREGISTER_H
+#define PXREGISTER_H 1
+
+#include <pslib.h>
 
 #include "pxtools.h"
@@ -25,3 +27,3 @@
 bool pxnewExpSetState(pxConfig *config, const char *exp_tag, const char *state);
 
-#endif // REGISTER_H
+#endif // PXREGISTER_H
Index: /branches/backtrack/ippTools/src/pxtables.c
===================================================================
--- /branches/backtrack/ippTools/src/pxtables.c	(revision 13993)
+++ /branches/backtrack/ippTools/src/pxtables.c	(revision 13994)
@@ -64,5 +64,5 @@
     CREATE_TABLE(chipProcessedImfileCreateTable);
     CREATE_TABLE(chipMaskCreateTable);
-    CREATE_TABLE(camPendingExpCreateTable);
+    CREATE_TABLE(camRunCreateTable);
     CREATE_TABLE(camProcessedExpCreateTable);
     CREATE_TABLE(camMaskCreateTable);
@@ -145,5 +145,5 @@
     DROP_TABLE(chipProcessedImfileDropTable);
     DROP_TABLE(chipMaskDropTable);
-    DROP_TABLE(camPendingExpDropTable);
+    DROP_TABLE(camRunDropTable);
     DROP_TABLE(camProcessedExpDropTable);
     DROP_TABLE(camMaskDropTable);
Index: /branches/backtrack/ippTools/src/pxtools.h
===================================================================
--- /branches/backtrack/ippTools/src/pxtools.h	(revision 13993)
+++ /branches/backtrack/ippTools/src/pxtools.h	(revision 13994)
@@ -33,4 +33,6 @@
 #include "pxtag.h"
 #include "pxregister.h"
+#include "pxchip.h"
+#include "pxcam.h"
 #include "pxconfig.h"
 
