Index: /branches/backtrack/dbconfig/tasks.md
===================================================================
--- /branches/backtrack/dbconfig/tasks.md	(revision 13966)
+++ /branches/backtrack/dbconfig/tasks.md	(revision 13967)
@@ -1,3 +1,3 @@
-# $Id: tasks.md,v 1.134 2007-06-05 00:24:52 eugene Exp $
+# $Id: tasks.md,v 1.134.4.1 2007-06-25 20:19:18 jhoblitt Exp $
 
 # this table records all exposure ID ever seen from the summit
@@ -85,8 +85,10 @@
     exp_tag     STR         64      # Primary Key
     exp_id      STR         64      # Key
+    state       STR         64      # Key
     camera      STR         64      # Key
     telescope   STR         64      # Key
     imfiles     S32         0
     workdir     STR         255     # destination for output files
+    workdir_state STR       64
 END
 
@@ -94,6 +96,5 @@
 # from there
 newImfile METADATA
-    exp_tag     STR         64      # Primary Key
-    class       STR         64      # Primary Key
+    exp_tag     STR         64      # Primary Key fkey(exp_tag) ref newExp(exp_tag)
     class_id    STR         64      # Primary Key
     uri         STR         255
@@ -102,5 +103,5 @@
 # paired with rawImfile
 rawExp METADATA
-    exp_tag     STR         64      # Primary Key
+    exp_tag     STR         64      # Primary Key fkey(exp_tag) ref newExp(exp_tag)
     exp_id      STR         64      # Key
     camera      STR         64
@@ -135,5 +136,5 @@
 
 rawImfile METADATA
-    exp_tag     STR         64      # Primary Key
+    exp_tag     STR         64      # Primary Key fkey(exp_tag, class_id) ref newImfile(exp_tag, class_id)
     class_id    STR         64      # Primary Key
     uri         STR         255
Index: /branches/backtrack/ippTools/src/Makefile.am
===================================================================
--- /branches/backtrack/ippTools/src/Makefile.am	(revision 13966)
+++ /branches/backtrack/ippTools/src/Makefile.am	(revision 13967)
@@ -15,6 +15,12 @@
 	pztool 
 
-include_HEADERS = \
+pkginclude_HEADERS = \
 	pxtoolsErrorCodes.h \
+	pxadmin.h \
+	pxdata.h \
+	pxio.h \
+	pxtag.h \
+	pxregister.h \
+	pxconfig.h \
 	pxtools.h
 
@@ -29,9 +35,5 @@
 	difftool.h \
 	stacktool.h \
-	pxadmin.h \
 	pxinject.h \
-	pxdata.h \
-	pxio.h \
-	pxtag.h \
 	pzgetexp.h \
 	pzgetimfiles.h \
@@ -51,4 +53,5 @@
 	pxio.c \
 	pxtag.c \
+	pxregister.c \
 	chipqueue.c \
 	camqueue.c
