Index: trunk/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 25781)
+++ trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 25801)
@@ -187,4 +187,15 @@
                 &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
             }
+
+	    # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
+	    my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
+	    $command = "$magicdstool -dbname gpc1  -stage $stage -stage_id $stage_id -updaterun -set_state goto_purged";
+	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
+	    }
+
         } else {
 
@@ -209,6 +220,6 @@
 		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
 		&my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
-
-	    }
+	    }
+
         }
     }
@@ -295,4 +306,15 @@
             &my_die("Unable to perform camtool: $error_code", "camera", $stage_id, $error_code);
         }
+
+	# Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
+	my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
+	$command = "$magicdstool -dbname gpc1 -stage $stage -stage_id $stage_id -updaterun -set_state goto_purged";
+	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => $verbose);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
+	}
+
     } else {
 	# since 'camera' has only a single imfile, we can just update the run
@@ -308,4 +330,6 @@
         exit $PS_EXIT_UNKNOWN_ERROR;
     }
+
+
     exit 0;
 }
@@ -418,4 +442,13 @@
             }
 
+	    # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
+	    my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
+	    $command = "$magicdstool -dbname gpc1 -stage $stage -stage_id $stage_id -updaterun -set_state goto_purged";
+	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
+	    }
 
          } else {
@@ -438,7 +471,5 @@
 ### added for cleanup, based on warp stage entry
 if ($stage eq 'stack') {
-    
     die "--stage_id required for stage stack\n" if !$stage_id;
-
     # this stage uses 'stacktool'
     my $stacktool = can_run('stacktool') or die "Can't find stacktool";
@@ -457,5 +488,5 @@
 	# No skycells were found for some reason.
 	# Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
-	my $command = "$stacktool -updaterun -stack_id $stage_id -set_state $error_state";
+	my $command = "$stacktool -updaterun -stack_id $stage_id -state $error_state";
 	$command .= " -dbname $dbname" if defined $dbname;
 	
@@ -464,5 +495,5 @@
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
+	    &my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
 	}
 	
@@ -482,22 +513,29 @@
 
 	my $status = 1;
-	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
-
-	    unless ($ipprc->file_exists($config_file)) {
-		print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
-		    " because config file is missing\n";
-		$status = 0;
-	    }
-	}
-	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
-
-	    if ($ipprc->file_exists($config_file)) {
-		print STDERR "skipping scrubbed for stackRun $stage_id $skycell_id" .
-		    " because config file is present\n";
-		$status = 0;
-	    }
-	}
+	if ((!exists($skyfile->{path_base}))||
+	    (!defined($path_base))) {
+	    $status = 0;
+	}
+	if ($status) {
+	    if ($mode eq "goto_cleaned") {
+		my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
+		
+		unless ($ipprc->file_exists($config_file)) {
+		    print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
+			" because config file is missing\n";
+		    $status = 0;
+		}
+	    }
+	    elsif ($mode eq "goto_scrubbed") {
+		my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
+		
+		if ($ipprc->file_exists($config_file)) {
+		    print STDERR "skipping scrubbed for stackRun $stage_id $skycell_id" .
+			" because config file is present\n";
+		    $status = 0;
+		}
+	    }
+	}
+
 	if ($status) {
 	    my @files = ();
@@ -539,4 +577,15 @@
 		&my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
 	    }
+
+	    # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
+	    my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
+	    $command = "$magicdstool -dbname gpc1  -stage $stage -stage_id $stage_id -updaterun -set_state goto_purged";
+	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
+	    }
+
 	} else {
 	    my $command = "$stacktool -updaterun  -stack_id $stage_id -state $error_state";
@@ -549,5 +598,5 @@
 		&my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
 	    }
-	    exit $PS_EXIT_UNKNOWN_ERROR;
+#	    exit $PS_EXIT_UNKNOWN_ERROR;
 	}
     }
@@ -575,5 +624,5 @@
 	# No skycells were found for some reason.
 	# Not technically an "error," but a "you told me to do X, and I can't. Please fix this yourself."
-	my $command = "$difftool -updaterun -diff_id $stage_id -set_state $error_state";
+	my $command = "$difftool -updaterun -diff_id $stage_id -state $error_state";
 	$command .= " -dbname $dbname" if defined $dbname;
 	
@@ -582,5 +631,5 @@
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
+	    &my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
 	}
 	
@@ -600,20 +649,27 @@
 
 	my $status = 1;
-	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
-
-	    unless ($ipprc->file_exists($config_file)) {
-		print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
-		    " because config file ($config_file) is missing\n";
-		$status = 0;
-	    }
-	}
-	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
-
-	    if ($ipprc->file_exists($config_file)) {
-		print STDERR "skipping scrubbed for diffRun $stage_id $skycell_id" .
-		    " because config file ($config_file) is present\n";
-		$status = 0;
+	if ((!exists($skyfile->{path_base}))||
+	    (!defined($path_base))) {
+	    $status = 0;
+	}
+	if ($status) {
+	    
+	    if ($mode eq "goto_cleaned") {
+		my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
+		
+		unless ($ipprc->file_exists($config_file)) {
+		    print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
+			" because config file ($config_file) is missing\n";
+		    $status = 0;
+		}
+	    }
+	    elsif ($mode eq "goto_scrubbed") {
+		my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
+		
+		if ($ipprc->file_exists($config_file)) {
+		    print STDERR "skipping scrubbed for diffRun $stage_id $skycell_id" .
+			" because config file ($config_file) is present\n";
+		    $status = 0;
+		}
 	    }
 	}
@@ -653,14 +709,14 @@
 #	print STDERR "MY STATUS: $status\n";
 	if ($status) {
-	    my $command = "$difftool -diff_id $stage_id";
-
-	    if ($mode eq "goto_purged") {
-		$command .= " -updaterun -state purged";
+	    my $command = "$difftool -diff_id $stage_id -skycell_id $skycell_id";
+
+	    if ($mode eq "goto_purged") {
+		$command .= " -topurgedskyfile";
 	    }
 	    elsif ($mode eq "goto_cleaned") {
-		$command .= " -updaterun -state cleaned";
+		$command .= " -tocleanedskyfile";
 	    }
 	    elsif ($mode eq "goto_scrubbed") {
-		$command .= " -updaterun -state scrubbed";
+		$command .= " -toscrubbedskyfile";
 	    }
 
@@ -673,4 +729,15 @@
 		&my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
 	    }
+
+	    # Tell magicdstool that we've cleaned up this data, so it needs to do the same if it needs to do the same.
+	    my $magicdstool = can_run('magicdstool') or die "Can't find magicdstool";
+	    $command = "$magicdstool -dbname gpc1 -stage $stage -stage_id $stage_id -updaterun -set_state goto_purged";
+	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform magicdstool: $error_code", "$stage", $stage_id, $error_code);
+	    }
+
 	} else {
 	    my $command = "$difftool -updaterun -diff_id $stage_id -state $error_state";
@@ -684,5 +751,5 @@
 		&my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
 	    }
-	    exit $PS_EXIT_UNKNOWN_ERROR;
+#	    exit $PS_EXIT_UNKNOWN_ERROR;
 	}
     }
