Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 11814)
+++ trunk/ippTools/src/stacktool.c	(revision 11851)
@@ -33,26 +33,11 @@
 static bool definerunMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
-static bool addinputexpMode(pxConfig *config);
-static bool expMode(pxConfig *config);
-static bool imfileMode(pxConfig *config);
-static bool tooverlapMode(pxConfig *config);
-static bool addoverlapMode(pxConfig *config);
-static bool scmapMode(pxConfig *config);
-static bool towarpedMode(pxConfig *config);
-static bool addwarpedMode(pxConfig *config);
-static bool warpedMode(pxConfig *config);
-
-#if 0
-static bool tostackedimfileMode(pxConfig *config);
-//static bool addstackedimfileMode(pxConfig *config);
-static bool stackedimfileMode(pxConfig *config);
-static bool todiffimfileMode(pxConfig *config);
-//static bool adddiffimfileMode(pxConfig *config);
-static bool diffimfileMode(pxConfig *config);
-#endif 
-
-static bool parseAndInsertSkyCellMap(pxConfig *config, const char *mapfile);
-static bool setp4RunState(pxConfig *config, const char *p4_id, const char *state);
-static bool isValidMode(pxConfig *config, const char *mode);
+static bool addinputscfileMode(pxConfig *config);
+static bool inputscfileMode(pxConfig *config);
+static bool tosumMode(pxConfig *config);
+static bool addsumscfileMode(pxConfig *config);
+static bool sumscfileMode(pxConfig *config);
+
+static bool setp6RunState(pxConfig *config, const char *p4_id, const char *state);
 
 # define MODECASE(caseName, func) \
@@ -68,28 +53,17 @@
 
     pxConfig *config = p6toolConfig(NULL, argc, argv);