Index: /branches/backtrack/ippTools/src/pxconfig.h
===================================================================
--- /branches/backtrack/ippTools/src/pxconfig.h	(revision 13967)
+++ /branches/backtrack/ippTools/src/pxconfig.h	(revision 13967)
@@ -0,0 +1,39 @@
+/*
+ * pxconfig.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 PXCONFIG_H
+#define PXCONFIG_H 1
+
+#include "pslib.h"
+#include "psmodules.h"
+
+typedef struct {
+    char *modeName;
+    int mode;
+    pmConfig *modules;
+    psDB *dbh;
+    psMetadata *args;
+    psMetadata *where;
+    int argc;
+    char **argv;
+} pxConfig;
+
+pxConfig *pxConfigAlloc(void);
+
+#endif // PXCONFIG_H
Index: /branches/backtrack/ippTools/src/pxinject.c
===================================================================
--- /branches/backtrack/ippTools/src/pxinject.c	(revision 13966)
+++ /branches/backtrack/ippTools/src/pxinject.c	(revision 13967)
@@ -24,11 +24,12 @@
 #include <stdlib.h>
 #include <ippdb.h>
+#include <string.h>
 
 #include "pxtools.h"
-#include "pxtag.h"
 #include "pxinject.h"
 
 static bool newExpMode(pxConfig *config);
 static bool newImfileMode(pxConfig *config);
+static bool updatenewExpMode(pxConfig *config);
 
 # define MODECASE(caseName, func) \
@@ -52,4 +53,5 @@
         MODECASE(PXINJECT_MODE_NEWEXP, newExpMode);
         MODECASE(PXINJECT_MODE_NEWIMFILE, newImfileMode);
+        MODECASE(PXINJECT_MODE_UPDATENEWEXP, updatenewExpMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -134,8 +136,10 @@
                 exp_tag,
                 exp_id,
+                "reg", // state
                 camera,
                 telescope,
                 imfiles,
-                workdir
+                workdir,
+                "dirty"
             )
         ) {
@@ -191,4 +195,5 @@
         return false;
     }
+#if 0
     psString class = psMetadataLookupStr(&status, config->args, "-class");
     if (!status) {
@@ -200,4 +205,5 @@
         return false;
     }
+#endif
     psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
     if (!status) {
@@ -220,5 +226,5 @@
 
     // insert with error flag state set to 0 (no errors)
-    if (!newImfileInsert(config->dbh, exp_tag, class, class_id, uri)) {
+    if (!newImfileInsert(config->dbh, exp_tag, class_id, uri)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
@@ -227,2 +233,36 @@
     return true;
 }
+
+
+static bool updatenewExpMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    bool status = false;
+    psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
+        return false;
+    }
+    if (!exp_tag) {
+        psError(PS_ERR_UNKNOWN, true, "-exp_tag is required");
+        return false;
+    }
+
+    psString state = psMetadataLookupStr(&status, config->args, "-state");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
+        return false;
+    }
+    if (!state) {
+        psError(PS_ERR_UNKNOWN, true, "-state is required");
+        return false;
+    }
+
+    if (state) {
+        // set detRun.state to state
+        return pxnewExpSetState(config, exp_tag, state);
+    }
+
+    return true;
+}
Index: /branches/backtrack/ippTools/src/pxinject.h
===================================================================
--- /branches/backtrack/ippTools/src/pxinject.h	(revision 13966)
+++ /branches/backtrack/ippTools/src/pxinject.h	(revision 13967)
@@ -26,5 +26,6 @@
     PXINJECT_MODE_NONE           = 0x0,
     PXINJECT_MODE_NEWEXP,
-    PXINJECT_MODE_NEWIMFILE
+    PXINJECT_MODE_NEWIMFILE,
+    PXINJECT_MODE_UPDATENEWEXP
 } pxinjectMode;
 
Index: /branches/backtrack/ippTools/src/pxinjectConfig.c
===================================================================
--- /branches/backtrack/ippTools/src/pxinjectConfig.c	(revision 13966)
+++ /branches/backtrack/ippTools/src/pxinjectConfig.c	(revision 13967)
@@ -46,26 +46,36 @@
     psMetadata *newExpArgs = psMetadataAlloc();
     psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-exp_id",  0,
-        "define the exp_id (required)", NULL);
+            "define the exp_id (required)", NULL);
     psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-inst",  0,
-        "define the camera name (required)", NULL);
+            "define the camera name (required)", NULL);
     psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-telescope",  0,
-        "define the telescope name (required)", NULL);
+            "define the telescope name (required)", NULL);
     psMetadataAddS32(newExpArgs, PS_LIST_TAIL, "-imfiles",  0,
-        "define the number of imfiles in this exp (required)", 0);
+            "define the number of imfiles in this exp (required)", 0);
     psMetadataAddStr(newExpArgs, PS_LIST_TAIL, "-workdir",  0,
-        "define workdir (required)", 0);
+            "define workdir (required)", 0);
     psMetadataAddBool(newExpArgs, PS_LIST_TAIL, "-simple",  0,
-        "use the simple output format", false);
+            "use the simple output format", false);
 
     // -newImfile
     psMetadata *newImfileArgs = psMetadataAlloc();
     psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-exp_tag",  0,
-        "define the exp_tag (required)", NULL);
+            "define the exp_tag (required)", NULL);
+#if 0
     psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-class",  0,
-        "define the class (required)", NULL);
+            "define the class (required)", NULL);
+#endif
     psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-class_id",  0,
-        "define the class ID (required)", NULL);
+            "define the class ID (required)", NULL);
     psMetadataAddStr(newImfileArgs, PS_LIST_TAIL, "-uri",  0,
-        "define the URI (required)", NULL);
+            "define the URI (required)", NULL);
+
+    // -updatenewExp
+    psMetadata *updatenewExpArgs = psMetadataAlloc();
+    psMetadataAddStr(updatenewExpArgs, PS_LIST_TAIL, "-exp_tag",  0,
+            "define the exp_tag (required)", NULL);
+    psMetadataAddStr(updatenewExpArgs, PS_LIST_TAIL, "-state", 0,
+            "set state (required)", NULL);
+
 
     psMetadata *argSets = psMetadataAlloc();
@@ -74,4 +84,5 @@
     PXTOOL_ADD_MODE("-newExp",      "", PXINJECT_MODE_NEWEXP,       newExpArgs);
     PXTOOL_ADD_MODE("-newImfile",   "", PXINJECT_MODE_NEWIMFILE,    newImfileArgs);
+    PXTOOL_ADD_MODE("-updatenewExp",   "", PXINJECT_MODE_UPDATENEWEXP,    updatenewExpArgs);
 
     if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
Index: /branches/backtrack/ippTools/src/pxregister.c
===================================================================
--- /branches/backtrack/ippTools/src/pxregister.c	(revision 13967)
+++ /branches/backtrack/ippTools/src/pxregister.c	(revision 13967)
@@ -0,0 +1,57 @@
+/*
+ * register.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 "pxtag.h"
+#include "pxinject.h"
+
+bool pxnewExpSetState(pxConfig *config, const char *exp_tag, const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(exp_tag, false);
+    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 newExp state: %s", state);
+        return false;
+    }
+
+    char *query = "UPDATE newExp SET state = '%s' WHERE exp_tag = '%s'";
+    if (!p_psDBRunQuery(config->dbh, query, state, exp_tag)) {
+        psError(PS_ERR_UNKNOWN, false,
+                "failed to change state for exp_tag %s", exp_tag);
+        return false;
+    }
+
+    return true;
+}
Index: /branches/backtrack/ippTools/src/pxregister.h
===================================================================
--- /branches/backtrack/ippTools/src/pxregister.h	(revision 13967)
+++ /branches/backtrack/ippTools/src/pxregister.h	(revision 13967)
@@ -0,0 +1,27 @@
+/*
+ * register.h
+ *
+ * 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.
+ */
+
+#ifndef REGISTER_H
+#define REGISTER_H 1
+
+#include "pxtools.h"
+
+bool pxnewExpSetState(pxConfig *config, const char *exp_tag, const char *state);
+
+#endif // REGISTER_H
Index: /branches/backtrack/ippTools/src/pxtag.h
===================================================================
--- /branches/backtrack/ippTools/src/pxtag.h	(revision 13966)
+++ /branches/backtrack/ippTools/src/pxtag.h	(revision 13967)
@@ -21,5 +21,7 @@
 #define PXTAG_H 1
 
