#!/bin/tcsh -fx
##########################################################################
#
# routine: polxxx.csh 
#
# purpose: Script to initiate the OTFC (OnTheFlyCalibration) pipeline.
#
#	Polls for files in the incoming retrieval directory and
#	if one is found, this command procedure will be
#	invoked.  All it does is create an observation status 
#	file (OSF) so that processing for that observation
#	will proceed.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/22/98 36848    MSwam   first version
# 11/24/98 36848_01 MSwam   qualify tr with /bin/tr
# 12/04/98 35081    MSwam   new format for request filename
# 02/19/99 38284_02 MSwam   echo the contents of each OTFC request
# 04/09/99 38834    MSwam   add banner print
# 06/09/09 38834_01 MSwam    fix banner print
# 12/23/99 39366    MSwam   add call to fill otfc_stats relation
# 09/09/00 43165    MSwam   convert to OTFR
# 02/14/01 43165_01 MSwam   adjust ASN ending character test   
# 12/03/01 44598    MSwam   handle all SIs in a single script
# 06/03/02 43915    WMiller Support IDR
# 07/02/02 45760    Sontag  change $status checks to look for '!= 0'
# 08/21/03 48370    Sherbert Add missing /bin/ qualifier to cat, date
# 09/03/03 47754    Sherbert Add check for empty *.podlist
# 09/29/03 43915    Sherbert Change OSF data_id from "req" to "dup" or "bad"
# 10/19/04 50663    MSwam    Add EPC linking, cos/wfc3 support
# 08/11/05 53803    MSwam    Replace PATH_FILE_NAME with PATH_BASENAME
# 01/20/09 64274    MSwam    Use a-z/A-Z for tr (upper/lower not in UCB version)
# 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
#
# split the OTFC_request filename into the timestamp and dataset
#
set basename = $EVENT_NAME
set basename = $basename:t
set e_dir = `cat $EVENT_NAME | grep DIRECTORY | awk -F= '{print $2}'`
set p_time = `echo $basename:r | awk '{n=split($1,a,"_");print a[1]}'`
set p_dset = `echo $basename:r | awk '{n=split($1,a,"_");print a[2]}'`
set p_dir = ${p_time}_${p_dset}
#
# create a request-specific directory under the calibration root dir
if (! -e $OUTPATH) then
   echo "ERROR: OUTPATH directory does not exist"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
   if ( $status != 0 ) then
        echo "ERROR: unable to create OSF for duplicate request"
   endif
   exit $go_absent
endif
cd $OUTPATH
if ($status != 0) then
   echo "ERROR: Failed to set directory to OUTPATH"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
   if ( $status != 0 ) then
        echo "ERROR: unable to create OSF for duplicate request"
   endif
   exit $go_absent
endif
if (-e ${p_dir}) then
   # the directory already exists, so this is a DUPLICATE request
   echo "ERROR: DUPLICATE REQUEST: $p_dir directory already exists under $OUTPATH"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
   if ( $status != 0 ) then
        echo "ERROR: unable to create OSF for duplicate request"
   endif
   exit $quit_this_dataset
endif
mkdir $p_dir
if ($status != 0) then
   echo "ERROR: Failed to create OTFC directory $OUTPATH/$p_dir"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
   if ( $status != 0 ) then
        echo "ERROR: unable to create OSF for duplicate request"
   endif
   exit $go_absent
endif
cd $p_dir

#
# echo the contents of the OTFC request & podlist file for later tracing
set event_name = $EVENT_NAME
set podlist = ${event_name:r}.podlist
cat $EVENT_NAME
cat $podlist
#                                                          <
# Exit with error if file is 0-length
if ( -z $podlist ) then
   echo "ERROR: $podlist seems to be empty! "
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t bad -s e -n 888
   exit $quit_this_dataset   #generates a "_bad" extension on triggering file
endif

#
# copy the OTFC_request to a local file
\cp $EVENT_NAME .
if ($status != 0) then
   # failure to copy a request to the local disk could be a temporary
   # problem, quit this request but allow others to be processed
   echo "ERROR: Failed to copy $EVENT_NAME to local directory"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t bad -s b -n 888
   if ( $status != 0 ) then
        # failure to create an OSF is likely a problem for ALL requests
        # so go absent to get attention
        echo "ERROR: unable to create OSF for failed copy of request"
        exit $go_absent
   endif
   exit $quit_this_dataset   #generates a "_bad" extension on triggering file
endif
#
# copy the .podlist file to a local file
\cp $podlist .
if ($status != 0) then
   # failure to copy a podlist file to the local disk could be a temporary
   # problem, quit this request but allow others to be processed
   echo "ERROR: Failed to copy $podlist to local directory"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t bad -s b -n 888
   if ( $status != 0 ) then
        # failure to create an OSF is likely a problem for ALL requests
        # so go absent to get attention
        echo "ERROR: unable to create OSF for failed copy of podlist"
        exit $go_absent
   endif
   exit $quit_this_dataset   #generates a "_bad" extension on triggering file
