#!/bin/sh -x
##########################################################################
#
# routine: cdbpsh.sh 
#
# purpose: Script to push a CDBS delivery to a remote site.
#
#       The release catalog and reference files are assumed to
#       already reside in a set of gzipped tar files and the files are FTPd to 
#       the remote site.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 11/09/01 44616  MSwam     first version
# 04/23/02 44741  WMiller   use getpasswd
# 11/15/02 46981  MSwam     make copy optional through DO_COPY resource parm
# 04/11/03 48141  MSwam     move cleanup and wait to another process
# 12/10/10 65423  MSwam     use ftpcopy, not netcopy
# 04/20/11 64748  MSwam     changes needed to deal with possible multiple tar files
# 
##########################################################################
banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
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
                       #  the process to try another dataset
all_is_well=0
netcpy_no_files=5
#
# if no copying , then no action needed
if [ $DO_COPY != "YES" ]
then
  echo "Copying has been turned off (DO_COPY != YES)."
  exit $all_is_well
fi

# get passwd
#
# turn off variable echo
set +x
enc_pwd=`getpasswd.ksh $*`
# verify usage
if [ $? != 0 ]
then
  echo "usage: cdbpsh.sh -v enc_passwd"
  exit $go_absent
fi
set -x

#
# TIME TRIGGER ONCE: This verifies the passwd once after startup
# 
if [ $EVENT_TYPE = "EVENT_TIME_EVENT" ]
then
      set +x
      ftpcopy -pw $enc_pwd -rm $RMT_MACHINE -rt $RMT_MACHINE_TYPE -rl $RMT_LOGIN -te $TEMP_DIR -rf zzz\*zzz -lf $CDB_IN_DIR -pg GET
      savestat=$?
      set -x
#     test for NO_FILES_FOUND (means connection succeeded)
      if [ $savestat = $netcpy_no_files ]
      then
#        connection successful, all done until real OSF event occurs
         exit $all_is_well
      else
#        ERROR occurred, write message and go absent
         echo "ERROR: Failed initial NETCOPY connect to $RMT_MACHINE,$RMT_LOGIN; exit=$savestat"
         exit $go_absent
      fi
fi

#
# OSF TRIGGER EVERY OTHER TIME: Normal mode of processing 
#
# resolve stretch for UNIX
cdbdir=`osfile_stretch_file $CDB_IN_DIR`
if [ ! -d $cdbdir ] 
then
   echo "ERROR: $cdbdir directory does not exist"
   exit $go_absent
fi
cd $cdbdir
if [ $? != 0 ]
then
   echo "ERROR: Failed to set directory to $cdbdir"
   exit $go_absent
fi
localdir=`pwd`
echo "sitting in $localdir"

# set trailer file name
trl=$localdir/${OSF_DATASET}.trl
echo "CDBPSH================verifying gzipped tar files exist" >> $trl

# verify tar file of catalog and ref files exists
tarfile_base=$localdir/${OSF_DATASET}.tar.gz_
tarfile_count=`ls -1 ${tarfile_base}* | wc -l`
if [ ! ${tarfile_count} ]
then
   echo "ERROR: Failed to find tar packages for ${tarfile_base}" | tee -a $trl
   exit $quit_this_dataset
fi

#
# copy the tar file to the remote site (using SAFE_COPY
#   since they poll for this file)
#
echo "ftpcopy -rm $RMT_MACHINE -rt $RMT_MACHINE_TYPE -rl $RMT_LOGIN -ds $DISK_SLOP_PERC -ft $FILE_TYPE -te $TEMP_DIR -rf ${RMT_DIRECTORY}/ -delete_after_copy -safe_copy -lf ${tarfile_base} -pg $PUT_OR_GET -pw XXXXXX" | tee -a $trl
set +x
ftpcopy -rm $RMT_MACHINE -rt $RMT_MACHINE_TYPE -rl $RMT_LOGIN -ds $DISK_SLOP_PERC -ft $FILE_TYPE -te $TEMP_DIR -rf ${RMT_DIRECTORY}/ -delete_after_copy -safe_copy -lf "${tarfile_base}*" -pg $PUT_OR_GET -pw $enc_pwd 
save_stat=$?
set -x
if [ $save_stat != 3 ]
then
       echo "ERROR: Failed ftpcopy; status = $save_stat" | tee -a $trl
       echo "Unable to send ${tarfile} to $RMT_DIRECTORY" | tee -a $trl
       exit $quit_this_dataset
else
       echo "ftpcopy complete." | tee -a $trl
fi

#
# complete, exit success
exit $all_is_well
