Index: branches/eam_branches/ipp-20110213/ippTools/share/disttool_pending_raw.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/disttool_pending_raw.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/disttool_pending_raw.sql	(revision 30675)
@@ -12,5 +12,6 @@
     TRIM(TRAILING '.fits' FROM rawImfile.uri) AS path_base,
     CAST(NULL AS CHAR(255)) AS alt_path_base,
-    chipProcessedImfile.path_base as chip_path_base,
+    -- pass camera stage path base since we want the camera mask file. The script knows what to do
+    camProcessedExp.path_base as chip_path_base,
     CAST(NULL AS CHAR(255)) AS state,
     CAST(NULL AS CHAR(255)) AS data_state,
@@ -26,4 +27,6 @@
 JOIN chipProcessedImfile
     USING(chip_id, class_id)
+JOIN camRun USING(chip_id)
+JOIN camProcessedExp using(cam_id)
 LEFT JOIN distComponent 
     ON distRun.dist_id = distComponent.dist_id 
@@ -36,4 +39,7 @@
     AND distComponent.dist_id IS NULL
     AND (rawExp.magicked OR distRun.no_magic)
+    -- need to have magicked the chip image which makes the camera mask
+    AND chipProcessedImfile.magicked != 0
+    AND camRun.magicked > 0 
     AND (Label.active OR Label.active IS NULL)
 UNION
@@ -52,5 +58,6 @@
     TRIM(TRAILING '.fits' FROM rawImfile.uri) AS path_base,
     magicDSFile.backup_path_base AS alt_path_base,
-    chipProcessedImfile.path_base AS chip_path_base,
+    -- pass camera stage path base since we want the camera mask file. The script knows what to do
+    camProcessedExp.path_base AS chip_path_base,
     CAST('full' AS CHAR(255)) AS state,
     CAST('full' AS CHAR(255)) AS data_state,
@@ -77,6 +84,6 @@
     AND distRun.clean = 0
     AND distComponent.dist_id IS NULL
-    AND chipProcessedImfile.data_state = 'full'
-    AND chipProcessedImfile.magicked > 0
+    AND chipProcessedImfile.magicked != 0
+    AND camRun.magicked > 0 
     AND (Label.active OR Label.active IS NULL)
 UNION
Index: branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_camera.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_camera.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_camera.sql	(revision 30675)
@@ -11,4 +11,5 @@
     camProcessedExp.path_base,
     camProcessedExp.path_base AS cam_path_base,
+    recovery_path_base,
     0 AS bothways,
     0 AS bytes,
Index: branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_chip.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_chip.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_chip.sql	(revision 30675)
@@ -11,4 +11,5 @@
     chipProcessedImfile.path_base,
     camProcessedExp.path_base AS cam_path_base,
+    recovery_path_base,
     0 AS bothways,
     0 AS bytes,
Index: branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_diff.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_diff.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_diff.sql	(revision 30675)
@@ -13,4 +13,5 @@
     magicDSFile.recovery_path_base,
     "NULL" AS cam_path_base,
+    recovery_path_base,
     CAST(diffRun.bothways AS SIGNED) AS bothways,
     0 AS bytes,
Index: branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_raw.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_raw.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_raw.sql	(revision 30675)
@@ -11,4 +11,5 @@
     rawImfile.uri AS path_base,
     CAST(NULL AS CHAR(255)) AS cam_path_base, 
+    recovery_path_base,
     0 AS bothways,
     bytes,
Index: branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_warp.sql
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_warp.sql	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/share/magicdstool_torevert_warp.sql	(revision 30675)
@@ -11,4 +11,5 @@
     warpSkyfile.path_base,
     "NULL" AS cam_path_base,
+    recovery_path_base,
     0 AS bothways,
     0 AS bytes,
Index: branches/eam_branches/ipp-20110213/ippTools/src/magicdstool.c
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/src/magicdstool.c	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/src/magicdstool.c	(revision 30675)
@@ -709,6 +709,9 @@
 
 
+// update the magicked column for the underlying component
+// Note: Must be called inside a transaction
+
 static bool
