Index: trunk/ippScripts/scripts/phase2.pl
===================================================================
--- trunk/ippScripts/scripts/phase2.pl	(revision 9097)
+++ trunk/ippScripts/scripts/phase2.pl	(revision 9364)
@@ -8,18 +8,33 @@
 use PS::IPP::Metadata::Stats;
 use Data::Dumper;
+use PS::IPP::Config;
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+use File::Spec;
 
-use constant RECIPE => 'PPIMAGE_OBDF.config'; # Recipe to use
-use constant PREFIX => 'obdf';	# Prefix to attach to filenames
-use constant DELETE_STATS => 0;	# Delete the statistics file when done?
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+use constant RECIPE => 'PPIMAGE_OBDSF.config'; # Recipe to use
 
 # Parse the command-line arguments
-if (scalar @ARGV != 4) {
-    die "Perform phase 2 processing.\n\n" .
-	"Usage: $0 EXP_ID CLASS_ID INPUT.fits OUTPUT_ROOT\n\n";
-}
-my $expTag = shift @ARGV;	# Exposure ID
-my $classId = shift @ARGV;	# Class ID
-my $input = shift @ARGV;	# Input FITS file
-my $output = shift @ARGV;	# Output root name
+my ($expTag,			# Exposure tag
+    $classId,			# Class Id
+    $input,			# Input FITS file
+    $no_update			# Don't update the database?
+    );
+GetOptions(
+    'exp_tag|e=s'   => \$expTag,
+    'class_id|i=s'  => \$classId,
+    'uri|u=s'       => \$input,
+    'no-update'     => \$no_update
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --exp_tag --class_id --uri",
+    -exitval => 3,
+) unless defined $expTag 
+    and defined $classId 
+    and defined $input;
 
 # Look for programs we need
@@ -30,10 +45,11 @@
 
 ### Output file name --- must match camera configuration!
-my $outputRoot =  PREFIX . '_' . $output;
-my $outputName = $outputRoot . '.' . $classId ;
-my $outputImage = $outputName . '.fit';
-my $outputStats = $outputName . '.stats';
-my $outputBin1Name = $outputName . 'b1.fit';
-my $outputBin2Name = $outputName . 'b2.fit';
+my ($vol, $dir, $file) = File::Spec->splitpath( $input );
+my $outputRoot =  $expTag . '.p2.';
+$outputRoot = File::Spec->rel2abs( File::Spec->catpath( $vol, $dir, $outputRoot ), $ipprc->workdir() );
+my $outputImage = $outputRoot  . '.' . $classId . '.fits';
+my $outputStats = $outputRoot  . '.' . $classId . '.stats';
+my $outputBin1Name = $outputRoot . '.' . $classId . 'b1.fits';
+my $outputBin2Name = $outputRoot . '.' . $classId . 'b2.fits';
 
 # Run ppImage
@@ -45,4 +61,7 @@
     die "Unable to perform ppImage on $input: $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: $outputBin1Name\n" if not -f $outputBin1Name;
+    die "Couldn't find expected output file: $outputBin2Name\n" if not -f $outputBin2Name;
+    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
 }
 
@@ -51,5 +70,5 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, "$outputStats" or die "Can't open statistics file $outputStats: $!\n";
+    open $statsFile, $outputStats or die "Can't open statistics file $outputStats: $!\n";
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
@@ -62,5 +81,8 @@
 
 # Add the processed file to the database
-{
+$outputImage = File::Spec->abs2rel( $outputImage, $ipprc->workdir() );
+$outputBin1Name = File::Spec->abs2rel( $outputBin1Name, $ipprc->workdir() );
+$outputBin2Name = File::Spec->abs2rel( $outputBin2Name, $ipprc->workdir() );
+unless ($no_update) {
     # Command to run dettool
     my $command = "$p2tool -processed";
@@ -79,7 +101,7 @@
     die "Unable to perform p2tool -processed for $expTag/$classId: $error_code\n"
 	if not $success;
+
+    unlink $outputStats;
 }
 
-unlink "$outputStats" if DELETE_STATS;
-
 __END__
