Index: trunk/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 24200)
+++ trunk/ippScripts/scripts/ipp_cleanup.pl	(revision 24642)
@@ -60,5 +60,8 @@
 
 
-my %stages = ( chip => 1, camera => 1, fake => 1, warp => 1, stack => 1, diff  => 1);
+my %stages = ( chip => 1, camera => 1, fake => 1, warp => 1, stack => 1, diff  => 1,
+	       detrend.process.imfile => 1, detrend.process.exp => 1, detrend.stack.imfile => 1,
+	       detrend.normstat.imfile => 1, detrend.norm.imfile => 1, detrend.norm.exp => 1,
+	       detrend.resid.imfile => 1, detrend.resid.exp => 1 );
 unless ($stages{$stage}) {
     die "unknown stage $stage for ipp_cleanup.pl\n";
@@ -118,5 +121,5 @@
         if ($mode eq "goto_cleaned") {
             my $config_file = $ipprc->filename("PPIMAGE.CONFIG", $path_base, $class_id);
-
+	    print STDERR "CHIP: CONFIG_FILE : $config_file\n";
             if (!$config_file or ! -e $config_file) {
                 print STDERR "skipping cleanup for chipRun $stage_id $class_id "
@@ -361,8 +364,203 @@
 }
 
-# left TODO
+### 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";
+
+    # Get list of component imfiles
+    my $skyfiles;                  # Array reference of component files
+    my $command = "stacktool -pendingcleanupskyfile -stack_id $stage_id"; # Command to run
+    $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 stacktool: $error_code", "stack", $stage_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
+	&my_die("Unable to parse metadata config doc", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    $skyfiles = parse_md_list($metadata) or 
+	&my_die("Unable to parse metadata list", "stack", $stage_id, $PS_EXIT_PROG_ERROR);
+    
+    my @files = ();
+    foreach my $skyfile (@{ $skyfiles }) {
+	my $path_base = $skyfile->{path_base};
+	my $skycell_id = $skyfile->{skycell_id};
+
+	my $status = 1;
+	if ($mode eq "goto_cleaned") {
+	    my $config_file = $ipprc->filename("PPSTACK.CONFIG", $path_base, $skycell_id);
+	    print STDERR "MY CONFIG FILE = $config_file\n";
+	    printf(STDERR "BOOLS: %d %d %d %s\n",!$config_file, ! -e $config_file, -e $config_file,$config_file);
+	    $config_file =~ s%^file://%%;
+	    if (!$config_file or ! -e $config_file) {
+		print STDERR "skipping cleanup for stackRun $stage_id $skycell_id" .
+		    " because config file is missing\n";
+		$status = 0;
+	    }
+	    $config_file = 'file://' . $config_file;
+	}
+	if ($status) {
+	    # delete the temporary image datafiles
+	    addFilename(\@files, "PPSTACK.OUTPUT", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSTACK.OUTPUT.MASK", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSTACK.OUTPUT.VARIANCE", $path_base, $skycell_id);
+
+	    if ($mode eq "goto_purged") {
+		# additional files to remove for 'purge' mode
+		addFilename(\@files, "PPSTACK.CONV.KERNEL", $path_base, $skycell_id);
+		addFilename(\@files, "PPSTACK.OUTPUT.JPEG1", $path_base, $skycell_id);
+		addFilename(\@files, "PPSTACK.OUTPUT.JPEG2", $path_base, $skycell_id);
+		# Commented out to match warp files.
+		#addFilename(\@files, "PPSTACK.TARGET.PSF", $path_base, $skycell_id);
+		#addFilename(\@files, "PPSTACK.CONFIG", $path_base, $skycell_id);
+	    }
+
+	    $status = &delete_files(\@files);
+	}
+
+	if ($status) {
+	    my $command = "$stacktool -stack_id $stage_id";
+	    if ($mode eq "goto_purged") {
+		$command .= " -updaterun -state purged";
+	    } else {
+		$command .= " -updaterun -state cleaned";
+	    }
+	    $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 stacktool: $error_code", "stack", $stage_id, $error_code);
+	    }
+	} else {
+	    my $command = "$stacktool -updaterun  -stack_id $stage_id -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 stacktool: $error_code", "stack", $stage_id, $error_code);
+	    }
+	    exit $PS_EXIT_UNKNOWN_ERROR;
+	}
+    }
+    exit 0;
+}
+
+if ($stage eq 'diff') {
+    
+    die "--stage_id required for stage diff\n" if !$stage_id;
+
+    # this stage uses 'difftool'
+    my $difftool = can_run('difftool') or die "Can't find difftool";
+
+    # Get list of component imfiles
+    my $skyfiles;                  # Array reference of component files
+    my $command = "difftool -pendingcleanupskyfile -diff_id $stage_id"; # Command to run
+    $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 difftool: $error_code", "diff", $stage_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @{ $stdout_buf }) or 
+	&my_die("Unable to parse metadata config doc", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
+
+    $skyfiles = parse_md_list($metadata) or 
+	&my_die("Unable to parse metadata list", "diff", $stage_id, $PS_EXIT_PROG_ERROR);
+    
+    my @files = ();
+    foreach my $skyfile (@{ $skyfiles }) {
+	my $path_base = $skyfile->{path_base};
+	my $skycell_id = $skyfile->{skycell_id};
+
+	my $status = 1;
+	if ($mode eq "goto_cleaned") {
+	    my $config_file = $ipprc->filename("PPSUB.CONFIG", $path_base, $skycell_id);
+	    print STDERR "MY CONFIG FILE = $config_file\n";
+	    printf(STDERR "BOOLS: %d %d %d %s\n",!$config_file, ! -e $config_file, -e $config_file,$config_file);
+	    $config_file =~ s%^file://%%;
+	    if (!$config_file or ! -e $config_file) {
+		print STDERR "skipping cleanup for diffRun $stage_id $skycell_id" .
+		    " because config file ($config_file) is missing\n";
+		$status = 0;
+	    }
+	    $config_file = 'file://' . $config_file;
+	}
+	if ($status) {
+	    # delete the temporary image datafiles
+	    addFilename(\@files, "PPSUB.OUTPUT", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.OUTPUT.MASK", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.OUTPUT.VARIANCE", $path_base, $skycell_id);
+
+	    addFilename(\@files, "PPSUB.INVERSE", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.INVERSE.MASK", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.INVERSE.VARIANCE", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.INPUT.CONV", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.INPUT.CONV.MASK", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.INPUT.CONV.VARIANCE", $path_base, $skycell_id);
+	    
+	    addFilename(\@files, "PPSUB.REF.CONV", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.REF.CONV.MASK", $path_base, $skycell_id);
+	    addFilename(\@files, "PPSUB.REF.CONV.VARIANCE", $path_base, $skycell_id);
+	    
+	    if ($mode eq "goto_purged") {
+		# additional files to remove for 'purge' mode
+		addFilename(\@files, "PPSUB.OUTPUT.KERNELS", $path_base, $skycell_id);
+		addFilename(\@files, "PPSUB.OUTPUT.JPEG1", $path_base, $skycell_id);
+		addFilename(\@files, "PPSUB.OUTPUT.JPEG2", $path_base, $skycell_id);
+		# Commented out to match warp files.
+		#addFilename(\@files, "PPSUB.CONFIG", $path_base, $skycell_id);
+		addFilename(\@files, "PPSUB.OUTPUT.SOURCES", $path_base, $skycell_id);
+		addFilename(\@files, "PPSUB.INVERSE.SOURCES", $path_base, $skycell_id);
+		
+	    }
+	    print STDERR "MY FILES: @files\n";
+	    $status = &delete_files(\@files);
+	}
+	print STDERR "MY STATUS: $status\n";
+	if ($status) {
+	    my $command = "$difftool -diff_id $stage_id";
+#	    my $command = "$difftool -diff_id $stage_id -skycell_id $skycell_id";
+	    if ($mode eq "goto_purged") {
+		$command .= " -updaterun -state purged";
+	    } else {
+		$command .= " -updaterun -state cleaned";
+	    }
+	    $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 difftool: $error_code", "diff", $stage_id, $error_code);
+	    }
+	} else {
+	    my $command = "$difftool -updaterun -diff_id $stage_id -state $error_state";
+#	    my $command = "$difftool -updaterun -diff_id $stage_id -skycell_id $skycell_id -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 difftool: $error_code", "diff", $stage_id, $error_code);
+	    }
+	    exit $PS_EXIT_UNKNOWN_ERROR;
+	}
+    }
+    exit 0;
+}
+if ($stage eq 'fake') {
+    die "ipp_cleanup.pl -stage fake not yet implemented. Probably will just mark database cleaned.\n";
+} 
 # fake : faketool : -pendingcleanupimfile (loop over imfiles)
-# stack: stacktool : -pendingcleanupskyfile
-# diff:  difftool : -pendingcleanupskyfile (loop over skyfiles)
 
 die "ipp_cleanup.pl -stage $stage not yet implemented\n";
