#!/bin/sh -x
################################################################################
#
# routine: retxxx.sh 
#
# purpose: Script to copy files to DADS staging directory as listed
#	   in an OTFC_response.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  ----------------------------------------------------
# 09/22/98 36848  MSwam     first version
# 10/20/98 36860  MSwam     use -pw option for netcopy 
# 12/04/98 35081    MSwam   new format for request filename
# 04/09/99 38834  MSwam     add banner print
# 03/16/00 40129_03 MSwam   change EVNT to EVENT for OAPI
# 09/12/01 44327  MSwam     add fitsverify on output
# 12/11/01 44393  MSwam     if no FITS files, exit
# 04/23/02 44741  WMiller   use getpasswd
# 07/01/02 46074  MSwam     add remote_disk_full exit
# 05/27/03 45889  MSwam     allow resource control of fits check
# 06/10/02 43915  WMiller   use cp in place of netcopy
# 06/03/03 43915  Sherbert  Incorporate 45889 resource control of fits check
# 11/13/03 49769  jschultz  modifications to utilize a new opus_fitsverify 
#                           tool. This new tools is a modification of the 
#                           original fitsverify tool provided by HEASARC at 
#                           GSFC.  The opus_fitsverify tool supports an 
#                           additional -s command line option which is not
#                           directly supportd by the original fitsverify tool.
#                           The additional option makes the error checking 
#                           performed by tool more flexible.
#                           The command line arguments passed to the tool are
#                           determined from environment variables which get 
#                           set to values specified in the process resource 
#                           file
# 06/10/08 60170 MSwam      Rename files out the door if DADS is expecting
#                           something different, according to ingest_long_ext
# 07/25/08 60382 FHamilton  added multiple instrument repro capability
# 08/14/08 60382 MSwam      Back out last 60170 change.
# 09/22/08 59821 MSwam      Oiy.  For now, back out 60382 changes (for 2008.4)
#                             and pick them up for 2009.1.
# 09/22/08 60382 MSwam      And now put the 60382 changes back in.
################################################################################
echo $banner
date
#
go_absent=102          #this exit status is in the FATAL range for XPOLL
quit_this_dataset=1    #this exit status is mapped and will allow
all_is_well=0
no_fits_files=2
netcpy_no_files=5
#
# OSF TRIGGER: Normal mode of processing 
#
# parse dataset,directory from OSF field
p_dset=`echo ${OSF_DATASET} | awk '{n=split($1,a,"_");print a[2]}'`
p_dir=$OSF_DATASET
#
# resolve stretch for UNIX
caldir=`osfile_stretch_file $OUTPATH`
if [ ! -d $caldir/$p_dir ] 
then
   echo "ERROR: $caldir/$p_dir directory does not exist"
   exit $go_absent
fi
cd $caldir/$p_dir
if [ $? != 0 ]
then
   echo "ERROR: Failed to set directory to $caldir/$p_dir"
   exit $go_absent
fi
localdir=`pwd`
echo "Sitting in $localdir"

# verify output files exist
fset=`ls -1 *.fits`
if [ $? != 0 ]
then
     echo "ERROR: No fits files found to return."
     exit $no_fits_files
fi
#
# perform fits check, unless suppressed
if [ ${FITS_VERIFY:-1} != "TRUE" ]
then
    if [ ${FITS_VERIFY:-1} != "FALSE" ]
    then
       echo "ERROR: FITS_VERIFY mnemonic not present in resource file"
       exit $go_absent
    fi
fi
#
if [ $FITS_VERIFY != "FALSE" ]
then
  # set command line options based on resource file mnemonics
  case $FITS_VERIFY_LEVEL in
    e|E) verify_level=-e;;
    s|S) verify_level=-s;;
  esac
  case $FITS_VERIFY_REPORT in
    q|Q) verify_report=-q;;
  esac   
  # run opus_fitsverify for every FITS file to be sent back to DADS
  for i in $fset
  do
     opus_fitsverify $verify_level $verify_report $i
     if [ $? != 0 ]
     then
          echo "ERROR: opus_fitsverify FAILED on $i"
          exit $quit_this_dataset
     fi
  done
