Index: trunk/ippTasks/magic.pro
===================================================================
--- trunk/ippTasks/magic.pro	(revision 24675)
+++ trunk/ippTasks/magic.pro	(revision 24685)
@@ -11,4 +11,5 @@
 book init magicToProcess
 book init magicToDS
+book init magicDSToRevert
 
 ### Database lists
@@ -16,4 +17,14 @@
 $magicToProcess_DB = 0
 $magicToDS_DB = 0
+$magicDSToRevert_DB = 0
+
+#list of stages
+$STAGE:n = 0
+list STAGE -add "raw"
+list STAGE -add "chip"
+#list STAGE -add "camera"
+list STAGE -add "warp"
+list STAGE -add "diff"
+$magicDSRevertStage = 0
 
 ### Check status of tasks
@@ -22,4 +33,5 @@
   book listbook magicToProcess
   book listbook magicToDS
+  book listbook magicDSToRevert
 end
 
@@ -29,4 +41,5 @@
   book init magicToProcess
   book init magicToDS
+  book init magicDSToRevert
 end
 
@@ -53,4 +66,12 @@
 end
 
+macro magic.ds.revert.on
+    task magic.ds.revert.load
+        active true
+    end
+    task magic.ds.revert.run
+        active false
+    end
+end
 ### Turn tasks off
 macro magic.off
@@ -74,4 +95,14 @@
   end
 end
+
+macro magic.ds.revert.off
+    task magic.ds.revert.load
+        active false
+    end
+    task magic.ds.revert.run
+        active false
+    end
+end
+
 
 task	       magic.tree.load
@@ -95,5 +126,5 @@
       $run = $run -dbname $DB:$magicToTree_DB
       $magicToTree_DB ++
-      if ($magicToTree_DB >= $DB:n) set magicToTree_DB = 0
+      if ($magicToTree_DB >= $DB:n) $magicToTree_DB = 0
     end
     add_poll_args run
@@ -477,2 +508,144 @@
 end
 
+task	       magic.ds.revert.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 20
+  npending     1
+  active       false
+
+  stdout NULL
+  stderr $LOGDIR/magic.ds.revert.log
+
+  task.exec
+    $run = magicdstool -torevert -stage $STAGE:$magicDSRevertStage
+    $magicDSRevertStage ++
+    if ($magicDSRevertStage >= $STAGE:n) set magicDSRevertStage = 0
+
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+
+      # save the DB name for the exit tasks
+      option $DB:$magicDSToRevert_DB
+      $run = $run -dbname $DB:$magicDSToRevert_DB
+
+      # only bump database number after we have gone through all of the stages
+      if ($magicDSRevertStage == 0)
+             $magicDSToRevert_DB ++
+      end
+      if ($magicDSToRevert_DB >= $DB:n) set magicDSToRevert_DB = 0
+    end
+    add_poll_args run
+    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+    # convert 'stdout' to book format
+    ipptool2book stdout magicDSToRevert -key magic_ds_id:component -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook magicDSToRevert
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup magicDSToRevert
+  end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+  end
+
+  task.exit    crash
+    showcommand crash
+  end
+
+  # operation times out?
+  task.exit    timeout
+    showcommand timeout
+  end
+end
+
+task	       magic.ds.revert.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 60
+  active       false
+
+  task.exec
+    book npages magicDSToRevert -var N
+    if ($N == 0) break
+    if ($NETWORK == 0) break
+    
+    # look for new images (pantaskState == INIT)
+    book getpage magicDSToRevert 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword magicDSToRevert $pageName pantaskState RUN
+    book getword magicDSToRevert $pageName exp_id -var EXP_ID
+    book getword magicDSToRevert $pageName magic_ds_id -var MAGIC_DS_ID
+    book getword magicDSToRevert $pageName camera -var CAMERA
+    book getword magicDSToRevert $pageName streaks_uri -var STREAKS
+    book getword magicDSToRevert $pageName stage -var STAGE
+    book getword magicDSToRevert $pageName stage_id -var STAGE_ID
+    book getword magicDSToRevert $pageName component -var COMPONENT
+    book getword magicDSToRevert $pageName uri -var URI
+    book getword magicDSToRevert $pageName path_base -var PATH_BASE
+    book getword magicDSToRevert $pageName inverse -var INVERSE
+    book getword magicDSToRevert $pageName cam_path_base -var CAM_PATH_BASE
+    book getword magicDSToRevert $pageName outroot -var OUTROOT
+    book getword magicDSToRevert $pageName recoveryroot -var RECROOT
+    book getword magicDSToRevert $pageName re_place -var REPLACE
+    book getword magicDSToRevert $pageName dbname -var DBNAME
+
+    sprintf logfile "%s/%s.mds.%s.%s.%s.log" $OUTROOT $EXP_ID $MAGIC_DS_ID $STAGE_ID $COMPONENT
+
+    host anyhost
+
+    # TODO: do not add recoveryroot or replace if they are null or zero
+
+    $run = magic_destreak_revert.pl --magic_ds_id $MAGIC_DS_ID --camera $CAMERA --streaks $STREAKS --stage $STAGE --stage_id $STAGE_ID --component $COMPONENT --uri $URI --path_base $PATH_BASE --cam_path_base $CAM_PATH_BASE --outroot $OUTROOT --logfile $logfile --recoveryroot $RECROOT --replace $REPLACE
+    if ("$INVERSE" == "T")
+       # Inverse subtraction
+       $run = $run --inverse
+    end
+
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    command $run
+  end
+
+  # default exit status
+  task.exit    0
+    process_exit magicDSToRevert $options:0 $JOB_STATUS
+   end
+
+  # locked list
+  task.exit    default
+    showcommand failure
+    process_exit magicDSToRevert $options:0 $JOB_STATUS
+  end
+
+  task.exit    crash
+    showcommand crash
+    book setword magicDSToRevert $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword magicDSToRevert $options:0 pantaskState TIMEOUT
+  end
+end
+
