#!/bin/sh
#******************************************************************************
#                       ISTP LZPR SOFTWARE
#               Property of the U.S. Government
#                       NASA/GSFC/Code 560
#******************************************************************************
#
# UNIT NAME: lzp_trend
#
# PURPOSE: To transfer files or directories to a remote host
#
# INVOCATION METHOD: lzp_trend [-hdv] [file|directory] [...]
#
# CHANGE HISTORY:
# Author        Change ID  Build/Release   Date      Description of Change
# ------        ---------  ------------- ---------   ---------------------
# R. Wiechert   ISTP       R1.0C         02/06/98    Original Code
# R. Wiechert   ISTP       R1.2A         05/06/98    Ignore core files
# R. Wiechert   ISTP       R1.3          06/16/98    Ignore empty files
#
# NOTES:
#
# This script requires standard FTP client/server processes which issue
# the standard diagnostic messages (in verbose mode) since these messages
# are used to determine overall success/failure for each ftp transaction.
# This script also requires the presence of the standard ".netrc" file in
# order to allow auto-login functions and to specify the trend archive
# host through the use of the standard "macdef" ftp shell command.  By
# changing the PREFIX/SUFFIX START/STOP constants, it is possible to wrap
# the ftp'd filename in a distinct prefix/suffix pair at the start of the
# transaction (to indicate in-progress state) and at the end of the trans-
# action (to indicate finished state).  If you do not wish to use a prefix
# or suffix then simply set the constant to the null string "".
#
#******************************************************************************
trap "echo IGNORED" 1 2 3 15
TREND_LOG=/tmp/lzp_trend_$$.log
TREND_LOCK=.trendlockfile
PREFIX_START=""
SUFFIX_START=".ftp"
PREFIX_STOP=""
SUFFIX_STOP=""
RETURN=1

while getopts hrdv option
do
   case $option in
      r) RENAME="-r" ;;
      d) DEBUG="-d" ;;
      v) VERBOSE="-v" ;;
      *) HELP="-h" ;;
   esac
done

