Index: /trunk/ippScripts/Build.PL
===================================================================
--- /trunk/ippScripts/Build.PL	(revision 30372)
+++ /trunk/ippScripts/Build.PL	(revision 30373)
@@ -58,4 +58,5 @@
         scripts/magic_tree.pl
         scripts/magic_process.pl
+        scripts/magic_cleanup.pl
         scripts/magic_destreak.pl
         scripts/magic_destreak_revert.pl
Index: /trunk/ippScripts/scripts/magic_cleanup.pl
===================================================================
--- /trunk/ippScripts/scripts/magic_cleanup.pl	(revision 30372)
+++ /trunk/ippScripts/scripts/magic_cleanup.pl	(revision 30373)
@@ -37,10 +37,10 @@
 
 # Parse the command-line arguments
-my ($magic_id, $exp_id, $workdir, $camera);
+my ($magic_id, $exp_id, $outroot, $camera);
 my ($dbname, $verbose, $no_update, $no_op, $logfile);
 
 GetOptions(
            'magic_id=s'     => \$magic_id,   # Magic run identifier
-           'workdir=s'      => \$workdir,    # directory to clean up
+           'outroot=s'      => \$outroot,    # directory to clean up
            'camera=s'       => \$camera,    # directory to clean up
            'logfile=s'      => \$logfile,
@@ -54,18 +54,21 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --magic_id --workdir --camera",
+pod2usage( -msg => "Required options: --magic_id --exp_id --outroot --camera",
            -exitval => 3) unless
     defined $magic_id and
-    defined $workdir and 
+    defined $exp_id and
+    defined $outroot and 
     defined $camera;
 
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $magic_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
+$ipprc->outroot_prepare($outroot);
 $ipprc->redirect_output($logfile) or my_die( "Unable to redirect output", $magic_id, $PS_EXIT_SYS_ERROR ) if $logfile;
 
-if (-e $workdir) {
+my $parentdir = dirname($outroot);
+if (-e $parentdir ) {
     my @extensions = ('.list', '.streakMap', '.streaks', '.clusters', '_hough.fits');
     foreach my $ext (@extensions) {
         my $n = 0;
-        my $path = $workdir . '/*' . $ext;
+        my $path = $outroot . '*' . $ext;
         my @files = glob($path);
         foreach my $file (@files) {
@@ -74,5 +77,5 @@
                 unless ($no_op) {
                     # remove this print
-                    print "unlinking $file\n";
+                    # print "unlinking $file\n";
                     unlink $file or my_die ("unable to delete $file", $magic_id, $PS_EXIT_SYS_ERROR);
                 } else {
@@ -84,5 +87,5 @@
     }
     # remove the verify directory
-    my $verify_dir = $workdir . "/$exp_id.mgc.$magic_id.verify";
+    my $verify_dir = $parentdir . "/$exp_id.mgc.$magic_id.verify";
     if (-e $verify_dir ) {
         my $rc = system "rm -r $verify_dir";
@@ -91,31 +94,27 @@
     }
 } else {
-    # workdir doesn't exist.
-    # If it's parent directory does exist, then assume workdir is gone so it is "cleaned'
-    my $parentdir = dirname($workdir);
-    if (! -e $parentdir ) {
-        # parent directory doesn't exist either, check that mountpoint exists
-        # we expect at least /data/volume
-        my @dirs = split "/", $parentdir;
-        # so the following should be true
-        # $dirs[0] eq ""
-        # $dirs[1] eq 'data'
-        # $dirs[2] eq the volume: for example ipp053.0
-        if ((scalar(@dirs) < 3) or ($dirs[0] ne "") or ($dirs[1] ne 'data')) {
-            my_die("unexpected parent of workdir found: '$parentdir'", $magic_id, $PS_EXIT_UNKNOWN_ERROR);
-        } else { 
-            # re-form mountpoint
-            my $mountpoint = "/$dirs[1]/$dirs[2]";
-            # check if it is readable
-            if (! -e $mountpoint or ! -r $mountpoint ) {
-                my_die("mount point for workdir not found or not readable: '$mountpoint'", $magic_id, $PS_EXIT_SYS_ERROR);
-            }
+    # parent directory doesn't "exist" either, check that mountpoint exists
+    # if it doesn't assume nfs error and fault
+    # we expect at least /data/volume
+    my @dirs = split "/", $parentdir;
+    # so the following should be true
+    # $dirs[0] eq ""
+    # $dirs[1] eq 'data'
+    # $dirs[2] eq the volume: for example ipp053.0
+    if ((scalar(@dirs) < 3) or ($dirs[0] ne "") or ($dirs[1] ne 'data')) {
+        my_die("unexpected parent of outroot found: '$parentdir'", $magic_id, $PS_EXIT_UNKNOWN_ERROR);
+    } else { 
+        # re-form mountpoint
+        my $mountpoint = "/$dirs[1]/$dirs[2]";
+        # check if it is readable
+        if (! -e $mountpoint or ! -r $mountpoint ) {
+            my_die("mount point for outroot not found or not readable: '$mountpoint'", $magic_id, $PS_EXIT_SYS_ERROR);
         }
-        print "Workdir $workdir not found. Setting magicRun.workdir_state to cleaned.\n";
-        # fall through to update workdir_state
     }
+    print "Workdir $outroot not found. Setting magicRun.workdir_state to cleaned.\n";
+    # fall through to update workdir_state
 }
             
-my $command = "$magictool -updaterun -magic_id $magic_id -set_workdir_state cleaned";
+my $command = "$magictool -setworkdirstate -magic_id $magic_id -set_workdir_state cleaned";
 $command   .= " -dbname $dbname" if defined $dbname;
 unless ($no_update) {
@@ -138,5 +137,5 @@
     $exit_code = $PS_EXIT_PROG_ERROR unless $exit_code;
 
-    my $command = "$magictool -updaterun -set_workdir_state error_cleaned";
+    my $command = "$magictool -setworkdirstate -set_workdir_state error_cleaned";
     $command   .= " -magic_id $magic_id";
     $command   .= " -dbname $dbname" if defined $dbname;
Index: /trunk/ippTasks/magic.cleanup.pro
===================================================================
--- /trunk/ippTasks/magic.cleanup.pro	(revision 30373)
+++ /trunk/ippTasks/magic.cleanup.pro	(revision 30373)
@@ -0,0 +1,220 @@
+## magic.cleanup.pro : support for the cleaning magic temporary files : -*- sh -*-
+
+# test for required global variables
+check.globals
+
+$LOGSUBDIR = $LOGDIR/magic
+mkdir $LOGSUBDIR
+
+### Initialise the books containing the tasks to do
+book init magicToCleanup
+
+### Database lists
+$magicToCleanup_DB = 0
+
+### Check status of tasks
+macro magic.cleanup.status
+  book listbook magicToCleanup
+end
+
+### Reset tasks
+macro magic.cleanup.reset
+  book init magicToCleanup
+end
+
+### Turn tasks on
+macro magic.cleanup.revert.on
+  task magic.cleanup.revert
+    active true
+  end
+end
+
+macro magic.cleanup.on
+  task magic.cleanup.load
+    active true
+  end
+  task magic.cleanup.run
+    active true
+  end
+end
+
+macro magic.cleanup.revert.off
+  task magic.cleanup.revert
+    active false
+  end
+end
+
+### Turn tasks off
+macro magic.cleanup.off
+  task magic.cleanup.load
+    active false
+  end
+  task magic.cleanup.run
+    active false
+  end
+end
+
+task	       magic.cleanup.load
+  host         local
+
+  periods      -poll $LOADPOLL
+  periods      -exec $LOADEXEC
+  periods      -timeout 30
+  npending     1
+
+  stdout NULL
+  stderr $LOGDIR/magic.cleanup.load.log
+
+  task.exec
+    $run = magictool -tocleanup
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$magicToCleanup_DB
+      $run = $run -dbname $DB:$magicToCleanup_DB
+      $magicToCleanup_DB ++
+      if ($magicToCleanup_DB >= $DB:n) set magicToCleanup_DB = 0
+    end
+    add_poll_args run
+    # XXX: add this back in once our inital cleanup is done
+#    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+    # convert 'stdout' to book format
+    ipptool2book stdout magicToCleanup -key magic_id -uniq -setword dbname $options:0 -setword pantaskState INIT
+    if ($VERBOSE > 2)
+      book listbook magicToCleanup
+    end
+
+    # delete existing entries in the appropriate pantaskStates
+    process_cleanup magicToCleanup
+  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.cleanup.run
+  periods      -poll $RUNPOLL
+  periods      -exec $RUNEXEC
+  periods      -timeout 300
+
+  task.exec
+    book npages magicToCleanup -var N
+    if ($N == 0) 
+        periods -exec $RUNEXEC
+        break
+    end
+    if ($NETWORK == 0) break
+    
+    book getpage magicToCleanup 0 -var pageName -key pantaskState INIT
+    if ("$pageName" == "NULL") break
+
+    book setword magicToCleanup $pageName pantaskState RUN
+    book getword magicToCleanup $pageName magic_id -var MAGIC_ID
+    book getword magicToCleanup $pageName exp_id -var EXP_ID
+    book getword magicToCleanup $pageName camera -var CAMERA
+    book getword magicToCleanup $pageName workdir -var WORKDIR
+    book getword magicToCleanup $pageName dbname -var DBNAME
+
+    host anyhost
+
+    sprintf outroot "%s/%s/%s.mgc.%s" $WORKDIR $EXP_ID $EXP_ID $MAGIC_ID
+
+    sprintf logfile "%s.cleanup.log" $outroot
+
+    stdout $logfile
+    stderr $logfile
+
+    $run = magic_cleanup.pl --magic_id $MAGIC_ID --exp_id $EXP_ID --camera $CAMERA --outroot $outroot --logfile $logfile
+    add_standard_args run
+
+    # save the pageName for future reference below
+    options $pageName
+
+    # create the command line
+    if ($VERBOSE > 1)
+      echo command $run
+    end
+    periods -exec 0.05
+    command $run
+  end
+
+  # default exit status
+  task.exit    default
+    process_exit magicToCleanup $options:0 $JOB_STATUS
+  end
+
+  task.exit    crash
+    showcommand crash
+    book setword magicToCleanup $options:0 pantaskState CRASH
+  end
+
+  # operation timed out?
+  task.exit    timeout
+    showcommand timeout
+    book setword magicToCleanup $options:0 pantaskState TIMEOUT
+  end
+end
+
+task magic.cleanup.revert
+  host         local
+
+  periods      -poll 60.0
+  periods      -exec 1800.0
+  periods      -timeout 120.0
+  npending     1
+  active false
+
+  stdout NULL
+  stderr $LOGSUBDIR/revertnode.log
+
+  task.exec
+    if ($LABEL:n == 0) break
+    $run = magictool -revertnode
+    if ($DB:n == 0)
+      option DEFAULT
+    else
+      # save the DB name for the exit tasks
+      option $DB:$magicRevertNode_DB
+      $run = $run -dbname $DB:$magicRevertNode_DB
+      $magicRevertNode_DB ++
+      if ($magicRevertNode_DB >= $DB:n) set magicRevertNode_DB = 0
+    end
+    add_poll_labels run
+    command $run
+  end
+
+  # success
+  task.exit    0
+  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
Index: /trunk/ippTools/share/Makefile.am
===================================================================
--- /trunk/ippTools/share/Makefile.am	(revision 30372)
+++ /trunk/ippTools/share/Makefile.am	(revision 30373)
@@ -246,4 +246,6 @@
 	magictool_exposure.sql \
 	magictool_setfull.sql \
+	magictool_setgotocleaned.sql \
+	magictool_tocleanup.sql \
 	magicdstool_clearstatefaults.sql \
 	magicdstool_change_file_data_state.sql \
Index: /trunk/ippTools/share/magictool_setgotocleaned.sql
===================================================================
--- /trunk/ippTools/share/magictool_setgotocleaned.sql	(revision 30373)
+++ /trunk/ippTools/share/magictool_setgotocleaned.sql	(revision 30373)
@@ -0,0 +1,4 @@
+UPDATE magicRun 
+    SET workdir_state = 'goto_cleaned'
+WHERE workdir_state = 'dirty'
+
Index: /trunk/ippTools/share/magictool_tocleanup.sql
===================================================================
--- /trunk/ippTools/share/magictool_tocleanup.sql	(revision 30373)
+++ /trunk/ippTools/share/magictool_tocleanup.sql	(revision 30373)
@@ -0,0 +1,10 @@
+SELECT magic_id,
+    magicRun.exp_id,
+    rawExp.camera,
+    magicRun.workdir,
+    IFNULL(priority, 10000) as priority
+FROM magicRun 
+    JOIN rawExp USING(exp_id)
+    LEFT JOIN Label ON magicRun.label = Label.label
+WHERE magicRun.workdir_state = 'goto_cleaned'
+    AND (Label.active OR Label.active IS NULL)