-setMagicked(pxConfig *config, psS64 magic_ds_id, psString component)
+setMagicked(pxConfig *config, psS64 magic_ds_id, psString component, bool clearMagicked)
 {
     // first query the magicDSRun to find the stage and the stage_id
@@ -741,47 +744,64 @@
     psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
+    psFree(output);
+
+    psS64 newMagickedValue;
+    if (clearMagicked) {
+        newMagickedValue = 0;
+    } else {
+        newMagickedValue = magic_id;
+    }
 
     ippStage stageNum = ippStringToStage(stage);
 
     // chose the appropriate query based on the stage
+    char *clearRunQuery = NULL;
     switch (stageNum) {
     case IPP_STAGE_RAW:
         query = "UPDATE rawImfile SET magicked = %" PRId64 " where exp_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE rawExp SET magicked = 0 where exp_id = %" PRId64;
         break;
     case IPP_STAGE_CHIP:
         query = "UPDATE chipProcessedImfile SET magicked = %" PRId64 " where chip_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE chipRun set magicked = 0 where chip_id = %" PRId64;
         break;
     case IPP_STAGE_CHIP_BG:
         query = "UPDATE chipBackgroundImfile SET magicked = %" PRId64 " where chip_bg_id = %" PRId64 " AND class_id = '%s'";
+        clearRunQuery = "UPDATE chipBackgroundRun SET magicked = 0 where chip_bg_id = %" PRId64;
         break;
     case IPP_STAGE_CAMERA:
-        // no there is no magicked column in camProcessedExp so we have nothing to do
-        psFree(output);
-        return true;
+        psFree(query);
+        query = NULL;
+        clearRunQuery = "UPDATE chipBackgroundRun SET magicked = 0 where chip_bg_id = %" PRId64;
+        break;
     case IPP_STAGE_WARP:
         query = "UPDATE warpSkyfile SET magicked = %" PRId64 " where warp_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE warpRun SET magicked = 0 where warp_id = %" PRId64;
         break;
     case IPP_STAGE_WARP_BG:
         query = "UPDATE warpBackgroundSkyfile SET magicked = %" PRId64 " where warp_bg_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE warpBackgroundRun SET magicked = 0 where warp_bg_id = %" PRId64;
         break;
     case IPP_STAGE_DIFF:
         query = "UPDATE diffSkyfile SET magicked = %" PRId64 " where diff_id = %" PRId64 " AND skycell_id = '%s'";
+        clearRunQuery = "UPDATE diffRun SET magicked = 0 where diff_id = %" PRId64;
         break;
     default:
         psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
-        psFree(output);
-        return false;
-    }
-
-    if (!p_psDBRunQueryF(config->dbh, query, magic_id, stage_id, component)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    psFree(output);
-
-    psU64 affected = psDBAffectedRows(config->dbh);
-    if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected 1 row");
-        return false;
+        return false;
+    }
+
+    if (query) {
+        if (!p_psDBRunQueryF(config->dbh, query, newMagickedValue, stage_id, component)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+    }
+
+    if (clearMagicked && clearRunQuery) {
+        if (!p_psDBRunQueryF(config->dbh, clearRunQuery, stage_id)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
     }
 
@@ -896,5 +916,5 @@
     if (setmagicked) {
         // set the image file's magicked flag
-        if (!setMagicked(config, magic_ds_id, component)) {
+        if (!setMagicked(config, magic_ds_id, component, false)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
             if (!psDBRollback(config->dbh)) {
@@ -1111,4 +1131,6 @@
     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
 
+    PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", true, false);
+    PXOPT_LOOKUP_STR(component, config->args, "-component", true, false);
     PXOPT_LOOKUP_STR(state, config->args, "-state", false, false);
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
@@ -1116,6 +1138,7 @@
     psString queryFile = NULL;
     bool stateIsUpdate = false;
+    bool toRestored = !strcmp(state, "goto_restored");
     if (state) {
-        if (! strcmp(state, "new") || !strcmp(state, "goto_restored")) {
+        if (! strcmp(state, "new") || toRestored) {
             queryFile = "magicdstool_revertdestreakedfile.sql";
         } else if (!strcmp(state, "update")) {
@@ -1153,4 +1176,8 @@
     }
 
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "failed to revert");
@@ -1159,4 +1186,18 @@
     }
     psFree(query);
+    if (toRestored) {
+        // clear the underlying component's magicked value
+        if (!setMagicked(config, magic_ds_id, component, true)) {
+            psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+    }
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
     return true;
 }
@@ -1661,8 +1702,7 @@
     }
     if (!strcmp(data_state, "full")) {
-        // if -tofullfile optoinally set the magicked value for the component
-        PXOPT_LOOKUP_BOOL(setmagicked, config->args, "-setmagicked", false);
-        // set the image file's magicked flag
-        if (!setMagicked(config, magic_ds_id, component)) {
+        // set the component's magicked flag
+        
+        if (!setMagicked(config, magic_ds_id, component, false)) {
             psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
             if (!psDBRollback(config->dbh)) {
@@ -1716,14 +1756,4 @@
     return change_file_data_state(config, "full");
 }
-/*
-static bool topurgedimfileMode(pxConfig *config)
-{
-    return change_imfile_data_state(config, "purged", "goto_purged");
-}
-static bool toscrubbedfileMode(pxConfig *config)
-{
-  return change_file_data_state(config, "scrubbed", "goto_scrubbed");
-}
-*/
 
 // a very specfic function to queue a cleaned magicDSFile to be updated
Index: branches/eam_branches/ipp-20110213/ippTools/src/regtool.c
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/src/regtool.c	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/src/regtool.c	(revision 30675)
@@ -259,4 +259,13 @@
       continue;
     }
+    char *is_ccim = strchr(psMetadataLookupStr(NULL,row,"exp_name"),'c');
+    if (is_ccim) { // Is a camera commanded exposure
+      //      fprintf(stderr,"IN camera commanded!\n");
+      if ((strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"DOMEFLAT") == 0)||
+	  (strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"DARK") == 0) ||
+	  (strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"BIAS") == 0)) {
+	continue;
+      }
+    }
     
     if ((psMetadataLookupS32(NULL,row,"is_downloaded") != 1)||
@@ -367,4 +376,14 @@
     }
 
+    char *is_ccim = strchr(psMetadataLookupStr(NULL,row,"exp_name"),'c');
+    if (is_ccim) { // Is a camera commanded exposure
+      //      fprintf(stderr,"IN camera commanded!\n");
+      if ((strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"DOMEFLAT") == 0)||
+	  (strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"DARK") == 0) ||
+	  (strcasecmp(psMetadataLookupStr(NULL,row,"exp_type"),"BIAS") == 0)) {
+	continue;
+      }
+    }
+
     bool status = false;
     char *tmp_id = psMetadataLookupStr(&status,row,"summit_class_id");
@@ -389,4 +408,5 @@
     }
 
+    
     if (0 && !strcmp(this_class_id, "ota44")) {
         printf("STAT 1: %s (%d %d) %d %d %d\n",
@@ -857,5 +877,6 @@
     PXOPT_LOOKUP_STR(set_md5sum, config->args, "-set_md5sum", false, false);
     PXOPT_LOOKUP_STR(set_state, config->args, "-set_state", false, false);
-
+    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
+    
     if ((fault == INT16_MAX) && (burntool_state == INT16_MAX) && !set_state) {
         psError(PS_ERR_UNKNOWN, false, "one of -fault or -burntool_state or -set_state must be selected");
@@ -905,5 +926,10 @@
       psStringAppend(&setvalues,"rawImfile.data_state = '%s'",set_state);
     }
-
+    if (hostname) {
+      if (setvalues) {
+	psStringAppend(&setvalues,",");
+      }
+      psStringAppend(&setvalues,"rawImfile.hostname = '%s'", hostname);
+    }
     psString query = pxDataGet("regtool_updateprocessedimfile.sql");
     if (!query) {
Index: branches/eam_branches/ipp-20110213/ippTools/src/regtoolConfig.c
===================================================================
--- branches/eam_branches/ipp-20110213/ippTools/src/regtoolConfig.c	(revision 30628)
+++ branches/eam_branches/ipp-20110213/ippTools/src/regtoolConfig.c	(revision 30675)
@@ -189,4 +189,5 @@
     ADD_OPT(S16, updateprocessedimfileArgs, "-burntool_state",        "set burntool state", INT16_MAX);
     ADD_OPT(S16, updateprocessedimfileArgs, "-fault",          "set fault code", INT16_MAX);
+    ADD_OPT(Str, updateprocessedimfileArgs, "-hostname",       "set host name",                NULL);
     ADD_OPT(S32, updateprocessedimfileArgs, "-set_bytes",      "set bytes", INT32_MAX);
     ADD_OPT(Str, updateprocessedimfileArgs, "-set_md5sum",     "set md5sum", NULL);