-#include "pxtools.h"
+#include <pslib.h>
+
+#include "pxconfig.h"
 
 psString pxGenExpTag(pxConfig *config, const char *exp_id);
Index: /branches/backtrack/ippTools/src/pxtools.h
===================================================================
--- /branches/backtrack/ippTools/src/pxtools.h	(revision 13966)
+++ /branches/backtrack/ippTools/src/pxtools.h	(revision 13967)
@@ -21,30 +21,21 @@
 #define PXTOOLS_H 1
 
-# include <stdio.h>
-# include <strings.h>  // for strcasecmp
-# include <unistd.h>   // for unlink
-# include <pslib.h>
-# include <psmodules.h>
-# include <ippdb.h>
+#include <stdio.h>
+#include <strings.h>  // for strcasecmp
+#include <unistd.h>   // for unlink
+#include <pslib.h>
+#include <psmodules.h>
+#include <ippdb.h>
 
-# include "pxtoolsErrorCodes.h"
-# include "pxio.h"
-# include "pxdata.h"
+#include "pxtoolsErrorCodes.h"
+#include "pxio.h"
+#include "pxdata.h"
+#include "pxtag.h"
+#include "pxregister.h"
+#include "pxconfig.h"
 
 # define MAX_ROWS 10e9
 # define PXTOOL_MODE_NONE 0x0
 
