Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 7073)
+++ /trunk/ippTools/src/dettool.c	(revision 7074)
@@ -14,4 +14,6 @@
 static bool addstacMode(pxConfig *config);
 static bool stacMode(pxConfig *config);
+
+static detInputExpRow *rawDetrenTodetInputExpRow(rawDetrendExpRow *rawExp, psS32 det_id);
 
 int main(int argc, char **argv)
@@ -63,7 +65,90 @@
 static bool defineMode(pxConfig *config)
 {
+    psString str    = NULL;
+    bool status     = false;
+
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    // what type of detRun is this?
+    // XXX make this flag required
+    str = psMetadataLookupStr(&status, config->args, "-det_type");
+    
+    // XXX the value being inserted is junk as we can't yet have an 'empty
+    // table'
+    detRunInsert(config->dbh, str);
+    long det_id = psDBLastInsertID(config->dbh);
+    
+    // XXX this needs to fixed to support multipe exp_ids
+    psMetadata *where = psMetadataAlloc();
+    if ((str = psMetadataLookupStr(&status, config->args, "-exp_id"))) {
+        if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", str)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psFree(where);
+            return false;
+        }
+    }
+
+    if (where->list->n < 1) {
+        psFree(where);
+        where = NULL;
+    }
+
+    psArray *rawExps = rawDetrendExpSelectRowObjects(config->dbh, where, 0);
+    psFree(where);
+    if (!rawExps) {
+        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
+        // rollback
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        return false;
+    }
+
+    detInputExpRow *detInputExp = rawDetrenTodetInputExpRow(
+        rawExps->data[0],
+        det_id
+    );
+
+    if (!detInputExpInsertObject(config->dbh, detInputExp)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        // rollback
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        return false;
+    }
+
+    // point of no return for det_id creation
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     return true;
+}
+
+static detInputExpRow *rawDetrenTodetInputExpRow(rawDetrendExpRow *rawExp, psS32 det_id)
+{
+    PS_ASSERT_PTR_NON_NULL(rawExp, NULL);
+
+    return detInputExpRowAlloc(
+        det_id,
+        rawExp->exp_id,
+        rawExp->camera,
+        rawExp->telescope,
+        rawExp->exp_type,
+        rawExp->imfiles,
+        rawExp->filter,
+        rawExp->stats
+    );
 }
 
