#!/bin/tcsh -fx
##########################################################################
#
# routine: copyaux_noslash.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
# 02/15/07 57062  MSwam     Remove end-of-line / for .lab file
# 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
# 09/10/12 70213  MSwam     use tcsh
# 
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
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 all_is_well = 0
#
# resolve stretches for UNIX
set sisdir = `osfile_stretch_file $OUTPATH`
#
# 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 " "
echo "Copyaux ---" `date +"%d-%b-%Y %T"` "-------began-----------------------" | tee -a $trailer
#
#
# copy the files
set redo = 0
set redoMAX = 10
set loop = 0
set poddir = `osfile_stretch_file $INPATH`
while ($redo < $redoMAX)
  # 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 "ERROR: Failed to copy $i to $INPATH" | tee -a $trailer
       exit $quit_this_dataset
    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
        # 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
        tr -d '\r' <{$OSF_DATASET}.dcx >a.b
        # sed 's/$/ \//g' a.b >{$OSF_DATASET}.dcx
        \mv a.b {$OSF_DATASET}.dcx
    endif    
  end
  if ($redo < $redoMAX) then
    # files weren't ready, retry after pausing to allow them to get there
    @ redo++
    echo "Files NOT ready...Trying again..."
    sleep 10
  endif
end
#
echo "Copyaux ---" `date +"%d-%b-%Y %T"` "----completed-------------------------" | tee -a $trailer
#
if ($loop == 0) then
   # no datasets processed, ERROR
   echo "ERROR: No datasets processed by Copyaux" | tee -a $trailer
   exit $quit_this_dataset
endif
#
exit $all_is_well
