#!/bin/ksh
################################################################################
#
# Description:
#   Copy a single file to the Mass Storage Facility (MSF).
#
# Usage:
#   msfput [-l logfile] [-m] [-n] [-o] [-q] local-file [remote-file]
#
# Created:
#   01/20/2000: David Sanders (SGI)
#
# History:
#   06/11/2001: Joe Werne (CoRA/NWRA)
#     1. modified to handle both msf and hafs systems.  -o uess msf,
#        otherwise use hafs.
#
################################################################################

###
# Set system-specific environments.
###
SYSTYPE=$(uname)
case ${SYSTYPE} in
  sn6705) PATH=/bin:/usr/bin:/usr/ucb
          export PATH
          if [[ -f /opt/modules/modules/init/sh ]]; then
            . /opt/modules/modules/init/sh
            module load modules nqe 2>/dev/null
          fi
          ;;
     AIX) PATH=${PATH}:/usr/craysoft/nqe/bin
          export PATH
          ;;
  IRIX64) PATH=${PATH}:/usr/craysoft/nqe/bin
          export PATH
          ;;
       *) print "Unknown host system type '${SYSTYPE}'"
          print "Please contact Customer Assistance\n"
          exit 1
          ;;
esac

###
# Initialize some variables.
###
SYSNAME=$(uname -n)
ADDRMSA="hafs1-hippi"
CMD=$(basename ${0})
FUNC="put"
LOGDEF="${HOME}/.msfputlog_${SYSNAME}"
LOGFILE=""
NOWAIT=""
VERBOSE=0

###
# Make sure the MSFHOME variable is set.
###
if [[ -z "${MSFHOME}" ]]; then
   print "${CMD}: Environment variable \$MSFHOME is not set.\n"
   exit 1
fi

###
# Parse the command line.
###
while getopts :l:omnq OPT; do
   case ${OPT} in
      l) LOGFILE="${OPTARG}";;
      o) ADDRMSA="msf-hippi";;
      m) FUNC="moveout";;
      n) NOWAIT="-nowait";;
      q) VERBOSE=1;;
     \?) print "Usage: ${CMD} [-l logfile] [-o] [-m] [-n] [-q] local-file [remote-file]\n"
         exit 1;;
   esac
done

###
# Make sure a local file is specified.
###
shift $(expr ${OPTIND} - 1)
if [[ ${#} -eq 0 ]]; then
   print "\nLocal file name not specified."
   print "Usage: ${CMD} [-l logfile] [-o] [-m] [-n] local-file [remote-file]\n"
   exit 1
fi
LOCFILE="${1}"

###
# Set up the remote file name.
###
if [[ ${#} -ge 2 ]]; then
   REMFILE="${2}"
else
   REMFILE=${MSFHOME}/$(basename "${LOCFILE}")
fi

###
# Force the remote file name to have a full path.
###
TEMP=$(print ${REMFILE} | cut -c 1)
if [[ "${TEMP}" != "/" ]]; then
   REMFILE=${MSFHOME}/${REMFILE}
fi

###
# Set up the log file.
###
if [[ -n "${LOGFILE}" ]]; then
   rm -f ${LOGFILE} 1>/dev/null 2>&1
   cat /dev/null >${LOGFILE}
   ERR=${?}
   if [[ ${ERR} -ne 0 ]]; then
      print "${CMD}: Unable to create log file '${LOGFILE}'"
      print "Using default log file '${LOGDEF}' instead."
      LOGFILE=${LOGDEF}
      rm -f ${LOGFILE} 1>/dev/null 2>&1
      cat /dev/null >${LOGFILE}
   fi
else
   LOGFILE=${LOGDEF}
   rm -f ${LOGFILE} 1>/dev/null 2>&1
   cat /dev/null >${LOGFILE}
fi

###
# Execute the transfer.
###
REQID=$(rft -function ${FUNC} -host ${ADDRMSA} -nopassword -type binary \
            -trace -debug ${NOWAIT} ${LOCFILE} ${REMFILE} 2>>${LOGFILE})
STATUS=${?}
if [[ -z "${NOWAIT}" ]]; then
   if [[ ${STATUS} -eq 0 || ${STATUS} -eq 255 ]]; then
      if [[ ${VERBOSE} -eq 0 ]]; then
         print "File transfer succeeded - ${LOCFILE}"
      fi
   else
      if [[ ${VERBOSE} -eq 0 ]]; then
         print "File transfer error ${STATUS} - ${LOCFILE}"
      fi
   fi
else
   if [[ ${STATUS} -eq 0 ]]; then
      if [[ ${VERBOSE} -eq 0 ]]; then
         print "File transfer queued - ${LOCFILE}"
      fi
   else
      if [[ ${VERBOSE} -eq 0 ]]; then
         print "File transfer queue error ${STATUS} - ${LOCFILE}"
      fi
   fi
fi

###
# Clean up and exit.
###
exit ${STATUS}

