#!/bin/csh
#
#--------------------------------------------------------------------
#
#   Program:
#
#       tape2ql.csh
#
#   Description:
#
#       C shell script to move DDF products extracted from tape
#       into the data receipt pipeline. This script takes three command
#       line arguments: the path into which the datasets will be
#       copied, the location where the tape was extracted, and the
#       input directory of the swapper. Files in the input directory 
#       of the form:
#
#       hst_????_lz_????-??-?????-??-???_v01.dat1
#       hst_????_lz_????-??-?????-??-???_v01.sfdu
#
#       are copied to the data receipt pipeline as input files to the
#       swapper of the form:
#
#       ql_????????????????????.raw
#       ql_????????????????????.dan
#
#       respectively. Additionally, an OSF is created for each pair of
#       files with the swapper stage triggered.
#
#	NOTE: the variable OPUS_DEFINITIONS_DIR must be defined in the 
#             in the process' environment
#
#   Return:
#
#       0 if processing encountered no errors
#       1 if one or more errors occurred during processing
#
#   Usage:
#
#       tape2ql.csh pathname inputdir swapper_input_dir
#
#   Example:
#
#       (no errors)
#
#       % tape2ql.csh blue.path . OPUS_DDF_DIR:
#
#       Found 1 input datasets in ..
#       ...creating ql_a2c419940911t171656z.raw
#       ...creating ql_a2c419940911t171656z.dan
#       ...creating OSF
#
#       Processing complete.
#
#       (errors)
#
#       % tape2ql.csh red.path . OPUS_DDF_DIR:
#
#       Found 1 input datasets in ..
#       ...creating ql_a2c419940911t171656z.raw
#       ...creating ql_a2c419940911t171656z.dan
#       ...creating OSF
#       Unable to set SW to w
#
#       Processing complete.
#
#       There were 1 errors:
#       ...failed copies to .raw file:
#       ...failed copies to .dan file:
#       ...failed OSF creations:
#       ql_a2c419940911t171656z
#
#--------------------------------------------------------------------
#                          REVISION HISTORY
#--------------------------------------------------------------------
#  MOD             PR
# LEVEL   DATE   NUMBER  User     Description
# ----- -------- ------  -------  ------------------------------------
#  001  03/03/99 38143   WMiller  Initial code
#  002  04/22/02 42218   Sontag   Fix col name in call to osf_create.
#--------------------------------------------------------------------
#
#
set USAGE = "Usage: tape2ql.csh pathname input_dir swapper_input_dir"
#
# Validate inputs
#
if ( $#argv != 3 ) then
   echo "tape2ql - Incorrect number of command line arguments."
   echo ""
   echo $USAGE
   echo ""
   exit 1
endif
if ( ! $?OPUS_DEFINITIONS_DIR ) then
   echo "tape2ql - OPUS_DEFINITIONS_DIR is not defined in the environment."
   echo ""
   exit 1
endif
set pathfile = `osfile_stretch_file OPUS_DEFINITIONS_DIR:$1`
if ( $status != 0) then
   echo "tape2ql - Unable to locate OPUS_DEFINITIONS_DIR:$1."
   echo ""
   exit 1
endif
set indir = `osfile_stretch_file $2`
if ( $status != 0) then
   echo "tape2ql - Unable to locate $2."
   echo ""
   exit 1
endif
set outdir = `osfile_stretch_file $3`
if ( $status != 0) then
   echo "tape2ql - Unable to locate $3."
   echo ""
   exit 1
endif
#
cd $indir
set targets = `ls hst_????_lz_????-??-?????-??-???_v01.dat1`
echo Found $#targets input datasets in $2.
echo
#
set error_count = 0
set bad_raw_copies = ""
set bad_dan_copies = ""
set bad_osfs = ""
# parse podname from each target file
foreach file ( $targets )
   # extract 4 digits following HST_
   set a = `echo $file | awk -F_ '{ print $2 }'`
   # extract sequence between LZ_ and _V01.DAT
   set b = `echo $file | awk -F_ '{ print $4 }'`
   # strip dashes from above
   set b = `echo $b | awk -F- '{ print $1$2$3$4$5 }'`
   set raw = ql_$a$b.raw
   set dan = ql_$a$b.dan
   echo "...creating "$raw
   cp $file $outdir/$raw
   if ( $status != 0 ) then
      set bad_raw_copies = ( $bad_raw_copies $file )
      @error_count++
   endif
   echo "...creating "$dan
   cp $file:r.sfdu $outdir/$dan
   if ( $status != 0 ) then
      set bad_dan_copies = ( $bad_dan_copies $file:r.sfdu )
      @error_count++
   endif
   echo "...creating OSF"
   osf_create -p $1 -f ql_$a$b -t ddf -n 000 -c SW -s w
   if ( $status != 0 ) then
      set bad_osfs = ( $bad_osfs ql_$a$b )
      @ error_count++
   endif
end
echo ""
echo "Processing complete."
echo ""
if ( $error_count != 0 ) then
   echo "There were $error_count errors:"
   echo "...failed copies to .raw file:"
   foreach file ( $bad_raw_copies )
      echo $file
   end
   echo "...failed copies to .dan file:"
   foreach file ( $bad_dan_copies )
      echo $file
   end
   echo "...failed OSF creations:"
   foreach file ( $bad_osfs )
      echo $file
   end
   exit 1
endif
exit 0