+    if (!config) {
+        psError(PXTOOLS_ERR_CONFIG, false, "failed to configure");
+        goto FAIL;
+    }
 
     switch (config->mode) {
         MODECASE(P6TOOL_MODE_DEFINERUN,         definerunMode);
         MODECASE(P6TOOL_MODE_UPDATERUN,         updaterunMode);
-        MODECASE(P6TOOL_MODE_ADDINPUTEXP,       addinputexpMode);
-        MODECASE(P6TOOL_MODE_EXP,               expMode);
-        MODECASE(P6TOOL_MODE_IMFILE,            imfileMode);
-        MODECASE(P6TOOL_MODE_TOOVERLAP,         tooverlapMode);
-        MODECASE(P6TOOL_MODE_ADDOVERLAP,        addoverlapMode);
-        MODECASE(P6TOOL_MODE_SCMAP,             scmapMode);
-        MODECASE(P6TOOL_MODE_TOWARPED,          towarpedMode);
-        MODECASE(P6TOOL_MODE_ADDWARPED,         addwarpedMode);
-        MODECASE(P6TOOL_MODE_WARPED,            warpedMode);
-
-#if 0
-        MODECASE(P6TOOL_MODE_ADDSCFILE,         addscfileMode);
-        MODECASE(P6TOOL_MODE_SCFILE,            scfileMode);
-        MODECASE(P6TOOL_MODE_TOSTACKEDIMFILE,   tostackedimfileMode);
-//        MODECASE(P6TOOL_MODE_ADDSTACKEDIMFILE,  addstackedimfileMode);
-        MODECASE(P6TOOL_MODE_STACKEDIMFILE,     stackedimfileMode);
-        MODECASE(P6TOOL_MODE_TODIFFIMFILE,      todiffimfileMode);
-//        MODECASE(P6TOOL_MODE_ADDDIFFIMFILE,     adddiffimfileMode);
-        MODECASE(P6TOOL_MODE_DIFFIMFILE,        diffimfileMode);
-#endif
+        MODECASE(P6TOOL_MODE_ADDINPUTSCFILE,    addinputscfileMode);
+        MODECASE(P6TOOL_MODE_INPUTSCFILE,       inputscfileMode);
+        MODECASE(P6TOOL_MODE_TOSUM,             tosumMode);
+        MODECASE(P6TOOL_MODE_ADDSUMSCFILE,      addsumscfileMode);
+        MODECASE(P6TOOL_MODE_SUMSCFILE,         sumscfileMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -104,4 +78,5 @@
 FAIL:
     psErrorStackPrint(stderr, "\n");
+    int exit_status = pxerrorGetExitStatus();
 
     psFree(config);
@@ -109,5 +84,5 @@
     psLibFinalize();
 
-    exit(EXIT_FAILURE);
+    exit(exit_status);
 }
 
@@ -126,9 +101,4 @@
     if (!mode) {
         psError(PS_ERR_UNKNOWN, true, "-mode is required");
-        return false;
-    }
-    // check mode
-    if (mode && !isValidMode(config, mode)) {
-        psError(PS_ERR_UNKNOWN, false, "invalud mode");
         return false;
     }
@@ -230,12 +200,12 @@
     if (state) {
         // set detRun.state to state
-        return setp4RunState(config, p4_id, state);
-    }
-
-    return true;
-}
-
-
-static bool addinputexpMode(pxConfig *config)
+        return setp6RunState(config, p4_id, state);
+    }
+
+    return true;
+}
+
+
+static bool addinputscfileMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -293,96 +263,5 @@
 
 
-static bool expMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    // find all rawImfiles matching the default query
-    psString query = psStringCopy( 
-        "SELECT\n"
-        "   p3ProcessedExp.*\n"
-        " FROM p4Run\n"
-        " JOIN p4InputExp\n"
-        "   USING(p4_id)\n"
-        " JOIN p3ProcessedExp\n"
-        "   ON p4InputExp.exp_tag = p3ProcessedExp.exp_tag\n"
-        "   AND p4InputExp.p3_version = p3ProcessedExp.p3_version\n"
-        " WHERE\n"
-        "   p4Run.state = 'run'\n" 
-        "   AND p3ProcessedExp.fault = 0\n"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4InputExp");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psErrorCode err = psErrorCodeLast();
-        switch (err) {
-            case PS_ERR_DB_CLIENT:
-                psError(PXTOOLS_ERR_SYS, false, "database error");
-            case PS_ERR_DB_SERVER:
-                psError(PXTOOLS_ERR_PROG, false, "database error");
-            default:
-                psError(PXTOOLS_ERR_PROG, false, "unknown error");
-        }
-
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    if (psArrayLength(output)) {
-        // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4InputExp", !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(output);
-            return false;
-        }
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool imfileMode(pxConfig *config)
+static bool inputscfileMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -472,5 +351,5 @@
 
 
-static bool tooverlapMode(pxConfig *config)
+static bool tosumMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -570,5 +449,5 @@
 
 
-static bool addoverlapMode(pxConfig *config)
+static bool addsumscfileMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -590,13 +469,4 @@
     }
 
