#!/bin/tcsh -fx
##########################################################################
#
# routine: copyaux.csh 
#
# purpose: 
#       copy the auxilliary files from the POD_ARCH_DIR to the
#       SIS_DIR.  Copy all files except those with the extension
#       .SDI or .sdi  If you find a file with the extension .com
#       or .COM, copy that file into the trailer.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 01/17/01 43165  MSwam     first version
# 12/07/01 44438  Rose      borrowed from f2fwf2.csh
# 10/10/02 46769  Rose      Copy and rename the STIM files too.
# 11/20/02 46769  Rose      remove the <cr> from the .ocr files
# 11/21/02 46769  Rose      add a '/' at the end of each .ocr line
# 12/03/02 47036  Rose      put the CSIx back on the p_dset name
# 02/13/03 47597  Rose      Append DCF_NUM for WFC3 to find stimulus
# 12/22/03 49468  MSwam     Put in retries as work-around for DP issue
# 07/16/07 58918  Sherbert Increase robustness w/more checks
# 09/19/07 58918  Sherbert Robustness completely missed, more reporting (-v)
#                   exit w/a retry if foreach loop fails
# 09/20/07 58918  Sherbert -v NOT helping know what ls cannot find :(  add echo
# 01/30/08 58918  Sherbert  Taking advantage of an available PR
# 01/20/09 64274  MSwam     Use a-z/A-Z for tr (upper/lower not in UCB version)
# 07/09/10 65515  MSwam     Remove lowercase filematch for input files
# 08/25/10 64342  Sherbert  fix just one echo line
# 
##########################################################################
echo " "
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
set opustime = "\date '+%Y%j%H%M%S'"
echo `eval ${opustime}`"-INFO-*** executable is $0 *** "
#
set go_absent = 102          #this exit status is in the FATAL range for XPOLL
set quit_this_dataset = 1    #this exit status is mapped and will allow
                             #  the process to try another dataset
set retrigger_me = 74        #this exist status is supposed to set a retrigger
                             #  value in the OSF
set all_is_well = 0
#
# resolve stretches for UNIX
set sisdir = `osfile_stretch_file $OUTPATH`
#
## template of name for failure-count files
## later add _$$ to create name, _* to find names
if ( ! $?USER ) then
    ## I doubt this could ever happen, but let's just make one up
    set USER = "scotchTape"
endif
set tmpDir  = /tmp/${USER}
if ( ! -e ${tmpDir} ) then
    mkdir -p ${tmpDir}
    if ( $status ) then
        ## drat, cannot create tmp dir to hold tmp files
        echo `eval ${opustime}`"-ERROR---Copyaux cannot create ${tmpDir} " | tee -a $trailer
	exit $quit_this_dataset
    endif
endif
set tmpFile = ${tmpDir}/copyAux.failed
#
# parse dataset from OSF field
set dset = `echo $OSF_DATASET | awk '{n=split($1,a,"_");print a[2]}'`
set p_dset = ${CSINAME}${dset}
set firstchar = `echo $OSF_DATASET | awk '{a=substr($1,1,1); print a}'`
#
#------PR.47597--------------------
#
# if this is a WFC3, append the DCF_NUM to the stimulus file name
if ($firstchar =~ [i]) then
    set dcfnum = `echo $OSF_DCF_NUM | awk '{a=substr($1,2,2); print a}'`
    set p_dset = ${CSINAME}${dset}_${dcfnum}
endif
#
#----------------------------------
#
set P_DSET = `echo $p_dset | tr '[a-z]' '[A-Z]'`
set p_dir = $OSF_DATASET
#
# move to working directory
if (! -e $sisdir) then
   echo "ERROR: $sisdir directory does not exist"
   exit $go_absent
endif
cd $sisdir
if ($status != 0) then
   echo "ERROR: Failed to set directory to $sisdir"
   exit $go_absent
endif
#
set trailer = $sisdir/$OSF_DATASET.trx
set logfile = $LOGFILE_EXTN
set stimext = $STIMFILE_EXTN

