#!/bin/ksh
####  UNIX Script Documentation Block
#
# Script name: exsend_codes2web.sh.sms
#
# Abstract: This script will use rsync to sync the /nwprod codes on 
#           the CCS to the NCO web site
#
# Script history log:
# 2010-05-12  Patrick O'Reilly  original version for implementation
#
# Attributes:
#
#   Language: /bin/sh script
#   Machine:  IBM SP
####

set -xa

# Turn on rsync program debug output.  Very verbose!
RSYNC_DEBUG=1

# Flag to do a test of the rsync program without actually transmitting any files.
# 0=actual run
# 1=run in test mode, show what will be done without doing anything.
TEST_MODE=0

# awk
AWK=awk

# GMT date
ZDATE="date -u"

# Separator line 
LINE="**********************************************************************"

# RSYNC command variables
REMOTE="${destUser}@${destHost}"
DEST="${REMOTE}:${destWebsiteDir}"
SRC="/gpfs/${CCS}/nco/ops/nwprod"

# Options for the rsync program.
RSYNC_OPTS="-l -p -e /usr/bin/ssh -r --stats --delete -W -u --exclude-from=${TABLEDIR}/send_codes2web.inc"

#### Begin Options Checking
# Add debug flags if debug was specified.
if [ $RSYNC_DEBUG -eq 1 ]; then
  RSYNC_OPTS="-v -v $RSYNC_OPTS"
fi

# Add test flag is test mode specified.
if [ $TEST_MODE -eq 1 ]; then
  RSYNC_OPTS="--dry-run $RSYNC_OPTS"
fi
#### End Options Checking

#### Functions Section
#

# Function used to log script messages to the specified
# logging facility.
log_it()
{
  SEVERITY=$1
  MESSAGE=$2

  case "$SEVERITY" in
    info|warn|debug) echo "[`${ZDATE}`] $MESSAGE"
            ;;
    err) echo "[`${ZDATE}`] $MESSAGE"
         err_exit $MESSAGE
         ;;
    summary) SUMMARY="${SUMMARY}\n${MESSAGE}"
         ;;
  esac
}

#### End Functions Section

# Make sure the utility programs and files can be found
for aprog in $SSH $RSYNC err_exit
do
  log_it info "Checking for $aprog..."
  if [ -f $aprog ]; then
    log_it info "  Found $aprog"
  else
    log_it err "Missing $aprog. Cannot continue."
  fi
done

log_it summary "${LINE}"

log_it info "Checking to see if we can log in to $REMOTE ..."
log_it info "  Running: $SSH $REMOTE \"pwd\""

$SSH $REMOTE "pwd"
if [ $? -eq 0 ]; then
  log_it info "All conditions met for synchronizing with $REMOTE."
  log_it info "  Running: $RSYNC $RSYNC_OPTS $SRC $DEST"
  toretry=3
  while [ $toretry -gt 0 ]
   do
    $RSYNC $RSYNC_OPTS $SRC $DEST >> standout 2> errfile
    rsyncerrcond=$?
    if [ "$rsyncerrcond" -eq 30 ]; then
      toretry=$(($toretry-1))
      if [ "$toretry" -eq 0 ]; then
       echo "Sync Failed - Retry count exceeded"
       log_it err "Sync Failed - Retry count exceeded"
       exit 42
      fi
      echo "RSYNC Timeout retrying $toretry more times"
      log_it info "RSYNC Timeout retrying ${toretry} more times"
      # add jlog entry here
      msg="SEND_CODES2WEB: RSYNC Timeout `date` "
      postmsg "$jlogfile" "$msg"
      # timeout occured (must have timeout option set)
    elif [ "$rsyncerrcond" -gt 0 ]; then
      log_it summary "Sync failed."
      log_it err "Error doing $RSYNC of $SRC to ${DEST}."
      exit 30
    else
      log_it summary "Sync successful."
      toretry=0
    fi
  done
else
  log_it err "Remote at $REMOTE denying login attempts."
  exit 35
fi

log_it summary "${LINE}"
log_it info "${SUMMARY}"

#####################################################################
# GOOD RUN
set +x
echo " "
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " "
set -x
#####################################################################

exit 0