endif
set local_request = $basename

# ensure that the request is not an empty file
set lines = `cat $local_request | wc -l`
if ($lines == 0 ) then
   # an empty request is a big problem
   echo "ERROR: empty request file $local_request"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t bad -s b -n 888
   if ( $status != 0 ) then
        # failure to create an OSF is likely a problem for ALL requests
        # so go absent to get attention
        echo "ERROR: unable to create OSF for failed copy of request"
        exit $go_absent
   endif
   exit $quit_this_dataset   #generates a "_bad" extension on triggering file
endif

#
# create a link to each POD file
# 
set input_files = `\ls $e_dir/*pod`
foreach file ( $input_files )
   set new_file = `basename $file`
   ln -s $file ${new_file:r}.pod
   if ( $status != 0 ) then
      echo "ERROR: unable to link POD file $file to $new_file:r.pod in $PWD"
      # create an error OSF for the request
      osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
      if ( $status != 0 ) then
           echo "ERROR: unable to create OSF for duplicate request"
      endif
      exit $quit_this_dataset  #generates a "_bad" extension on triggering file
   endif
end

#
# create a link to each EDT set file
#
set input_files = `\ls $e_dir/*.??[xi]`
foreach file ( $input_files )
   ln -s $file
   if ( $status != 0 ) then
      echo "ERROR: unable to link EDT file $file in $PWD"
      # create an error OSF for the request
      osf_create -p $PATH_BASENAME -f {$p_dir} -t dup -s d -n 999
      if ( $status != 0 ) then
           echo "ERROR: unable to create OSF for duplicate request"
      endif
      exit $quit_this_dataset  #generates a "_bad" extension on triggering file
   endif
end

#
# create a link to each EPC file
#
set input_files = `\ls $e_dir/*_epc.fits`
foreach file ( $input_files )
   ln -s $file
   if ( $status != 0 ) then
      echo "ERROR: unable to link EPC file $file in $PWD"
      # create an error OSF for the request
      osf_create -p $PATH_BASENAME -f {$p_dir} -t req -s d -n 999
      if ( $status != 0 ) then
           echo "ERROR: unable to create OSF for failed EPC link creation"
      endif
      exit $quit_this_dataset  #generates a "_bad" extension on triggering file
   endif
end

#
# create the observation status files for this OTFC_request by parsing
# the "DATASET_NAME" entries from the request
#
set count = 0
foreach i (`grep DATASET_NAME ./$local_request | awk '{n=split($1,a,"=");print a[2]}'| tr '[A-Z]' '[a-z]'`)
    #
    # determine instrument by first character of ipppssoot
    set firstchar = `echo $i | awk '{a=substr($1,1,1); print a}'`
    if ($firstchar =~ [n]) then
      set datatyp = nic
    else if ($firstchar =~ [o]) then
      set datatyp = sti
    else if ($firstchar =~ [u]) then
      set datatyp = wf2
    else if ($firstchar =~ [j]) then
      set datatyp = acs
    else if ($firstchar =~ [l]) then
      set datatyp = cos
    else if ($firstchar =~ [i]) then
      set datatyp = wf3
    else
      echo "ERROR: Unknown instrument for $i"
      exit $quit_this_dataset
    endif
    osf_create -p $PATH_BASENAME -f {$p_time}_{$i} -t $datatyp -s $OSF_START_STATUS -n 000
    if ( $status != 0 ) then
        # failure to create an OSF is likely a problem for ALL requests
        # so go absent to get attention
        echo "ERROR: unable to create OSF for $i"
        exit $go_absent
    else
        # create a trailer
        echo "OTFRBEG ---" `\date +"%d-%b-%Y %T"` "--------------------------------" > $p_dir.trl
        #
        # echo the contents of the OTFC request for later tracing
        cat $EVENT_NAME >> $p_dir.trl
        set count = 1
    endif
end
#
# check for no OSFs created
if ($count == 0) then
   # possibly a corrupted request file
   echo "ERROR: No OSFs created for request $EVENT_NAME"
   # create an error OSF for the request
   osf_create -p $PATH_BASENAME -f {$p_dir} -t bad -s b -n 888
   if ( $status != 0 ) then
        # failure to create an OSF is likely a problem for ALL requests
        # so go absent to get attention
        echo "ERROR: unable to create OSF for bad request"
        exit $go_absent
   endif
   exit $quit_this_dataset
endif

#
# register this request in the otfr_request_stats db table
db_otfr_stats_po.pl $EVENT_NAME

#
# complete, exit success
exit $all_is_well
