Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 24552)
+++ trunk/ippTools/src/magicdstool.c	(revision 24683)
@@ -40,4 +40,5 @@
 static bool toremoveMode(pxConfig *config);
 static bool torestoreMode(pxConfig *config);
+static bool torevertMode(pxConfig *config);
 
 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
@@ -72,4 +73,5 @@
         MODECASE(MAGICDSTOOL_MODE_TOREMOVE,            toremoveMode);
         MODECASE(MAGICDSTOOL_MODE_TORESTORE,           torestoreMode);
+        MODECASE(MAGICDSTOOL_MODE_TOREVERT,            torevertMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -502,4 +504,9 @@
     psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
 
+    if (!strcmp(stage, "camera")) {
+        // no there is no magicked column in camProcessedExp
+        psFree(output);
+        return true;
+    }
 
     // chose the appropriate query based on the stage
@@ -570,4 +577,6 @@
     } else if (!strcmp(stage, "chip")) {
         query = "UPDATE chipRun SET magicked = %" PRId64 " where chip_id = %" PRId64;
+    } else if (!strcmp(stage, "camera")) {
+        query = "UPDATE camRun SET magicked = %" PRId64 " where cam_id = %" PRId64;
     } else if (!strcmp(stage, "warp")) {
         query = "UPDATE warpRun SET magicked = %" PRId64 " where warp_id = %" PRId64;
@@ -674,6 +683,8 @@
     } else if (!strcmp(stage, "chip")) {
         stageNum = 1;
+    } else if (!strcmp(stage, "camera")) {
+        stageNum = 2;
     } else if (!strcmp(stage, "warp")) {
-        stageNum = 2;
+        stageNum = 3;
     } else {
         psError(PXTOOLS_ERR_DATA, true, "%s is not a valid value for stage", stage);
@@ -718,4 +729,6 @@
         *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
     } else if (stageNum == 2) {
+        *stage_id = *cam_id;
+    } else if (stageNum == 3) {
         *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
     }
@@ -1039,2 +1052,85 @@
     return true;
 }
+
+
+static bool torevertMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
+
+    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
+    pxAddLabelSearchArgs (config, where, "-label", "magicDSRun.label", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString sql_file = NULL;
+    psStringAppend(&sql_file, "magicdstool_torevert_%s.sql", stage);
+
+    psString query = pxDataGet(sql_file);
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file);
+        psFree(sql_file);
+        return false;
+    }
+    psFree(sql_file);
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    // 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)) {
+        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
+    if (psArrayLength(output)) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "torevert", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            return false;
+        }
+    }
+
+    psFree(output);
+
+    return true;
+}
+
+
