Index: trunk/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 25762)
+++ trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 25781)
@@ -91,6 +91,7 @@
 
     # if there are no chipProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
+    # XXX Why? This could just mean there's nothing to cleanup, or that we're trying to rerun an errored run.
     if (@$stdout_buf == 0)  {
-	my $command = "$chiptool -chip_id $stage_id -updaterun -set_state new";
+	my $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
 	$command .= " -dbname $dbname" if defined $dbname;
 
@@ -123,5 +124,4 @@
             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
 
-#            if (!$config_file or ! -e $config_file) {
 	    unless ($ipprc->file_exists($config_file)) {
                 print STDERR "skipping cleanup for chipRun $stage_id $class_id "
@@ -199,4 +199,16 @@
                 &my_die("Unable to perform chiptool: $error_code", "chip", $stage_id, $error_code);
             }
+	    
+	    # We want to flag the run as well, to avoid attempting to reprocess the same data over and over again.
+	    $command = "$chiptool -chip_id $stage_id -updaterun -set_state $error_state";
+	    $command .= " -dbname $dbname" if defined $dbname;
+
+	    ( $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 chiptool: $error_code", "chip", $stage_id, $error_code);
+
+	    }
         }
     }
@@ -314,4 +326,20 @@
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
         &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
+    }
+    
+    if (@$stdout_buf == 0) {
+	# 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 = "$warptool -updaterun -warp_id $stage_id -set_state $error_state";
+	$command .= " -dbname $dbname" if defined $dbname;
+	
+	my ( $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 warptool: $error_code", "warp", $stage_id, $error_code);
+	}
+	
+	exit(0);
     }
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
@@ -389,4 +417,6 @@
                 &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
             }
+
+
          } else {
 	    my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -set_state $error_state";
@@ -399,5 +429,6 @@
                 &my_die("Unable to perform warptool: $error_code", "warp", $stage_id, $error_code);
             }
-            exit $PS_EXIT_UNKNOWN_ERROR;
+
+	    #            exit $PS_EXIT_UNKNOWN_ERROR;
         }
     }
@@ -422,4 +453,21 @@
 	&my_die("Unable to perform stacktool: $error_code", "stack", $stage_id, $error_code);
     }
+
+    if (@$stdout_buf == 0) {
+	# 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";
+	$command .= " -dbname $dbname" if defined $dbname;
+	
+	my ( $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 warptool: $error_code", "warp", $stage_id, $error_code);
+	}
+	
+	exit(0);
+    }
+
     my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
 	&my_die("Unable to parse metadata config doc", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
@@ -523,4 +571,21 @@
 	&my_die("Unable to perform difftool: $error_code", "diff", $stage_id, $error_code);
     }
+
+    if (@$stdout_buf == 0) {
+	# 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";
+	$command .= " -dbname $dbname" if defined $dbname;
+	
+	my ( $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 warptool: $error_code", "warp", $stage_id, $error_code);
+	}
+	
+	exit(0);
+    }
+
     my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
 	&my_die("Unable to parse metadata config doc", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
@@ -646,5 +711,5 @@
     # if there are no fakeProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
     if (@$stdout_buf == 0)  {
-	my $command = "$faketool -fake_id $stage_id -updaterun -set_state new";
+	my $command = "$faketool -fake_id $stage_id -updaterun -set_state $error_state";
 	$command .= " -dbname $dbname" if defined $dbname;
 
