Index: trunk/pstamp/scripts/pstamp_finish.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_finish.pl	(revision 21190)
+++ trunk/pstamp/scripts/pstamp_finish.pl	(revision 21410)
@@ -14,4 +14,5 @@
 use File::Temp qw( tempfile );
 use File::Copy;
+use File::Basename qw(dirname);
 
 use PS::IPP::Metadata::Config;
@@ -23,22 +24,31 @@
 use PStamp::Job qw( :standard );
 
-my ( $req_id, $req_name, $req_file, $product, $dbname, $verbose, $save_temps );
+my ( $req_id, $req_name, $req_file, $out_dir, $product, $dbname, $verbose, $save_temps, $redirect_output);
 
 # the char to the right of the bar may be used as a single - alias for the longer name
 # for example --input and -i are equivalent
 GetOptions(
-           'req_id=s'   => \$req_id,
-           'req_name=s' => \$req_name,
-           'req_file=s' => \$req_file,
-           'product=s'  => \$product,
-	   'dbname=s'   => \$dbname,
-	   'verbose'    => \$verbose,
-	   'save-temps' => \$save_temps,
+           'req_id=s'       => \$req_id,
+           'req_name=s'     => \$req_name,
+           'req_file=s'     => \$req_file,
+           'product=s'      => \$product,
+           'out_dir=s'      => \$out_dir,
+	   'dbname=s'       => \$dbname,
+	   'verbose'        => \$verbose,
+	   'save-temps'     => \$save_temps,
+	   'redirect-output' => \$redirect_output,
 ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 
-die "usage: --req_id id --req_name name --req_file file --product product [--dbname dbname --verbose]\n"
-    if !$req_id or !$req_name or !$req_file or !$product;
+die "usage: --req_id id --req_name name --req_file file --product product --out_dir output_directory [--dbname dbname --verbose]\n"
+    if !$req_id or !$req_name or !$req_file or !$product or !$out_dir;
+
+my $ipprc = PS::IPP::Config->new(); # IPP Configuration
+
+if ($redirect_output) {
+    my $logDest = "$out_dir/psfinish.$req_id.log";
+    $ipprc->redirect_output($logDest);
+}
 
 my $missing_tools;
@@ -53,5 +63,4 @@
 }
 
-my $ipprc = PS::IPP::Config->new(); # IPP Configuration
 
 my $outputDataStoreRoot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT');
@@ -64,7 +73,11 @@
     my $fileset = $req_name;
 
+
     # Here we invoke the assumption that the output for the request is placed in the
     # fileset directory directly
-    my $out_dir = "$outputDataStoreRoot/$product/$fileset";
+#    my $out_dir = "$outputDataStoreRoot/$product/$fileset";
+
+    # now we are assuming that the output directory is the dirname of the request file
+    # XXX: put this in the database
 
     print STDERR "product: $product  REQ_NAME: $req_name $out_dir\n" if $verbose;
@@ -72,5 +85,5 @@
     if (!-e $out_dir) {
         # something must have gone wrong parsing the request
-        print STDERR  "output fileset directory $out_dir does not exist\n";
+        print STDERR  "output directory $out_dir does not exist\n";
 
         if (!mkdir $out_dir) {
@@ -83,5 +96,5 @@
         # XXX TODO: fault the request so we pstamp_finish doesn't keep trying to process the
         # request
-        print STDERR "output fileset directory $out_dir exists but is not a directory";
+        print STDERR "output directory $out_dir exists but is not a directory";
         stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR, $dbname);
     }
@@ -111,7 +124,6 @@
     print $rlf "results.fits|||table|\n";
 
-    # XXX: this file is in the request's workdir not the out_dir
-    my $err_file = "$out_dir/parse_error.txt";
-    if (-e $err_file ) {
+    my $err_file = "parse_error.txt";
+    if (-e "$out_dir/$err_file" ) {
         print $rlf "$err_file|||text|\n";
     }
@@ -222,8 +234,6 @@
     if (!$request_fault) {
         # register the fileset
-        # Note that we are assuming that the fileset's files are already in the fileset directory.
-        # We could put them somewhere else and copy them in but for now we set the output directory
-        # to the fileset directory at parse time
         my $command = "$dsreg --list $reglist_name --add $fileset --product $product --type PSRESULTS";
+        $command .= " --link --datapath $out_dir";
         $command .= " --dbname $dbname" if $dbname;
 
