Index: trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11298)
+++ trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11316)
@@ -11,5 +11,13 @@
 use PS::IPP::Metadata::Stats;
 
-use PS::IPP::Config;
+use PS::IPP::Config qw(
+    $PS_EXIT_SUCCESS
+    $PS_EXIT_UNKNOWN_ERROR
+    $PS_EXIT_SYS_ERROR
+    $PS_EXIT_CONFIG_ERROR
+    $PS_EXIT_PROG_ERROR
+    $PS_EXIT_DATA_ERROR
+    $PS_EXIT_TIMEOUT_ERROR
+    );
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
 use File::Spec;
@@ -60,9 +68,15 @@
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
+if ($missing_tools) { 
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
 
 # Recipe to use in processing
 my $recipe = RECIPES->{lc($det_type)};
-die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
+unless (defined $recipe) {
+    warn("Unrecognised detrend type: $det_type");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 ### Output file name
@@ -88,9 +102,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input_uri: $error_code\n" if not $success;
-    die "Couldn't find expected output file: $outputImage\n" if not -f $outputImage;
-    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
-    die "Couldn't find expected output file: $outputBin1\n" if not -f $outputBin1;
-    die "Couldn't find expected output file: $outputBin2\n" if not -f $outputBin2;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $class_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputImage", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputImage;
+    &my_die("Couldn't find expected output file: $outputStats", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
+    &my_die("Couldn't find expected output file: $outputBin1", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
+    &my_die("Couldn't find expected output file: $outputBin2", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
 }
 
@@ -104,7 +121,7 @@
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @contents)
-        or die "unable to parse metadata config";
+        or &my_die("Unable to parse metadata config", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -131,8 +148,28 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addprocessedimfile for $det_id/$exp_tag/$class_id: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addprocessedimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;
+}
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $exp_tag = shift; # Exposure tag
+    my $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $exp_tag and $class_id) {
+	my $command = "$dettool -addprocessedimfile -det_id $det_id -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
