#!/bin/tcsh -fx
##########################################################################
#
# routine: podump.csh
#
# purpose: Move HLA PODs into HLA's dct directory.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 11jul12  70739  FHam      Initial version
# 09/10/12 70213  MSwam     use tcsh
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
/bin/date
#
set go_absent = 102          #this exit status is in the FATAL range for XPOLL
set all_is_well = 0

# get the name of the directory where HLA puts PODs
set poddir = ${HLA_POD_IN}
set pod_in_dir = ${MV_PODS_HERE}

# move to working directory
if (! -e $poddir) then
   echo "ERROR: $poddir directory does not exist"
   echo $banner
   exit $go_absent
endif
cd $poddir
if ($status != 0) then
   echo "ERROR: Failed to set directory to $poddir"
   echo $banner
   exit $go_absent
endif

# check to see if there are PODs to move

ls ${poddir}*.pod

if ($status == 1) then
   echo "No PODs found to move."
   exit $all_is_well
endif

# sleep for 10 seconds to allow any PODs being copied in can complete

sleep 10

# move the PODs!

foreach i (`ls *.pod`)

# check to make sure there is not already a pod file in the dct directory

  set podname=`echo $i | /bin/awk '{n=split($1,a,".");print a[1]}'`
  ls ${pod_in_dir}${podname}*

# if not, move the pod

  if ($status == 1) then

# now verify that none of the observations in the pod file have
# osfs in the hla pipe

# write SQL to a file
#
      echo "select distinct ipppssoot from podnames where podname = '""${podname}""'" > sqlTemp$$
      echo "go -m vert" >> sqlTemp$$
#
      isql -S${OPUS_SERVER} -D${OPUS_DB} -i sqlTemp$$ > ${podname}.out

# get a list of OSFs in the hla pipe

      osf_test -p hla > ${podname}_osfs.out

# loop through the set of ipppssoots in the sql output file
# if no ipppssoots are found in the pipe, move the pod

      foreach j (`egrep "ipppssoot:" ${podname}.out | awk '{print $2}'`)
        echo $j
        setenv count `grep -i $j ${podname}_osfs.out | wc -l`
        if ( $count != 0 ) then
           echo "$j is already in the pipe, moving on."
        else
           echo "Moving $podname"
           /bin/mv -f $i $pod_in_dir
        endif
      end
      rm sqlTemp$$ ${podname}.out ${podname}_osfs.out
  endif
end

exit $all_is_well