-    if (!parseAndInsertSkyCellMap(config, mapfile)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to inject mapfile: %s into the database", mapfile);
-        // rollback
-        if (!psDBRollback(config->dbh)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-        }
-        return false;
-    }
-
     // point of no return
     if (!psDBCommit(config->dbh)) {
@@ -609,65 +479,5 @@
 
 
-static bool parseAndInsertSkyCellMap(pxConfig *config, const char *mapfile)
-{
-    unsigned int nFail = 0;
-    psMetadata *skycells = psMetadataConfigRead(NULL, &nFail, mapfile, false);
-    if (!skycells) {
-        psError(PS_ERR_UNKNOWN, false, "failed to parse mapfile: %s", mapfile);
-        return false;
-    }        
-    if (nFail) {
-        psError(PS_ERR_UNKNOWN, false, "there were %d errors parsing mapfile: %s", nFail, mapfile);
-        psFree(skycells);
-        return false;
-    }
-
-    psMetadataItem *item = NULL;
-    psMetadataIterator *iter = psMetadataIteratorAlloc(skycells, 0, NULL);
-    if ((item = psMetadataGetAndIncrement(iter))) {
-        if (item->type != PS_DATA_METADATA) {
-            psError(PS_ERR_UNKNOWN, false, "mapfile: %s is in the wrong format", mapfile);
-            psFree(iter);
-            psFree(skycells);
-            return false;
-        }
-
-        psMetadata *sc = item->data.md;
-        // this conversion isn't strictly nessicary but it's an easy way of
-        // validating the format
-        p4SkyCellMapRow *row = p4SkyCellMapObjectFromMetadata(sc);
-        if (!row) {
-            psError(PS_ERR_UNKNOWN, false, "failed to convert mapfile: %s metdata entry into a p4SkyCellMap object", mapfile);
-            psFree(iter);
-            psFree(skycells);
-            return false;
-        }
-
-        if (!p4SkyCellMapInsertObject(config->dbh, row)) {
-            psErrorCode err = psErrorCodeLast();
-            switch (err) {
-                case PS_ERR_DB_CLIENT:
-                    psError(PXTOOLS_ERR_SYS, false, "database error");
-                case PS_ERR_DB_SERVER:
-                    psError(PXTOOLS_ERR_PROG, false, "database error");
-                default:
-                    psError(PXTOOLS_ERR_PROG, false, "unknown error");
-            }
-            psFree(row);
-            psFree(iter);
-            psFree(skycells);
-            return false;
-        }
-
-        psFree(row);
-    }
-    psFree(iter);
-    psFree(skycells);
-
-    return true;
-}
-
-
-static bool scmapMode(pxConfig *config)
+static bool sumscfileMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -683,7 +493,7 @@
     psString query = psStringCopy( 
         "SELECT\n"
-        "   p4SkyCellMap.*\n"
+        "   p4Scfile.*\n"
         " FROM p4Run\n"
-        " JOIN p4SkyCellMap\n"
+        " JOIN p4Scfile\n"
         "   USING(p4_id)\n"
         " WHERE\n"
@@ -692,5 +502,5 @@
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4SkyCellMap");
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4Scfile");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -726,5 +536,5 @@
     }
     if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4SkyCellMap rows found");
+        psError(PS_ERR_UNKNOWN, false, "no p4Scfile rows found");
         psFree(output);
         return true;
@@ -743,279 +553,4 @@
     if (psArrayLength(output)) {
         // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4SkyCellMap", !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(output);
-            return false;
-        }
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool towarpedMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    // find all rawImfiles matching the default query
-    psString query = psStringCopy( 
-        "SELECT\n"
-        "   p4SkyCellMap.*\n"
-        " FROM p4Run\n"
-        " JOIN p4SkyCellMap\n"
-        "   USING(p4_id)\n"
-        " LEFT JOIN p4Scfile\n"
-        "   USING(p4_id, skycell_id, tess_id, exp_tag, p3_version)\n"
-        " WHERE\n"
-        "   p4Run.state = 'run'\n" 
-        "   AND p4Scfile.p4_id IS NULL\n"
-        "   AND p4Scfile.skycell_id IS NULL\n"
-        "   AND p4Scfile.tess_id IS NULL\n"
-        "   AND p4Scfile.exp_tag IS NULL\n"
-        "   AND p4Scfile.p3_version IS NULL\n"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4SkyCellMap");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psErrorCode err = psErrorCodeLast();
-        switch (err) {
-            case PS_ERR_DB_CLIENT:
-                psError(PXTOOLS_ERR_SYS, false, "database error");
-            case PS_ERR_DB_SERVER:
-                psError(PXTOOLS_ERR_PROG, false, "database error");
-            default:
-                psError(PXTOOLS_ERR_PROG, false, "unknown error");
-        }
-
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4PendingSkyCell rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    if (psArrayLength(output)) {
-        // negative simple so the default is true
-        if (!ippdbPrintMetadatas(stdout, output, "p4PendingSkyCell", !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(output);
-            return false;
-        }
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool addwarpedMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4_id");
-        return false;
-    }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
-        return false;
-    }
-
-    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
-        return false;
-    }
-    if (!skycell_id) {
-        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
-        return false;
-    }
-
-    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
-        return false;
-    }
-    if (!tess_id) {
-        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
-        return 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 uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    // defaults to 0
-    psS32 p3_version = psMetadataLookupS32(&status, config->args, "-p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p3_version");
-        return false;
-    }
-
-    // optional
-    psF64 bg = psMetadataLookupF64(&status, config->args, "-bg");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg");
-        return false;
-    }
-
-    psF64 bg_mean_stdev = psMetadataLookupF64(&status, config->args, "-bg_mean_stdev");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_mean_stdev");
-        return false;
-    }
-
-
-    // XXX need to validate that this coresponds to an p4InputImfile
-    if (!p4ScfileInsert(config->dbh, 
-            (psS32)atoi(p4_id),
-            skycell_id,
-            tess_id,
-            exp_tag,
-            p3_version,
-            uri,
-            bg,
-            bg_mean_stdev
-        )) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
-
-static bool warpedMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    // find all rawImfiles matching the default query
-    psString query = psStringCopy( 
-        "SELECT\n"
-        "   p4Scfile.*\n"
-        " FROM p4Run\n"
-        " JOIN p4Scfile\n"
-        "   USING(p4_id)\n"
-        " WHERE\n"
-        "   p4Run.state = 'run'\n" 
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4Scfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psErrorCode err = psErrorCodeLast();
-        switch (err) {
-            case PS_ERR_DB_CLIENT:
-                psError(PXTOOLS_ERR_SYS, false, "database error");
-            case PS_ERR_DB_SERVER:
-                psError(PXTOOLS_ERR_PROG, false, "database error");
-            default:
-                psError(PXTOOLS_ERR_PROG, false, "unknown error");
-        }
-
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4Scfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    if (psArrayLength(output)) {
-        // negative simple so the default is true
         if (!ippdbPrintMetadatas(stdout, output, "p4Scfile", !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print array");
@@ -1030,412 +565,8 @@
 }
 
-#if 0
-static bool tostackedimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "   p4WarpedImfile.*,"
-        "   p4Run.workdir"
-        " FROM p4Run"
-        " JOIN p4WarpedImfile"
-        "   USING(p4_id)"
-        " LEFT JOIN p4StackedImfile"
-        "   USING(p4_id, class_id)"
-        " WHERE"
-        "   p4Run.state = 'run'"
-        "   AND p4StackedImfile.p4_id IS NULL"
-        "   AND p4Stackedmfile.class_id IS NULL"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4WarpedImfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4WarpedImfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p4WarpedImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-static bool addstackedimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    bool status = false;
-    psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4_id");
-        return false;
-    }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
-        return false;
-    }
-
-    psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
-        return false;
-    }
-    if (!class_id) {
-        psError(PS_ERR_UNKNOWN, true, "-class_id is required");
-        return false;
-    }
-
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    // optional
-    psString b1_uri = psMetadataLookupStr(&status, config->args, "-b1_uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -b1_uri");
-        return false;
-    }
-
-    psString b2_uri = psMetadataLookupStr(&status, config->args, "-b2_uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -b2_uri");
-        return false;
-    }
-
-    // XXX need to validate that this coresponds to an p4WarpedImfile
-    if (!p4StackedImfileInsert(config->dbh, 
-            (psS32)atoi(p4_id),
-            class_id,
-            uri,
-            b1_uri,
-            b2_uri
-        )) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
-
-static bool stackedimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "   p4StackedImfile.*"
-        " FROM p4Run"
-        " JOIN p4StackedImfile"
-        "   USING(p4_id)"
-        " WHERE"
-        "   p4Run.state = 'run'"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4StackedImfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4StackedImfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p4StackedImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-static bool todiffimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "   p4StackedImfile.*,"
-        "   p4WarpedImfile.uri as warped_uri,"
-        "   p4Run.workdir"
-        " FROM p4Run"
-        " JOIN p4StackedImfile"
-        "   USING(p4_id)"
-        " JOIN p4WarpedImfile"
-        "   USING(p4_id, class_id)"
-        " LEFT JOIN p4DiffImfile"
-        "   ON p4WarpedImfile.p4_id = p4DiffImfile.p4_id"
-        "   AND p4WarpedImfile.exp_tag = p4DiffImfile.exp_tag"
-        "   AND p4WarpedImfile.p3_version = p4DiffImfile.p3_version"
-        "   AND p4WarpedImfile.class_id = p4DiffImfile.class_id"
-        " WHERE"
-        "   p4Run.state = 'run'"
-        "   AND p4DiffImfile.p4_id IS NULL"
-        "   AND p4DiffImfile.exp_tag IS NULL"
-        "   AND p4DiffImfile.p3_version IS NULL"
-        "   AND p4DiffImfile.class_id IS NULL"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4StackedImfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4StackedImfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p4StackedImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-static bool adddiffimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    bool status = false;
-    psString p4_id = psMetadataLookupStr(&status, config->args, "-p4_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p4_id");
-        return false;
-    }
-    if (!p4_id) {
-        psError(PS_ERR_UNKNOWN, true, "-p4_id is required");
-        return 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 class_id = psMetadataLookupStr(&status, config->args, "-class_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
-        return false;
-    }
-    if (!class_id) {
-        psError(PS_ERR_UNKNOWN, true, "-class_id is required");
-        return false;
-    }
-
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    // defaults to 0
-    psS32 p3_version = psMetadataLookupS32(&status, config->args, "-p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -p3_version");
-        return false;
-    }
-
-    // optional
-    psString b1_uri = psMetadataLookupStr(&status, config->args, "-b1_uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -b1_uri");
-        return false;
-    }
-
-    psString b2_uri = psMetadataLookupStr(&status, config->args, "-b2_uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -b2_uri");
-        return false;
-    }
-
-    // XXX need to validate that this coresponds to an p4InputImfile
-    if (!p4DiffImfileInsert(config->dbh, 
-            (psS32)atoi(p4_id),
-            exp_tag,
-            p3_version,
-            class_id,
-            uri,
-            b1_uri,
-            b2_uri
-        )) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
-
-static bool diffimfileMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    psString query = psStringCopy(
-        " SELECT DISTINCT"
-        "   p4DiffImfile.*"
-        " FROM p4Run"
-        " JOIN p4DiffImfile"
-        "   USING(p4_id)"
-        " WHERE"
-        "   p4Run.state = 'run'"
-    );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "p4DiffImfile");
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no p4DiffImfile rows found");
-        psFree(output);
-        return true;
-    }
-
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p4DiffImfile", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-#endif
-
-static bool setp4RunState(pxConfig *config, const char *p4_id, const char *state)
-{
-    PS_ASSERT_PTR_NON_NULL(p4_id, false);
+
+static bool setp6RunState(pxConfig *config, const char *p6_id, const char *state)
+{
+    PS_ASSERT_PTR_NON_NULL(p6_id, false);
     PS_ASSERT_PTR_NON_NULL(state, false);
 
@@ -1453,31 +584,10 @@
 
     char *query = "UPDATE p4Run SET state = '%s' WHERE p4_id = '%s'";
-    if (!p_psDBRunQuery(config->dbh, query, state, p4_id)) {
+    if (!p_psDBRunQuery(config->dbh, query, state, p6_id)) {
         psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for p4_id %s", p4_id);
-        return false;
-    }
-
-    return true;
-}
-
-static bool isValidMode(pxConfig *config, const char *mode)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-    PS_ASSERT_PTR_NON_NULL(mode, false);
-
-    // check that state is a valid string value
-    if (!(
-            (strncmp(mode, "warp", 5) == 0)
-            || (strncmp(mode, "diff", 5) == 0)
-            || (strncmp(mode, "stack", 6) == 0)
-            || (strncmp(mode, "magic", 6) == 0)
-        )
-    ) {
-        psError(PS_ERR_UNKNOWN, false,
-                "invalid detRun mode: %s", mode);
-        return false;
-    }
-
-    return true;
-}
+                "failed to change state for p6_id %s", p6_id);
+        return false;
+    }
+
+    return true;
+}
