Index: trunk/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 23186)
+++ trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 23229)
@@ -45,10 +45,18 @@
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set up", $stage_id, $PS_EXIT_CONFIG_ERROR); # this is used for PATH, NEB filename conversions
 
-# $mode must be one of "goto_cleaned", "goto_scrubbed", or "goto_purged"
-# goto_cleaned and goto_scrubbed both result in 'cleaned': scrubbed allows chips without config files to
-# be cleaned (they cannot be recovered, but the small data is left behind)
+# $mode must be one of "goto_cleaned", "goto_scrubbed", or
+# "goto_purged" goto_cleaned and goto_scrubbed both result in
+# 'cleaned' on success ('scrubbed' allows chips without config files
+# to be cleaned; they cannot be recovered, but the small data is left
+# behind). XXX make 'scrubbed' a data_state?
 unless (($mode eq "goto_cleaned") || ($mode eq "goto_scrubbed") || ($mode eq "goto_purged")) {
     die "invalid cleanup mode $mode\n";
 }
+
+my $error_state;
+if ($mode eq "goto_cleaned")  { $error_state = "error_cleaned";  }
+if ($mode eq "goto_scrubbed") { $error_state = "error_scrubbed"; }
+if ($mode eq "goto_purged")   { $error_state = "error_purged";   }
+
 
 my %stages = ( chip => 1, camera => 1, fake => 1, warp => 1, stack => 1, diff  => 1);
@@ -148,7 +156,8 @@
             }
         } else {
-            # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will stop be run
-            my $command = "$chiptool -updateprocessedimfile -chip_id $stage_id -class_id $class_id -code 1";
-            $command .= " -dbname $dbname" if defined $dbname;
+
+	    # if an error happens for one chip, the chipRun will stay in goto_*, but the chips will go to error_* (matching the goto_*)
+	    my $command = "$chiptool -updateprocessedimfile -chip_id $stage_id -class_id $class_id -set_state $error_state";
+	    $command .= " -dbname $dbname" if defined $dbname;
 
             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -216,9 +225,13 @@
 
     if ($status)  {
-        my $command = "$camtool -cam_id $stage_id -updaterun";
+        my $command;
         if ($mode eq "goto_cleaned") {
-            $command .= " -state cleaned";
-        } else {
-            $command .= " -state purged";
+            $command = "$camtool -updaterun -cam_id $stage_id -set_state cleaned";
+	}
+        if ($mode eq "goto_scrubbed") {
+            $command = "$camtool -updaterun -cam_id $stage_id -set_state cleaned";
+	}
+        if ($mode eq "goto_purged") {
+            $command = "$camtool -updaterun -cam_id $stage_id -set_state purged";
         }
         $command .= " -dbname $dbname" if defined $dbname;
@@ -230,5 +243,6 @@
         }
     } else {
-        my $command = "$camtool -updateprocessedexp -cam_id $stage_id -code 1";
+	# since 'camera' has only a single imfile, we can just update the run
+        my $command = "$camtool -updaterun -cam_id $stage_id -set_state $error_state";
         $command .= " -dbname $dbname" if defined $dbname;
 
@@ -319,7 +333,6 @@
             }
          } else {
-            # XXX: -updateskyfile mode does not exist, need to add it
-            my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -code 1";
-            $command .= " -dbname $dbname" if defined $dbname;
+	    my $command = "$warptool -updateskyfile -warp_id $stage_id -skycell_id $skycell_id -set_state $error_state";
+	    $command .= " -dbname $dbname" if defined $dbname;
 
             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -337,6 +350,6 @@
 # left TODO
 # fake : faketool : -pendingcleanupimfile (loop over imfiles)
-# stack: stacktool : -pendingcleanupskyfile (loop over skyfiles)
-# diff:  difftool : -pendingcleanupskyfile
+# stack: stacktool : -pendingcleanupskyfile
+# diff:  difftool : -pendingcleanupskyfile (loop over skyfiles)
 
 die "ipp_cleanup.pl -stage $stage not yet implemented\n";
@@ -367,4 +380,5 @@
 }
 
+# XXX we currently do not set the error state in the db on my_die
 sub my_die
 {