shift `expr $OPTIND - 1`
if [ $# -lt 1 -o "$HELP" = "-h" ]
then
myname=`expr "$0" : '.*/\(.*\)'`
more << END_USAGE_TEXT

USAGE:     $myname [-hdv] [file|directory] [...]

PURPOSE:   Transfers files or directories to a remote host

OPTIONS:   -h      display help screen
           -r      rename after processing
           -d      debug without processing
           -v      enable verbose mode

SYNOPSIS:  The specified files are transferred to the remote
           host flagged as the "trendarchive" host in the
           user's ".netrc" file.  Files are transferred using
           standard binary mode FTP service and removed from
           the local host once the transfer has completed.
           If the -r option is used, files will be renamed by
           stripping one leading dot character.  Directories
           are transferred one file at a time in non-recursive
           fashion; although files are removed, the directory
           and its subdirectories will remain.

NOTES:     The ".netrc" file must be created and maintained
           in the user's home directory on the local host
           and is a standard FTP hook which allows one to
           specify automatic FTP logins for remote hosts
           according to the following prototype:

           machine darkstar login guest password none macdef trendarchive
           cd /export/home/temp/replay

           Each line specifies the automatic login for the
           current user on a different host.  In the above
           example, the current user is automatically
           logged in as "guest" using the password "none"
           whenever ftping to the host "darkstar".  A macro
           called "trendarchive" is defined which, when
           executed, changes the current working directory
           as specified on the next line.  Note that macro
           definitions are terminated with a blank line.
           Beware of extra whitespace which has been known
           to crash certain FTP programs.

           For security reasons, the FTP server will abort
           if the permissions for ".netrc" are not set to
           deny read/write/execute permission to everyone
           except the owner.

END_USAGE_TEXT
exit $RETURN
fi

/usr/bin/chmod go-rwx $HOME/.netrc 2>/dev/null
if [ $? = 0 ]
then
   NETRC=`grep " macdef trendarchive" $HOME/.netrc 2>/dev/null | tail -1`
   TREND_HOST=`expr "$NETRC" : '.*machine \(.*\) login .* password .* macdef trendarchive.*'`
   if [ "$TREND_HOST" ]
   then
      while [ $# -gt 0 ]
      do

         CLEANUP=""
         if [ -d $1 -a -x $1 ]
         then
            cd $1
            if [ $? = 0 ]
            then
               if [ ! -f $TREND_LOCK ]
               then
                  /usr/bin/touch $TREND_LOCK 2>/dev/null
                  if [ $? = 0 ]
                  then
                     CLEANUP=$TREND_LOCK
                     TREND_FILE_LIST=`ls -trd * 2>/dev/null`
                     RETURN=0
                  else
                     echo "ERROR: lzp_trend: $1 not locked"
                     TREND_FILE_LIST=""
                     RETURN=0
                  fi
               else
                  echo "WARNING: lzp_trend: $1 is locked"
                  TREND_FILE_LIST=""
                  RETURN=0
               fi
            else
               TREND_FILE_LIST=""
               echo "ERROR: lzp_trend: $1 does not exist"
            fi
         else
            if [ -f $1 ]
            then
               DIRECTORY=`dirname $1`
               if [ -x "$DIRECTORY" -a "$DIRECTORY" != "." ]
               then
                  cd $DIRECTORY
                  if [ $? = 0 ]
                  then
                     TREND_FILE_LIST=`expr "$1" : '.*/\(.*\)'`
                     RETURN=0
                  else
                     TREND_FILE_LIST=""
                     echo "ERROR: lzp_trend: $1 does not exist"
                  fi
               else
                  TREND_FILE_LIST=$1
                  RETURN=0
               fi
            else
               TREND_FILE_LIST=""
               echo "ERROR: lzp_trend: $1 does not exist"
            fi
         fi

         for TREND_FILE in $TREND_FILE_LIST
         do
            if [ "$TREND_FILE" = "core"  -o ! -s "$TREND_FILE" ]
            then
               /usr/bin/rm -f $TREND_FILE 2>/dev/null
               continue
            fi
            if [ "$RENAME" = "-r" ]
            then
               REMOTE_FILE=`expr "$TREND_FILE" : '\.\(.*\)'`
               if [ ! "$REMOTE_FILE" ]
               then
                  REMOTE_FILE=$TREND_FILE
               fi
            else
               REMOTE_FILE=$TREND_FILE
            fi
            if [ "$VERBOSE" = "-v" ]
            then
               echo "ftp -v $TREND_HOST"
               echo "put $TREND_FILE $PREFIX_START$REMOTE_FILE$SUFFIX_START"
               echo "rename $PREFIX_START$REMOTE_FILE$SUFFIX_START $PREFIX_STOP$REMOTE_FILE$SUFFIX_STOP"
               echo "bye"
            fi
            if [ "$DEBUG" != "-d" ]
            then
               /usr/bin/rm -f $TREND_LOG 2>/dev/null
               /usr/bin/touch $TREND_LOG 2>/dev/null
               /usr/bin/chmod go-rwx $TREND_LOG 2>/dev/null
               if [ $? != 0 ]
               then
                  echo "ERROR: lzp_trend: $TREND_LOG access error"
                  exit $RETURN
               fi

# push file to remote host, use log to detect errors
ftp -v $TREND_HOST << END_OF_FTP_ARGS 1>>$TREND_LOG 2>>$TREND_LOG
binary
\$trendarchive null
put $TREND_FILE $PREFIX_START$REMOTE_FILE$SUFFIX_START
rename $PREFIX_START$REMOTE_FILE$SUFFIX_START $PREFIX_STOP$REMOTE_FILE$SUFFIX_STOP
bye
END_OF_FTP_ARGS

               BIN_MSG=`grep '200 Type set to I.' $TREND_LOG 2>/dev/null`
               CWD_MSG=`grep '250 CWD command successful.' $TREND_LOG 2>/dev/null`
               PORT_MSG=`grep '200 PORT command successful.' $TREND_LOG 2>/dev/null`
               OPEN_MSG=`grep '150 Opening BINARY mode data connection' $TREND_LOG 2>/dev/null`
               XFER_MSG=`grep '226 Transfer complete.' $TREND_LOG 2>/dev/null`
               RNTO_MSG=`grep '250 RNTO command successful.' $TREND_LOG 2>/dev/null`
               ABORT_MSG=`grep 'send aborted' $TREND_LOG 2>/dev/null`
               /usr/bin/rm -f $TREND_LOG 2>/dev/null

               if [ ! "$ABORT_MSG" -a "$BIN_MSG" -a "$CWD_MSG" -a "$PORT_MSG" -a "$OPEN_MSG" -a "$XFER_MSG" -a "$RNTO_MSG" ]
               then
                  /usr/bin/rm -f $TREND_FILE 2>/dev/null
               else
                  /usr/bin/mv -f $TREND_FILE $REMOTE_FILE 2>/dev/null
                  echo "ERROR: lzp_trend: ftp $TREND_FILE"
                  RETURN=1
               fi
            fi
         done

         if [ "$CLEANUP" ]
         then
            /usr/bin/rm -f $CLEANUP 2>/dev/null
            CLEANUP=""
         fi

         shift 1
      done
   fi
else
   echo "ERROR: lzp_trend: .netrc access error"
fi

exit $RETURN
