Index: trunk/ippTools/src/magictool.c
===================================================================
--- trunk/ippTools/src/magictool.c	(revision 18561)
+++ trunk/ippTools/src/magictool.c	(revision 18571)
@@ -111,46 +111,46 @@
     // create warped skycells temp table
     {
-	psString query = pxDataGet("magictool_create_tmp_warpcomplete.sql");
-	if (!query) {
-	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-	    return false;
-	}
-
-	if (!p_psDBRunQuery(config->dbh, query)) {
-	    psError(PS_ERR_UNKNOWN, false, "database error");
-	    return false;
-	}
+        psString query = pxDataGet("magictool_create_tmp_warpcomplete.sql");
+        if (!query) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            return false;
+        }
+
+        if (!p_psDBRunQuery(config->dbh, query)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
     }
 
     // find warped skycells
     {
-	psString query = pxDataGet("magictool_find_complete_warpruns.sql");
-	if (!query) {
-	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-	    return false;
-	}
-
-	if (!p_psDBRunQuery(config->dbh, query)) {
-	    psError(PS_ERR_UNKNOWN, false, "database error");
-	    psFree(query);
-	    return false;
-	}
-	psFree(query);
+        psString query = pxDataGet("magictool_find_complete_warpruns.sql");
+        if (!query) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            return false;
+        }
+
+        if (!p_psDBRunQuery(config->dbh, query)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(query);
+            return false;
+        }
+        psFree(query);
     }
 
     // find the diff_id's of the warped skycells
     {
-	psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
-	if (!query) {
-	    psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-	    return false;
-	}
-
-	if (!p_psDBRunQuery(config->dbh, query)) {
-	    psError(PS_ERR_UNKNOWN, false, "database error");
-	    psFree(query);
-	    return false;
-	}
-	psFree(query);
+        psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
+        if (!query) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            return false;
+        }
+
+        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);
@@ -158,10 +158,10 @@
         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");
+          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");
         }
 
@@ -211,13 +211,13 @@
         // create a new magicRun for this group
         magicRunRow *run = magicRunRowAlloc(
-	    0,				// ID
-	    "reg",			// state
-	    workdir,			// workdir
-	    "dirty",			// workdir_state
-	    label,			// label
-	    dvodb,			// dvodb
-	    registered,			// registered
-	    0				// fault
-	    );
+            0,                          // ID
+            "reg",                      // state
+            workdir,                    // workdir
+            "dirty",                    // workdir_state
+            label,                      // label
+            dvodb,                      // dvodb
+            registered,                 // registered
+            0                           // fault
+            );
         if (!run) {
             psAbort("failed to alloc magicRun object");
@@ -248,7 +248,7 @@
 
             if (!magicInputSkyfileInsert(config->dbh,
-					 magic_id,
-					 diff_id,
-					 node)) {
+                                         magic_id,
+                                         diff_id,
+                                         node)) {
                 psError(PS_ERR_UNKNOWN, false, "database error");
                 psFree(grouped);
@@ -320,10 +320,10 @@
 
     // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
 
     if (state) {
         // set detRun.state to state
-        return setmagicRunState(config, (psS64)atoll(magic_id), state);
+        return setmagicRunState(config, magic_id, state);
     }
 
@@ -337,12 +337,12 @@
 
     // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
+    PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_S64(diff_id, config->args, "-diff_id", true, false);
     PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
 
     magicInputSkyfileInsert(
             config->dbh,
-            (psS64)atoll(magic_id),
-            (psS64)atoll(diff_id),
+            magic_id,
+            diff_id,
             node
     );
@@ -357,6 +357,6 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-diff_id", "diff_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-diff_id", "diff_id", "==");
     PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
 
@@ -431,5 +431,5 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magicRun.magic_id", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -503,8 +503,23 @@
 
     // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
-    PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", true, false);
-
-    if (!parseAndInsertNodeDeps(config, (psS64)atoll(magic_id), dep_file)) {
+    PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
+
+    // Optional values
+    PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", false, false);
+
+    // default values
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
+
+    if (code > 0) {
+        char *query = "UPDATE magicRun SET fault = %d WHERE magic_id = %" PRId64;
+        if (!p_psDBRunQuery(config->dbh, query, code, magic_id)) {
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to set fault for magic_id %" PRId64, magic_id);
+            return false;
+        }
+        return true;
+    }
+
+    if (!parseAndInsertNodeDeps(config, magic_id, dep_file)) {
         psError(PS_ERR_UNKNOWN, false, "failed to parse file");
         return false;
@@ -519,5 +534,5 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
     PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
 
@@ -663,5 +678,5 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
 
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -678,5 +693,5 @@
         whereClause = psDBGenerateWhereSQL(where, NULL);
         psStringAppend(&query, " %s", whereClause);
-	psFree(whereClause);
+        psFree(whereClause);
     }
     psFree(where);
@@ -705,5 +720,5 @@
     if (!psArrayLength(output)) {
         psTrace("magictool", PS_LOG_INFO, "no rows found");
-	// psFree(output);
+        // psFree(output);
         // return true;
     }
@@ -798,5 +813,5 @@
 
     // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
     PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
 
@@ -808,5 +823,5 @@
 
     if (!magicNodeResultInsert(config->dbh,
-                               (psS64)atoll(magic_id),
+                               magic_id,
                                node,
                                uri,
@@ -888,5 +903,5 @@
 
     // required
-    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
 
     // optional
@@ -903,5 +918,5 @@
 
     if (!magicMaskInsert(config->dbh,
-                         (psS64)atoll(magic_id),
+                         magic_id,
                          uri,
                          streaks,
@@ -920,5 +935,5 @@
 
     // manually add constraint
-    psStringAppend(&query, " AND magic_id = %s", magic_id);
+    psStringAppend(&query, " AND magic_id = %" PRId64, magic_id);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -943,5 +958,5 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-magic_id", "magicRun.magic_id", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