fi
#
# parse the DADS directory from the request for copying datasets back to DADS
DADS_DIR=`awk -F= '/DIRECTORY/ {print $2}' ./${OSF_DATASET}$REQUEST_FILEEXT`
echo "DADS_DIR= $DADS_DIR"
#
# list files for log (copy all FITS files back to DADS)
ls -l $localdir/*.fits
#
# instrument determines some special file renaming
instr=`echo ${p_dset} | awk '{a=substr($0,1,1);print a}'`

# copy all FITS files back to DADS, translating names where DADS wants
# a short extension
for i in $fset
do
  DADSname=$i   # no name translations needed at present
  #
  cp $localdir/$i $DADS_DIR/$DADSname
  save_stat=$?
  if [ $save_stat != 0 ]
  then
       echo "ERROR: Failed file copy; status = $save_stat"
       exit $quit_this_dataset
  else
       echo "File copy complete."
  fi
  # TEMPORARY: open up permissions for IDR rename
  chmod go+rw $DADS_DIR/$DADSname
  save_stat=$?
  if [ $save_stat != 0 ]
  then
       echo "WARNING: Failed file permissions change; status = $save_stat"
  else
       echo "File permissions change complete."
  fi
done
#
# Get the archive request account name to see if this is the repro account
#
p_otfcid=`echo $OSF_DATASET | awk '{n=split($1,a,"_");print a[1]}'`
atest=`echo $p_otfcid | grep -i ${REPRO_ACCOUNT}`
save_stat=$?
if [ $save_stat = 0 ]
then
#
# list files for log (copy all FITS files to reprocessing pipeline for ingest)
   ls -l $localdir/*.fits
#
# copy all FITS files to the repro science pipeline, except for WF2, which is 
# copied over to the science repro pipe by f2gxxx so that Multiple 
# Extension FITS is copied instead of the wFITS available to retxxx
#

#
# Get the instrument from OSF to determine where to copy the data for ingest
#
   ipppssoo=`echo $p_dset | awk '{a=substr($1,1,8); print a}'`

   if [ $instr = 'i' ] 
   then 
      cp $localdir/*.fits $WF3_REPING_DIR
      chmod go+rw $WF3_REPING_DIR/$ipppssoo*
   fi
   if [ $instr = 'j' ] 
   then
      cp $localdir/*.fits $ACS_REPING_DIR
      chmod go+rw $ACS_REPING_DIR/$ipppssoo*
   fi
   if [ $instr = 'l' ] 
   then
      cp $localdir/*.fits $COS_REPING_DIR
      chmod go+rw $COS_REPING_DIR/$ipppssoo*
   fi  
   if [ $instr = 'n' ] 
   then
      cp $localdir/*.fits $NIC_REPING_DIR
      chmod go+rw $NIC_REPING_DIR/$ipppssoo*
   fi 
   if [ $instr = 'o' ] 
   then
      cp $localdir/*.fits $STI_REPING_DIR
      chmod go+rw $STI_REPING_DIR/$ipppssoo*
   fi  

   save_stat=$?
   if [ $save_stat != 0 ]
   then
          echo "ERROR: Failed file copy/chmod for ingest (WF2 data excluded); status = $save_stat"
          exit $quit_this_dataset
   else
          echo "Ingest file copy/chmod complete (except for WF2 data)."
   fi

   ipppssoot=`osf_test -p $REPRO_SCI_PATH -f $ipppssoo\? -pr dataset`
   repro_data_id=`osf_test -p $REPRO_SCI_PATH -f $ipppssoot -pr data_id`

# 
# Create an OSF in the reprocessing science pipeline to trigger ingest there
#
   echo "Testing $REPRO_SCI_PATH OSF."
   
   if [ $repro_data_id = "" ] 
   then
           echo "ERROR: unable to test $REPRO_SCI_PATH OSF for data_id"
           exit $quit_this_dataset
   fi
   echo "Creating $REPRO_ING_PATH OSF."
   osf_create -p $REPRO_ING_PATH -f $ipppssoot -t $repro_data_id -n 000 -c RQ -s w
   save_stat=$?
   if [ $save_stat != 0 ] 
   then
           echo "ERROR: unable to create OSF in $REPRO_ING_PATH"
           exit $quit_this_dataset
   fi
fi
#
# complete, exit success
exit $all_is_well
