Index: trunk/ippTools/src/chiptool.c
===================================================================
--- trunk/ippTools/src/chiptool.c	(revision 12131)
+++ trunk/ippTools/src/chiptool.c	(revision 12174)
@@ -111,5 +111,5 @@
 { \
     bool status = false; \
-    ps##type var = psMetadataLookup##type(&status, from, "-" #name); \
+    ps##type var = psMetadataLookup##type(&status, from, "-" name); \
     if (!status) { \
         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -" name); \
@@ -118,5 +118,5 @@
     if (!isnan(var)) { \
         if (!psMetadataAdd##type(to, PS_LIST_TAIL, #name, 0, "==", var)) { \
-            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
+            psError(PS_ERR_UNKNOWN, false, "failed to add item " name); \
             psFree(to); \
             return false; \
@@ -153,7 +153,7 @@
     ADDPARAMSTR(config->args, where, "object");
 
-    if (config->where->list->n < 1) {
-        psFree(config->where);
-        config->where = NULL;
+    if (where->list->n < 1) {
+        psFree(where);
+        where = NULL;
     }
 
@@ -165,28 +165,33 @@
     }
 
-    // XXX this does not work!!!
+    psString recipe = psMetadataLookupStr(&status, config->args, "-set_recipe");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_recipe");
+        return false;
+    }
+
+    psString expgroup = psMetadataLookupStr(&status, config->args, "-set_expgroup");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_expgroup");
+        return false;
+    }
+
+    psString dvodb = psMetadataLookupStr(&status, config->args, "-set_dvodb");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_dvodb");
+        return false;
+    }
+
+    // find the exp_tag of all the exposures that we want to queue up.
     psString query = psStringCopy(
-            "INSERT INTO chipPendingExp\n"
-            "   SElECT\n"
-            "       0\n"            // chip_id
-            "       exp_tag,\n"
-            "       'my recipe',\n"  // recipe
-            "       255\n"         // guide_version
-            "       255\n"         // chip_version
-            "       %s\n"
-            "   FROM rawExp"
-            "   LEFT JOIN chipProcessedExp"
-            "       USING(exp_tag)"
-            "   WHERE"
-            "       rawExp.fault = 0"
-        );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawExp");
+            "SELECT exp_tag FROM rawExp WHERE rawExp.fault = 0");
+    if (where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawExp");
+        psFree(where);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
 
-    if (!p_psDBRunQuery(config->dbh, query, label ? label : "NULL")) {
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
@@ -201,7 +206,104 @@
     }
     if (!psArrayLength(output)) {
-        // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no chipPendingImfile rows found");
-        psFree(output);
+        psTrace("chiptool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    // load the SQL to enqueue our exp_tags from disk once
+    psString queuerawexp_query = pxDataGet("chiptool_queuerawexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psFree(output);
+        return false;
+    }
+
+    psString queuerawimfile_query = pxDataGet("chiptool_queuerawimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psFree(queuerawexp_query);
+        psFree(output);
+        return false;
+    }
+
+    // start a transaction so we don't end up with an exp without any associted
+    // imfiles
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(queuerawimfile_query);
+        psFree(queuerawexp_query);
+        psFree(output);
+        return false;
+    }
+
+    // loop over our list of exp_tags
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+        psString exp_tag = psMetadataLookupStr(&status, md, "exp_tag");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for exp_tag");
+            psFree(queuerawimfile_query);
+            psFree(queuerawexp_query);
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp
+        if (!p_psDBRunQuery(config->dbh, queuerawexp_query,
+                    label    ? label    : "NULL",
+                    recipe   ? recipe   : "NULL",
+                    expgroup ? expgroup : "NULL",
+                    dvodb    ? dvodb    : "NULL",
+                    exp_tag
+        )) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(queuerawimfile_query);
+            psFree(queuerawexp_query);
+            psFree(output);
+            return false;
+        }
+
+        // just to be safe, we should have changed at least one row
+        if (psDBAffectedRows(config->dbh) < 1) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "no rows affected - should have changed at least one row");
+            psFree(queuerawimfile_query);
+            psFree(queuerawexp_query);
+            psFree(output);
+            return false;
+        }
+
+        // queue the imfiles for the exp we just queued
+        if (!p_psDBRunQuery(config->dbh, queuerawimfile_query)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(queuerawimfile_query);
+            psFree(queuerawexp_query);
+            psFree(output);
+            return false;
+        }
+
+        // just to be safe, we should have changed at least one row
+        if (psDBAffectedRows(config->dbh) < 1) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "no rows affected - should have changed at least one row");
+            psFree(queuerawimfile_query);
+            psFree(queuerawexp_query);
+            psFree(output);
+            return false;
+        }
+    }
+    psFree(queuerawimfile_query);
+    psFree(queuerawexp_query);
+    psFree(output);
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
     }
@@ -776,14 +878,4 @@
     }
 
-    psString recipe = psMetadataLookupStr(&status, config->args, "-recip");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -recip");
-        return false;
-    }
-    if (!recipe) {
-        psError(PS_ERR_UNKNOWN, true, "-recip is required");
-        return false;
-    }
-
     psF64 bg = psMetadataLookupF64(&status, config->args, "-bg");
     if (!status) {
@@ -820,8 +912,5 @@
     return chipProcessedImfileRowAlloc(
         imfile->chip_id,
-        imfile->exp_tag,
-        imfile->guide_id,
         imfile->class_id,
-        recipe,
         uri,
         bg,
