Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 28043)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 29893)
@@ -70,9 +70,19 @@
 my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $cam_id, $PS_EXIT_CONFIG_ERROR ); # IPP configuration
 
-my $logDest = $ipprc->filename("LOG.EXP", $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-
 if (not defined $run_state) { $run_state = 'new'; }
-if ($run_state eq 'update') {
-    $logDest .= '.update';
+
+my_die ("$run_state is an invalid value for run-state", $cam_id, $PS_EXIT_PROG_ERROR) unless ($run_state eq 'new' or $run_state eq 'update');
+
+
+my $replicateOutputs = 1;
+
+my $logDest;
+my $traceDest;
+if ($run_state eq 'new') {
+    $logDest = prepare_output("LOG.EXP", $outroot, undef, 0);
+    $traceDest = prepare_output("TRACE.EXP", $outroot, undef, 0);
+} else {
+    $logDest = prepare_output("LOG.EXP.UPDATE", $outroot, undef, 0);
+    $traceDest = prepare_output("TRACE.EXP.UPDATE", $outroot, undef, 0);
 }
 
@@ -188,5 +198,5 @@
     print $list4File ($chipMask . "\n");
 
-    push @outMasks, $ipprc->filename("PSASTRO.OUTPUT.MASK", $outroot, $class_id) if $produceMasks;
+    push @outMasks, prepare_output("PSASTRO.OUTPUT.MASK", $outroot, $class_id, 1) if $produceMasks;
 }
 close $list1File;
@@ -199,24 +209,17 @@
 
 # the camera configurations should define the psastro output to be a single file (MEF), regardless of the inputs
-my $jpeg1      = $ipprc->filename("PPIMAGE.JPEG1",      $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-my $jpeg2      = $ipprc->filename("PPIMAGE.JPEG2",      $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-my $fpaObjects = $ipprc->filename("PSASTRO.OUTPUT",     $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-my $fpaStats   = $ipprc->filename("PSASTRO.STATS",      $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-my $traceDest  = $ipprc->filename("TRACE.EXP",          $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-my $configuration = $ipprc->filename("PSASTRO.CONFIG",  $outroot) or &my_die("Missing entry from camera config", $cam_id, $PS_EXIT_CONFIG_ERROR);
-
-if ($run_state eq 'update') {
-    $traceDest .= '.update';
-    $fpaStats .= '.update';
-}
-
-# convert supplied DVO database name to UNIX filename
-my $dvodbReal;
-if (defined $dvodb) {
-    $dvodbReal = $ipprc->dvo_catdir( $dvodb ); # catdir for DVO
-    $dvodbReal = $ipprc->convert_filename_absolute( $dvodbReal );
-}
-
-#my $dtime_addstar = 0;
+my $jpeg1      = prepare_output("PPIMAGE.JPEG1",      $outroot, undef, 1);
+my $jpeg2      = prepare_output("PPIMAGE.JPEG2",      $outroot, undef, 1);
+my $fpaObjects = prepare_output("PSASTRO.OUTPUT",     $outroot, undef, 1);
+my $configuration = prepare_output("PSASTRO.CONFIG",  $outroot, undef, 1);
+
+my $do_stats;
+my $fpaStats; 
+if ($run_state eq 'new') {
+    $do_stats = 1;
+    $fpaStats = prepare_output("PSASTRO.STATS",      $outroot, undef, 1);
+} else {
+    $do_stats = 0;
+}
 
 unless ($no_op) {
@@ -236,5 +239,5 @@
             &my_die("Unable to perform ppImage: $error_code", $cam_id, $error_code);
         }
-        &my_die("Unable to find expected output file: $jpeg1", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg1);
+        check_output($jpeg1, $replicateOutputs);
     }
 
@@ -251,5 +254,5 @@
             &my_die("Unable to perform ppImage: $error_code", $cam_id, $error_code);
         }
-        &my_die("Unable to find expected output file: $jpeg2", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($jpeg2);
+        check_output($jpeg2, $replicateOutputs);
     }
 
@@ -265,9 +268,6 @@
         $command .= " -dbname $dbname" if defined $dbname;
 
-        my $do_stats;
         if ($run_state eq 'new') {
-            $command .= " -stats $fpaStats -recipe PPSTATS CAMSTATS";
             $command .= " -dumpconfig $configuration";
-            $do_stats = 1;
         } elsif ($run_state eq 'update') {
             $command .= " -ipprc $configuration";
@@ -275,4 +275,5 @@
             &my_die("invalid value for run-state: $run_state", $cam_id, $PS_EXIT_CONFIG_ERROR);
         }
+        $command .= " -stats $fpaStats -recipe PPSTATS CAMSTATS" if $do_stats;
 
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -286,6 +287,7 @@
         my $quality;            # Quality flag
         if ($do_stats) {
+            check_output($fpaStats, $replicateOutputs);
+
             my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
-            &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $fpaStatsReal );
 
             # parse stats from metadata
@@ -306,59 +308,14 @@
 
         if (!$quality) {
-            &my_die("Unable to find expected output file: $fpaObjects", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($fpaObjects);
+            check_output($fpaObjects, $replicateOutputs);
 
             foreach my $outMask (@outMasks) {
-                &my_die("Unable to find expected output file: $outMask", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outMask);
+                check_output($outMask, $replicateOutputs);
             }
 
             if ($run_state eq 'new') {
-                &my_die("Couldn't find expected output file: $configuration", $cam_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($configuration);
+                check_output($configuration, $replicateOutputs);
             }
         }
-
-        # run addstar on the output fpaObjects (if a DVO database is defined)
-#         if (defined $dvodbReal and ($run_state eq 'new')) {
-
-#             ## XXX the camera analysis can either save the full set of
-#             ## detections, or just the image metadata, in the dvodb
-
-#             ## get the addstar recipe for this camera and CAMERA reduction
-#             $command = "$ppConfigDump -camera $camera -recipe ADDSTAR $recipe_addstar -dump-recipe ADDSTAR -";
-#             ( $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 ppConfigDump: $error_code", $cam_id, $PS_EXIT_SYS_ERROR);
-#             }
-#             my $recipeData = $mdcParser->parse(join "", @$stdout_buf) or
-#                 &my_die("Unable to parse metadata config doc", $cam_id, $PS_EXIT_SYS_ERROR);
-
-#             ## allow the dvodb to save only images, or the full detection set
-#             my $imagesOnly = metadataLookupBool($recipeData, 'IMAGES.ONLY');
-
-#             # XXX this construct requires the user to have a valid .ptolemyrc
-#             # XXX which in turn points at ippconfig/dvo.site
-#             # require a defined output dvo database to run addstar (ie, refuse to use the .ptolemyrc default)
-#             # XXX this needs to be converted to addstar_client...
-
-#             my $camdir = $ipprc->dvo_cameradir(); # Camera directory for addstar
-#             my $command;
-#             $command  = "$addstar -D CAMERA $camdir -update";
-#             $command .= " -image" if $imagesOnly;
-#             $command .= " -D CATDIR $dvodbReal";
-
-#             my $realFile = $ipprc->file_resolve($fpaObjects);
-#             $command .= " $realFile";
-
-#             my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
-
-#             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 addstar: $error_code", $cam_id, $error_code);
-#             }
-#             $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);   # MJD of starting script
-#         }
     }
 }
@@ -374,5 +331,4 @@
     $fpaCommand .= " -hostname $host" if defined $host;
     $fpaCommand .= " -dtime_script $dtime_script";
-#    $fpaCommand .= " -dtime_addstar $dtime_addstar";
 } else {
     $fpaCommand .= " -updaterun -set_state full";
@@ -393,4 +349,37 @@
 }
 
+exit 0;
+
+sub prepare_output
+{
+    my $filerule = shift;
+    my $outroot  = shift;
+    my $class_id = shift;
+    my $delete = shift;
+    $delete = 0 if !defined $delete;
+
+    my $error;
+    my $output = $ipprc->prepare_output($filerule, $outroot, $class_id, $delete, \$error)
+                    or &my_die("failed to prepare output file for: $filerule", $cam_id, $error);
+
+    return $output;
+}
+
+sub check_output
+{
+    my $file = shift;
+    my $replicate = shift;
+
+    if (!defined $file) {
+        return;
+    }
+
+    &my_die("Couldn't find expected output file: $file",  $cam_id, $PS_EXIT_SYS_ERROR) unless
+        $ipprc->file_exists($file);
+
+    if ($replicate and (file_scheme($file) eq 'neb')) {
+        $ipprc->replicate_file($file) or &my_die("failed to replicate: $file\n",  $cam_id, $PS_EXIT_SYS_ERROR);
+    }
+}
 
 sub my_die