echo `eval ${opustime}`"-INFO---Copyaux ----------begin ${p_dir} -----------------------" | tee -a $trailer
#
#
# copy the files
set redo = 0
set redoMAX = 10
set loop = 0
set poddir = `osfile_stretch_file $INPATH`
while ($redo < $redoMAX)
  echo `eval ${opustime}`"-INFO-Looking for ${poddir}/${p_dset}.* or ${poddir}/${P_DSET}.*"
  # UNDER LINUX you apparently can't include a file matching pattern that 
  # finds no matches, and still get an overall successful result.  So this
  # trick to handle either upper or lower case input file names ALWAYS FAILS,
  # since only one of the filemasks will end up matching some files.
  # 
  # FOR NOW I have taken the lowercase matching case OUT of the command.
  # If this is needed, some other way will have to be found (like rewriting
  # this whole thing in some other language besides tcsh)
  #
  #foreach i (`/bin/ls -1 ${poddir}/${p_dset}.* ${poddir}/${P_DSET}.*`)
  foreach i (`/bin/ls -1 ${poddir}/${P_DSET}.*`)
    set redo = $redoMAX
    set loop = 1
    set outfil = $i
    #...guard against "./" as poddir used for testing
    set filnam = `echo $i | awk '{a=substr($1,2); print a}'`
    set extension = `echo $filnam | awk '{n=split($1,a,".");print a[2]}'| tr '[A-Z]' '[a-z]'`
    #
    # skip .sdi file
    echo $outfil | grep -i .sdi
    if ( $status == 0 ) then
       continue
    endif
    #
    # copy the outfil to the sis directory
    #
    \cp -f $outfil ${OSF_DATASET}.${extension}
    if ( $status != 0 ) then
       echo `eval ${opustime}`"-ERROR-Failed to copy $i to $INPATH" | tee -a $trailer
       exit $quit_this_dataset
    endif
    ## Make sure new file is same size as old file
    set sizeOrig = `/bin/ls -s $outfil                     | awk '{print $1}'`
    set sizeCopy = `/bin/ls -s ${OSF_DATASET}.${extension} | awk '{print $1}'`
    if ( ${sizeOrig} != ${sizeCopy} ) then
        ## Check that these are same size...how?
        echo `eval ${opustime}`"-ERROR-osc file copy failed "
        echo `eval ${opustime}`"-ERROR-new file ${OSF_DATASET}.${extension} NOT same SIZE as source ${outfil} file "
    endif
    #
    # append the log comments file to the trailer
    #
    echo $outfil | grep -i $logfile
    if ( $status == 0) then
        cat $outfil >> $trailer
    endif    
    #
    # rename the STIM file to .dcx
    #
    echo $outfil | grep -i $stimext
    if ( $status == 0) then
        \cp ${OSF_DATASET}${stimext} ${OSF_DATASET}.dcx
       ## Force failure by changing cp code (test)
       #cp ${OSF_DATASET}.abc ${OSF_DATASET}.dcx
        if ( $status != 0 ) then
            ## cp of osc to dcx failed
            touch ${tmpFile}_$$
            set fCount = `/bin/ls -1 ${tmpFile}_* | wc -l | awk '{print $1}'`
            if ( $fCount <= 10 ) then
                echo `eval ${opustime}`"-WARNING-Must retry copy ${OSF_DATASET}${stimext} to ${OSF_DATASET}.dcx " | tee -a $trailer
                exit $retrigger_me
            else
                echo `eval ${opustime}`"-ERROR-Too many failures on copy ${OSF_DATASET}${stimext} to ${OSF_DATASET}.dcx " | tee -a $trailer
                ## Make sure failure-count files are cleaned up!
                \rm ${tmpFile}_*
                exit $quit_this_dataset
            endif
        endif
        ## Remove any error markers now that we've had success
        \rm ${tmpFile}_*
        ## We may be having problems where copy does NOT fail 
        ## yet DCX file is empty or does not yet exist
        ## Check that these are same size... original OSC file should NOT be zero-length accd'g to Slowinski
        set sizeOrig = `/bin/ls -s ${OSF_DATASET}${stimext} | awk '{print $1}'`
        set sizeCopy = `/bin/ls -s ${OSF_DATASET}.dcx       | awk '{print $1}'`
        if ( ${sizeOrig} != ${sizeCopy} ) then
            ## mark this puppy for retriggering
            touch ${tmpFile}_$$
            set fCount = `/bin/ls -1 ${tmpFile}_* | wc -l | awk '{print $1}'`
            if ( $fCount <= 10 ) then
                echo `eval ${opustime}`"-WARNING-Must retry copy ${OSF_DATASET}${stimext} to ${OSF_DATASET}.dcx " | tee -a $trailer
                exit $retrigger_me
            else
                echo `eval ${opustime}`"-ERROR-Too many failures on copy ${OSF_DATASET}${stimext} to ${OSF_DATASET}.dcx " | tee -a $trailer
                ## Make sure failure-count files are cleaned up!
                \rm ${tmpFile}_*
                exit $quit_this_dataset
            endif
        endif
        ## Remove any error markers now that we've had success
        \rm ${tmpFile}_*
        # guard against quotes 
        # sed -e "s/'//g" -e 's/"//g' ${OSF_DATASET}.dcx
        # guard against no space after the equal sign
        # sed -e "s/=/= /g" ${OSF_DATASET}.dcx
        ## Remove the CR (^M) at end of line
        tr -d '\r' <${OSF_DATASET}.dcx >a.b
        ## Slap a backslash onto the end because keyword package parsing requires it
        sed 's/$/ \//g' a.b >${OSF_DATASET}.dcx
        \rm a.b
    endif    
  end
  if ($redo < $redoMAX) then
    # files weren't ready, retry after pausing to allow them to get there
    @ redo++
    echo `eval ${opustime}`"-INFO-Files NOT ready...Trying again..." | tee -a $trailer
    sleep 15
  endif
end
#
echo `eval ${opustime}`"-INFO---Copyaux ----------completed ${p_dir} -----------------------" | tee -a $trailer
#
if ($loop == 0) then
   # no datasets processed, ERROR
   echo `eval ${opustime}`"-ERROR-No datasets processed by Copyaux" | tee -a $trailer
#  exit $quit_this_dataset
   exit $retrigger_me
endif
#
exit $all_is_well