-typedef struct {
-    char *modeName;
-    int mode;
-    pmConfig *modules;
-    psDB *dbh;
-    psMetadata *args;
-    psMetadata *where;
-    int argc;
-    char **argv;
-} pxConfig;
-
-pxConfig *pxConfigAlloc(void);
 bool pxCreateTables (pxConfig *config);
 bool pxDeleteTables (pxConfig *config);
Index: /branches/backtrack/ippTools/src/regtool.c
===================================================================
--- /branches/backtrack/ippTools/src/regtool.c	(revision 13966)
+++ /branches/backtrack/ippTools/src/regtool.c	(revision 13967)
@@ -282,4 +282,5 @@
                 return false;
             }
+            psFree(object);
             // insert the rawImfile object into the database
             if (!rawImfileInsertObject(config->dbh, imfile)) {
@@ -290,21 +291,8 @@
                 psError(PS_ERR_UNKNOWN, false, "failed to insert row into the database");
                 psFree(imfile);
-                psFree(object);
                 psFree(output);
                 return false;
             }
             psFree(imfile);
-            // remove the neImfile object from the database
-            if (!newImfileDeleteObject(config->dbh, object)) {
-                // rollback
-                if (!psDBRollback(config->dbh)) {
-                    psError(PS_ERR_UNKNOWN, false, "database error");
-                }
-                psError(PS_ERR_UNKNOWN, false, "failed to delete row from the database");
-                psFree(object);
-                psFree(output);
-                return false;
-            }
-            psFree(object);
         }
 
@@ -573,11 +561,9 @@
     }
     if (!psArrayLength(output)) {
-        psTrace("regtool", PS_LOG_INFO, "no rows found");
+        psError(PS_ERR_UNKNOWN, false, "no pending newExp rows found");
         psFree(output);
-        return true;
-    }
-
-    // start a transaction so we don't end up with an exp in both rawExp &
-    // newExp
+        return false;
+    }
+
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -603,4 +589,5 @@
             return false;
         }
+        psFree(newExp);
 
         // insert the rawExp object into the database
@@ -612,27 +599,22 @@
             psError(PS_ERR_UNKNOWN, false, "database error");
             psFree(rawExp);
-            psFree(newExp);
             psFree(output);
             return false;
         }
         psFree(rawExp);
-
-        // delete the newExp object from the database
-        if (!newExpDeleteObject(config->dbh, newExp)) {
-            // rollback
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            psFree(newExp);
-            psFree(output);
-            return false;
-        }
-
-        psFree(newExp);
 
         // if this is a detrend image don't put it in the chip queue (and we're
         // done)
         if (detrend) {
+            // set the state for the newExp to stop 
+            if (!pxnewExpSetState(config, exp_tag, "stop")) {
+                // rollback
+                if (!psDBRollback(config->dbh)) {
+                    psError(PS_ERR_UNKNOWN, false, "database error");
+                }
+                psError(PS_ERR_UNKNOWN, false, "failed to change newExp.state for exp_tag: %s", exp_tag);
+                psFree(output);
+                return false;
+            }
             continue;
         }
@@ -648,6 +630,16 @@
             return false;
         }
-    }
-
+
+        // set the state for the newExp to stop
+        if (!pxnewExpSetState(config, exp_tag, "stop")) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "failed to change newExp.state for exp_tag: %s", exp_tag);
+            psFree(output);
+            return false;
+        }
+    }
     psFree(output);
 
