Index: /trunk/ippTools/src/regtool.c
===================================================================
--- /trunk/ippTools/src/regtool.c	(revision 6285)
+++ /trunk/ippTools/src/regtool.c	(revision 6286)
@@ -5,4 +5,5 @@
 static bool pendingMode(pxConfig *config);
 static bool updateMode(pxConfig *config);
+static psArray *newFrameSearchPending(pxConfig *config);
 
 int main(int argc, char **argv)
@@ -32,10 +33,13 @@
 static bool pendingMode(pxConfig *config)
 {
-    psArray *pendingFrames = newFrameSearch(config);
-    if (!pendingFrames) {
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psArray *new = newFrameSearchPending(config);
+    if (!new) {
         psError(PS_ERR_UNKNOWN, false, "no newFrames found");
         return false;
     }
-    bool status = newFramePrint(stdout, config, pendingFrames);
+
+    bool status = newFramePrint(stdout, config, new);
     if (!status) {
         psError(PS_ERR_UNKNOWN, false,"newFramePrint() failed");
@@ -48,85 +52,91 @@
 static bool updateMode(pxConfig *config)
 {
-    // get exp_id/class/class_id/url from the CLI // add the completed imfile to
-    // the p2DoneImfile tables // remove corresponding entries from the
-    // p2PendingImfile table // check to see if any p2PendingExps have no
-    // associated p2PendingImfiles //    if so move the p2PendingExp(s) to
-    // p2DoneExp
+    PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // find pending
-    psArray *pendingImfiles = p2searchPendingImfiles(config);
-    if (!pendingImfiles) {
-        psError(PS_ERR_UNKNOWN, false, "no p2PendingImfiles found");
-        return false;
-    }
-    // insert into done
-    psArray *doneImfiles = p2pendingToDoneImfile(pendingImfiles);
-    for (int i = 0; i < doneImfiles->n; i++) {
-        if (!p2DoneImfileInsertObject(config->database, doneImfiles->data[i])) {
-            psError(PS_ERR_UNKNOWN, false, "database access failed");
-            return false;
+    psArray *new = newFrameSearchPending(config);
+
+    // insert 'new' rawScience & detrendframes
+    if (new->n > 0) {
+        for (long i = 0; i < new->n; i++) {
+            newFrame *newFrame = new->data[i];
+            if (strcmp(newFrame->exposure->exp_type, "object") == 0) {
+                // it's a science frame
+                rawScienceFrame *rawScienceFrame = newToRawScienceFrame(newFrame);
+                if (!rawScienceFrameInsert(config, rawScienceFrame)) {
+                    // error
+                }
+            } else {
+                // it's a detrend frame
+                rawDetrendFrame *rawDetrendFrame = newToRawDetrendFrame(newFrame); 
+                if (!rawDetrendFrameInsert(config, rawDetrendFrame)) {
+                    // error
+                }
+            }
         }
     }
-    // delete from pending
-    if (p2PendingImfileDeleteRowObjects(config->database, pendingImfiles, MAX_ROWS) < 0) {
-        // there must be atleast 1 Imfile to get this far
-        psError(PS_ERR_UNKNOWN, false, "database access failed");
+
+    return true;
+}
+
+static psArray *newFrameSearchPending(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+
+    psArray *new = newFrameSearch(config);
+    if (!new) {
+        psError(PS_ERR_UNKNOWN, false, "no newFrames found");
         return false;
     }
 
-    // look for pending exposures
-    psArray *pendingExps = p2searchPendingExp(config);
-    if (!pendingExps) {
-        psError(PS_ERR_UNKNOWN, false, "no p2PendingExps found");
-        return false;
+    psArray *raw = rawScienceFrameSearch(config);
+    if (!raw) {
+        psError(PS_ERR_UNKNOWN, false, "no rawScienceFrames found");
+    //    return false;
     }
 
-    for (int i = 0; i < pendingExps->n; i++) {
-        p2PendingExpRow *pendingExp = pendingExps->data[i];
+    psArray *rawDetrend = rawDetrendFrameSearch(config);
+    if (!rawDetrend) {
+        psError(PS_ERR_UNKNOWN, false, "no rawDetrendFrames found");
+    //    return false;
+    }
 
-        psMetadata *where = psMetadataAlloc();
-        psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", pendingExp->exp_id);
-        psArray *pendingImfiles = p2PendingImfileSelectRowObjects(config->database, where, MAX_ROWS);
-        psFree(where)
-        if (!pendingImfiles) {
-            // exp has no coresponding imfiles
-            psArray *nukeMe = psArrayAlloc(1);
-            nukeMe->n = 0;
-            psArrayAdd(nukeMe, 0, pendingExp);
-            bool status = p2PendingExpDeleteRowObjects(config->database, nukeMe, MAX_ROWS);
-            psFree(nukeMe);
-            if (!status) {
-                psError(PS_ERR_UNKNOWN, false, "database access failed");
-                return false;
-            }
-            // XXX need a func to convert from p2PendingExp -> p2DoneExp
-
-            p2DoneExpRow *doneExp = p2pendingToDoneExp(pendingExp);
-            if (!doneExp) {
-                psError(PS_ERR_UNKNOWN, false, "p2PendingExp -> p2DoneExp failed");
-                return false;
-            }
-            if (!p2DoneExpInsertObject(config->database, doneExp)) {
-                psError(PS_ERR_UNKNOWN, false, "database access failed");
-                return false;
+    // ignore duplicate raw frames
+    if (raw) {
+        for (long i = 0; i < new->n; i++) {
+            newFrame *newFrame = new->data[i];
+            for (long j = 0; j < raw->n; j++) {
+                rawScienceFrame *rawScienceFrame = raw->data[j];
+                if (strcmp(newFrame->exposure->exp_id,
+                           rawScienceFrame->exposure->exp_id) == 0) {
+                    psArrayRemove(new, newFrame);
+                    // dec the counter as the array just got shorter
+                    //and we don't want to skip elemnts
+                    i--;
+                    break;
+                }
             }
         }
-        // skip phase 3 for the time being
-        psFree(pendingImfiles);
+        psFree(raw);
     }
 
-    /*
-    psArray *doneFrames = p2pendingToDone(config, pendingFrames);
-    if (!doneFrames) {
-        psError(PS_ERR_UNKNOWN, false, "p2pendingToDone() failed");
-        return false;
+    // ignore duplicate rawDetrend frames
+    if (rawDetrend) {
+        for (long i = 0; i < new->n; i++) {
+            newFrame *newFrame = new->data[i];
+            for (long j = 0; j < rawDetrend->n; j++) {
+                rawDetrendFrame *rawDetrendFrame = rawDetrend->data[j];
+                if (strcmp(newFrame->exposure->exp_id,
+                           rawDetrendFrame->exposure->exp_id) == 0) {
+                    psArrayRemove(new, newFrame);
+                    // dec the counter as the array just got shorter
+                    //and we don't want to skip elemnts
+                    i--;
+                    break;
+                }
+            }
+        }
+        psFree(rawDetrend);
     }
-    bool status = p2insertDoneFrames(config, doneFrames);
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "p2insertDoneFrames() failed");
-        return false;
-    }
-    */
 
-    return true;
+    return new;
 }
