#!/usr/bin/env perl
##########################################################################
#
# routine: no_whk.pl
#
# purpose: For instruments with one exposure per pod, this module
#          renames the pod file for pipeline processing.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/03/00 43165  MSwam     first version
# 06/20/01 44084  MSwam     verify file size of copied file
# 10/19/04 50663  MSwam     skip EPC if found in podlist file
# 
##########################################################################
#
# prepend the Perl include variable using all components of PATH
# (this allows us to reuse other OTFR Perl code)
#
$PATH  = $ENV{"PATH"};
@parts = split /:/, $PATH;
while (defined($val = pop(@parts))) {
   if (substr($val,0,1) eq "/") { unshift(@INC, $val)}
}

#
# include these modules
#
require 'printmsg.pl';       # prints a formatted status message
require 'otfr_setup.pl';     # OTFR pipeline set-up for processing a request

OTFR_setup();
$exit_copy_failed = 5;

PrintMsg("I","Processing $p_dir");

# open the PODLIST file listing exposures and podfile names
$infile = $p_dir . ".podlist";
if (!open(PODLIST, "$infile")) {
   PrintMsg("E","ERROR: failed to open infile $infile\n"); 
   exit $quit_this_dataset;
}

#
# read each podlist entry, extract the exposure into a new pod file
#
while (<PODLIST>) {
   #
   # split the read buffer ($_) into fields and lower case them
   ($exp, $pod, $indicator) = split(/ /,$_);  
   $exp = lc($exp);
   $pod = lc($pod);
   chomp($indicator);
 
   if ($indicator eq "EDT" or $indicator eq "EPC") {
      # don't process EDT entries
      next;
   }

   # verify pod file exists
   $podfile = $pod . ".pod";
   if (! -e $podfile) {
       PrintMsg("E","no_whk: ERROR - $podfile not found.\n");
       exit $quit_this_dataset;
   }

   #
   # form the output pod file name from the podfilename and exposure name
   #
   $outpod = $pod . "_" . $exp . ".pod";

   # 
   # no podwhack is needed, but copy the pod file to give it the proper
   #   name and ownership
   use File::Copy;
   if (!copy($podfile, $outpod)) {
      PrintMsg("E","ERROR: copy from $podfile to $outpod failed; (error=$!) .\n");
      exit $quit_this_dataset;
   }

   # pause allow buffer flush?
   sleep 5;

   #
   # verify the sizes of the source and dest files match
   # (work-around for dropped blocks in acdsdops environment)
   ($dontcare_dev,$dontcare_ino,$dontcare_mode,$dontcare_nlink,$dontcare_uid,
    $dontcare_gid,$dontcare_rdev,$src_size,$dontcare_atime,$dontcare_mtime,
    $dontcare_ctime,$dontcare_blksize,$dontcare_blocks) = stat($podfile);

   ($dontcare_dev,$dontcare_ino,$dontcare_mode,$dontcare_nlink,$dontcare_uid,
    $dontcare_gid,$dontcare_rdev,$dest_size,$dontcare_atime,$dontcare_mtime,
    $dontcare_ctime,$dontcare_blksize,$dontcare_blocks) = stat($outpod);

   if ($src_size != $dest_size) {
      PrintMsg("E","File size verification failed: $podfile:$src_size != $outpod:$dest_size\n");
      close(PODLIST);
      exit $exit_copy_failed;
   }

   PrintMsg("D","no_whk: copied $podfile to $outpod");
}
close(PODLIST);

# complete, exit success
exit $all_is_well;
