Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 21402)
+++ trunk/ippTools/src/magicdstool.c	(revision 21433)
@@ -43,22 +43,4 @@
 static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id);
 
-#ifdef notdef
-static bool toprocessMode(pxConfig *config);
-static bool addresultMode(pxConfig *config);
-static bool revertnodeMode(pxConfig *config);
-static bool inputsMode(pxConfig *config);
-static bool tomaskMode(pxConfig *config);
-static bool addmaskMode(pxConfig *config);
-static bool revertmaskMode(pxConfig *config);
-static bool maskMode(pxConfig *config);
-static bool diffskyfileMode(pxConfig *config);
-static bool warpskyfileMode(pxConfig *config);
-static bool chipprocessedimfileMode(pxConfig *config);
-static bool rawimfileMode(pxConfig *config);
-
-static bool setmagicRunState(pxConfig *config, psS64 magic_id, const char *state);
-static bool parseAndInsertNodeDeps(pxConfig *config, psS64 magic_id, const char *filename);
-#endif
-
 # define MODECASE(caseName, func) \
     case caseName: \
@@ -482,4 +464,66 @@
 
 
+static bool
+setMagicked(pxConfig *config, psS64 magic_ds_id, psString component)
+{
+    // first query the magicDSRun to find the stage and the stage_id
+    psString query = "SELECT stage, stage_id from magicDSRun where magic_ds_id = %" PRId64;
+
+    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("magicdstool", PS_LOG_INFO, "magic_ds_id: %" PRId64 " not found", magic_ds_id);
+        psFree(output);
+        return true;
+    }
+    if (psArrayLength(output) > 1) {
+        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found %ld for magic_ds_id %" PRId64,
+            psArrayLength(output), magic_ds_id);
+        psFree(output);
+        return false;
+    }
+    psMetadata *row = output->data[0];
+
+    psString stage = psMetadataLookupStr(NULL, row, "stage");
+    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
+
+
+    // chose the appropriate query based on the stage
+    if (!strcmp(stage, "raw")) {
+        query = "UPDATE rawImfile SET magicked = 1 where exp_id = %" PRId64 " AND class_id = '%s'";
+    } else if (!strcmp(stage, "chip")) {
+        query = "UPDATE chipProcessedImfile SET magicked = 1 where chip_id = %" PRId64 " AND class_id = '%s'";
+    } else if (!strcmp(stage, "warp")) {
+        query = "UPDATE warpSkyfile SET magicked = 1 where warp_id = %" PRId64 " AND skycell_id = '%s'";
+    } else if (!strcmp(stage, "diff")) {
+        query = "UPDATE diffSkyfile SET magicked = 1 where diff_id = %" PRId64 " AND skycell_id = '%s'";
+    } else {
+        psError(PS_ERR_UNKNOWN, true, "unexpected value for stage: %s found", stage);
+        psFree(output);
+        return false;
+    }
+    if (!p_psDBRunQueryF(config->dbh, query, 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 true;
+}
+
 static bool adddestreakedfileMode(pxConfig *config)
 {
@@ -494,4 +538,10 @@
     PXOPT_LOOKUP_STR(backup_path_base, config->args, "-backup_path_base", false, false);
     PXOPT_LOOKUP_STR(recovery_path_base, config->args, "-recovery_path_base", false, false);
+    PXOPT_LOOKUP_BOOL(setmagicked, config->args, "-setmagicked", false);
+
+    if (setmagicked && (code != 0)) {
+        psError(PS_ERR_UNKNOWN, true, " cannot setmagicked for faulted file");
+        return false;
+    }
 
     if (!psDBTransaction(config->dbh)) {
@@ -500,4 +550,10 @@
     }
 
+    if (setmagicked) {
+        if (!setMagicked(config, magic_ds_id, component)) {
+            psError(PS_ERR_UNKNOWN, false, "setMagicked failed");
+            return false;
+        }
+    }
 
     if (!magicDSFileInsert(config->dbh, magic_ds_id, component, backup_path_base, recovery_path_base, code)) {
