#!/bin/ksh
#############################################################
# Program:	eStorLink.sh
#
# Description:	Unix-to-Unix connection program.
#               This program provides end-to-end communication
#		functions with confirmations
#
# Author:	Dana French (dfrench@aignetplex.com)
#		Applied Intelligence Group
#		(405) 936-2342
#               Copyright 1999
#
# Date:		05/24/99
#
#############################################################
# Modifications:
#
# 05/24/99	Original code		Version: 1.0
#############################################################
rcommand()
{
    echo "${1}" | tee -a "${UU_OUTPUT_FILE}"
}
#############################################################
lcommand()
{
    rcommand "echo \"${1}\" >> /tmp/eStorLink\${$}.tmp"
}
#############################################################
lcomment()
{
    rcommand "echo \"# \${TIMESTAMP}|\${UU_LOCAL_UUNAME}|\${UU_LOCAL_DIR}/\${UU_LOCAL_FNAME}|\${UU_REMOTE_UUNAME}|\${UU_REMOTE_DIR}/${UU_REMOTE_FNAME}|\${UU_NOTIFY_USER}|\${UU_OVERWRITE}|\${UU_SENDNOW}|\${UU_CURRENT_UUNAME}|${1}\" | tee -a \${UU_LOGFILE} >> /tmp/eStorLink\${$}.tmp"
}

#############################################################
rcomment()
{
    rcommand "echo \"# \${TIMESTAMP}|\${UU_LOCAL_UUNAME}|\${UU_LOCAL_DIR}/\${UU_LOCAL_FNAME}|\${UU_REMOTE_UUNAME}|\${UU_REMOTE_DIR}/${UU_REMOTE_FNAME}|\${UU_NOTIFY_USER}|\${UU_OVERWRITE}|\${UU_SENDNOW}|\${UU_CURRENT_UUNAME}|${1}\" >> \${UU_LOGFILE}"
}
#############################################################
llogfile()
{
    lcommand "echo \\\"# \\\${TIMESTAMP}|\\\${UU_REMOTE_UUNAME}|\\\${UU_REMOTE_DIR}/\\\${UU_REMOTE_FNAME}|\\\${UU_LOCAL_UUNAME}|\\\${UU_LOCAL_DIR}/\\\${UU_LOCAL_FNAME}|\\\${UU_NOTIFY_USER}|\\\${UU_OVERWRITE}|\\\${UU_SENDNOW}|\\\${UU_CURRENT_UUNAME}|${1}\\\" >> \\\${UU_LOGFILE}"
}
################################################################
usage()
{
cat << _END_
Usage:

  estorlink.sh -l localfile -r remotefile ( -g host | -p host ) [-o]
	[-a transmit|confirm|hold|receive]
	[-f file|print|mail|news]
	[-b ascii|binary]
	[-c comment]
	[-t now|##:##|date/time]

  Options are:

  -l localfile  : Full path name of local file

  -r remotefile : Full path name of remote file

  -m email  : Email address of person to notify upon completion

  -g A:B:C  :	List of hosts from which to "get" a file

  -p A:B:C  :	List of host on which to "put" a file

  -a action :	Action of this command. Choices are:
		transmit
		hold
		confirm
		receive

  -f function :	Function of this command.  Choices are:
		file
		print
		mail
		news

  -b type :	Type of this command.  Choices are:
		ascii
		binary
		
  -o :		If destination file exists, overwrite it.

  -t :		Send now
		( default is spool and wait for cron )

  -h :		Print this info text

  Version 1.0: May 25, 1999
_END_
exit 1
}
################################################################
TIMESTAMP=`date +"%Y%m%d%H%M%S"`
DEV_NULL=/dev/null
UU_PROGRAMID="pgm2035"			# (program ID number)
UU_OUTPUT_TYPE="${UU_OUTPUT_TYPE:-log}"	# (Type of output to display to user)
UU_ACTION="${UU_ACTION:-transmit}"	# (Action to perform on this file at the origination site)
UU_FUNCTION="${UU_FUNCTION:-file}"	# (Function of the file to be transfered)
UU_TYPE="${UU_TYPE:-ascii}"		# (type of file transfer mechanism to use with this file)
UU_SENDNOW="0"				# (0=do-not-send-now  1=send-now)
UU_LOCAL_UUNAME="`uname -n`"
UU_LOCAL_UUNAME="${UU_LOCAL_UUNAME%%.*}"
UU_CURRENT_UUNAME="`uname -n`"
UU_CURRENT_UUNAME="${UU_CURRENT_UUNAME%%.*}"
UU_SPOOL_DIR="${UU_SPOOL_DIR:-/var/spool/uucppublic}" # (spool Directory of files to be transfered using UU)
UU_LOGFILE="${UU_LOGFILE:-${UU_SPOOL_DIR}/.eStorLinkLog}" # (Log file)
UU_DATABASE_DIR="${UU_DATABASE_DIR:-${UU_SPOOL_DIR}/.Database}"  # (Directory location of eStorLink data files)
UU_OVERWRITE="0"	# (0=donot overwrite existing file  1=overwrite existing file)
UU_REMOTE_EXISTS="0"	# (0=destination file does not exist  1=Destination file exists)
UU_NOTIFY_USER="webmaster@mailhost"
UU_NOTIFY_USER=""

# UU_PUT_PATH1="${UU_PUT_PATH1:-aigaix01}"
# UU_REMOTE_ALT="HQ2"
# UU_PUT_PATH2="STR004::STR004"

UU_LOCAL_FNAME=""	# (local name of file before transfer)
UU_LOCAL_DIR=""	# (local directory location of file before transfer)
UU_REMOTE_FNAME=""	# (remote name of file after transfer)
UU_REMOTE_DIR=""	# (remote directory location of file after transfer)
UU_PUT_PATH1=""	# (List of remote destination hosts)
UU_GET_PATH1=""	# (List of remote source hosts)

################################################################
while getopts othl:r:m:g:p:a:f:b:c: i $*
do
  case $i in
	o )	UU_OVERWRITE="1";;
	l )	UU_LOCAL_FNAME=`basename ${OPTARG}`;
		UU_LOCAL_DIR=`dirname ${OPTARG}`;;
	r )	UU_REMOTE_FNAME=`basename ${OPTARG}`;
		UU_REMOTE_DIR=`dirname ${OPTARG}`;;
	m )	UU_NOTIFY_USER="${OPTARG}";;
	g )	UU_GET_PATH1="${OPTARG}";;
	p )	UU_PUT_PATH1="${OPTARG}";;
	a )	UU_ACTION="${OPTARG}";;
	f )	UU_FUNCTION="${OPTARG}";;
	b )	UU_TYPE="${OPTARG}";;
	c )	UU_COMMENT="${OPTARG}";;
	t )	UU_SENDNOW="1";;
        * ) usage ;;
  esac
done
################################################################

case "_" in
  "_${UU_LOCAL_FNAME}" )  echo "ERROR: Local File Name not specified";exit;;
  "_${UU_LOCAL_DIR}" )    echo "ERROR: Local Directory not specified";exit;;
  "_${UU_REMOTE_FNAME}" ) echo "ERROR: Remote File Name not specified";exit;;
  "_${UU_REMOTE_DIR}" )   echo "ERROR: Remote Directory not specified";exit;;
esac

if [ "_${UU_GET_PATH1}" = "_" -a "_${UU_PUT_PATH1}" = "_" ]
then
    echo "ERROR: Host list not specified"
    exit
fi

if [ "_${UU_GET_PATH1}" != "_" -a "_${UU_PUT_PATH1}" != "_" ]
then
    echo "ERROR: Cannot specify both GET and PUT Host list"
    exit
fi

if [ "_${UU_PUT_PATH1}" != "_" -a ! -d "${UU_LOCAL_DIR}" ]
then
  echo "ERROR: Local Directory does not exist"
  exit
fi

if [ "_${UU_PUT_PATH1}" != "_" -a ! -f "${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}" ]
then
  echo "ERROR: Local file does not exist"
  exit
fi

UU_OPTIONS="-a ${UU_NOTIFY_USER}"
UU_OPTIONS=""

[ ${UU_SENDNOW} -eq 0 ] && UU_OPTIONS="${UU_OPTIONS} -r"

case "_${UU_OUTPUT_TYPE}" in
	"_" ) exit;;
	"_ascii" ) exit;;
	"_none" ) exit;;
	"_html" ) exit;;
	"_log" ) ;;
esac


if [ "_${UU_PUT_PATH1}" != "_" ]
then
  if UU_LOCAL_CHKSUM=`sum < "${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}"`
  then
    UU_LOCAL_CHKSUM="${UU_LOCAL_CHKSUM%% *}"
    UU_OUTPUT_FILE="${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T"
    echo "#!/bin/ksh" > "${UU_OUTPUT_FILE}"
    rcommand "################################################################"
    rcommand "TIMESTAMP=\"${TIMESTAMP}\""
    rcommand "UU_PROGRAMID=\"${UU_PROGRAMID}\""
    rcommand "UU_OUTPUT_TYPE=\"${UU_OUTPUT_TYPE}\""
    rcommand "UU_ACTION=\"${UU_ACTION}\""
    rcommand "UU_FUNCTION=\"${UU_FUNCTION}\""
    rcommand "UU_TYPE=\"${UU_TYPE}\""
    rcommand "UU_SENDNOW=\"${UU_SENDNOW}\""
    rcommand "UU_PUT_PATH1=\"${UU_PUT_PATH1}\""
    rcommand "UU_GET_PATH1=\"${UU_GET_PATH1}\""
    rcommand "UU_REMOTE_ALT=\"${UU_REMOTE_ALT}\""
    rcommand "UU_PUT_PATH2=\"${UU_PUT_PATH2}\""

    rcommand "UU_LOCAL_UUNAME=\"${UU_LOCAL_UUNAME}\""
    rcommand "UU_CURRENT_UUNAME=\"\`uname -n\`\""
    rcommand "UU_CURRENT_UUNAME=\"\${UU_CURRENT_UUNAME%%.*}\""

    rcommand "UU_SPOOL_DIR=\"${UU_SPOOL_DIR}\""
    rcommand "UU_LOGFILE=\"${UU_LOGFILE}\""
    rcommand "UU_DATABASE_DIR=\"${UU_DATABASE_DIR}\""
    rcommand "UU_LOCAL_DIR=\"${UU_LOCAL_DIR}\""
    rcommand "UU_REMOTE_DIR=\"${UU_REMOTE_DIR}\""
    rcommand "UU_LOCAL_FNAME=\"${UU_LOCAL_FNAME}\""
    rcommand "UU_REMOTE_FNAME=\"${UU_REMOTE_FNAME}\""
    rcommand "UU_LOCAL_CHKSUM=\"${UU_LOCAL_CHKSUM}\""
    rcommand "UU_OVERWRITE=\"${UU_OVERWRITE}\""
    rcommand "UU_OPTIONS=\"${UU_OPTIONS}\""

    rcommand "case \"_\${UU_OUTPUT_TYPE}\" in"
    rcommand "	\"_\" ) exit;;"
    rcommand "	\"_ascii\" ) exit;;"
    rcommand "	\"_none\" ) exit;;"
    rcommand "	\"_html\" ) exit;;"
    rcommand "	\"_log\" ) ;;"
    rcommand "esac"

    rcommand "for UU_REMOTE_UUNAME in \`echo \"\${UU_PUT_PATH1}\" | sed -e \"s/:/	/g\"\`"
    rcommand "do"
    rcommand "  if [[ \"_\${UU_CURRENT_UUNAME}\" != \"_\${UU_REMOTE_UUNAME}\" ]]"
    rcommand "  then"

    rcomment "Sending Send Request"
    rcommand "    uux \${UU_OPTIONS} \"\${UU_REMOTE_UUNAME}!/bin/ksh \${UU_LOCAL_UUNAME}!\${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_CURRENT_UUNAME}.\${UU_LOCAL_FNAME}.T > !\${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_REMOTE_UUNAME}.\${UU_LOCAL_FNAME}.S\" 2>&1"

    rcommand "  else"

    rcommand "    touch \${UU_LOGFILE}"
    rcommand "    echo \"#!/bin/ksh\" > /tmp/eStorLink\${$}.tmp"
    lcommand "################################################################"
    lcommand "touch \${UU_LOGFILE}"
    lcomment "Begin \${UU_ACTION}"
    lcommand "TIMESTAMP=\\\"\${TIMESTAMP}\\\""
    lcommand "UU_PROGRAMID=\\\"\${UU_PROGRAMID}\\\""
    lcommand "UU_OUTPUT_TYPE=\\\"\${UU_OUTPUT_TYPE}\\\""
    lcommand "UU_ACTION=\\\"\${UU_ACTION}\\\""
    lcommand "UU_FUNCTION=\\\"\${UU_FUNCTION}\\\""
    lcommand "UU_TYPE=\\\"\${UU_TYPE}\\\""
    lcommand "UU_SENDNOW=\\\"\${UU_SENDNOW}\\\""
    lcommand "UU_PUT_PATH1=\\\"\${UU_PUT_PATH1}\\\""
    lcommand "UU_GET_PATH1=\\\"\${UU_GET_PATH1}\\\""
    lcommand "UU_REMOTE_ALT=\\\"\${UU_REMOTE_ALT}\\\""
    lcommand "UU_PUT_PATH2=\\\"\${UU_PUT_PATH2}\\\""

    lcommand "UU_LOCAL_UUNAME=\\\"\${UU_LOCAL_UUNAME}\\\""
    lcommand "UU_CURRENT_UUNAME=\\\"\`uname -n\`\\\""
    lcommand "UU_CURRENT_UUNAME=\\\"\${UU_CURRENT_UUNAME%%.*}\\\""

    lcommand "UU_SPOOL_DIR=\\\"\${UU_SPOOL_DIR}\\\""
    lcommand "UU_LOGFILE=\\\"\${UU_LOGFILE}\\\""
    lcommand "UU_DATABASE_DIR=\\\"\${UU_DATABASE_DIR}\\\""
    lcommand "UU_LOCAL_DIR=\\\"\${UU_LOCAL_DIR}\\\""
    lcommand "UU_REMOTE_DIR=\\\"\${UU_REMOTE_DIR}\\\""
    lcommand "UU_LOCAL_FNAME=\\\"\${UU_LOCAL_FNAME}\\\""
    lcommand "UU_REMOTE_FNAME=\\\"\${UU_REMOTE_FNAME}\\\""
    lcommand "UU_LOCAL_CHKSUM=\\\"\${UU_LOCAL_CHKSUM}\\\""
    lcommand "UU_OVERWRITE=\\\"\${UU_OVERWRITE}\\\""
    lcommand "UU_OPTIONS=\\\"\${UU_OPTIONS}\\\""

    lcommand "UU_REMOTE_UUNAME=\\\"\${UU_REMOTE_UUNAME}\\\""

    rcommand "    if [ -f \"\${UU_REMOTE_DIR}/\${TIMESTAMP}.\${UU_LOCAL_UUNAME}.\${UU_REMOTE_FNAME}\" ]"
    rcommand "    then"
    lcomment "TIMESTAMP File already exists"
    rcommand "    else"
    lcomment "TIMESTAMP File does not exist"
    rcommand "    fi"

    rcommand "    if [ ! -f \"\${UU_REMOTE_DIR}/\${TIMESTAMP}.\${UU_LOCAL_UUNAME}.\${UU_REMOTE_FNAME}\" -o \"_${UU_OVERWRITE}\" = \"_1\" ]"
    rcommand "    then"
    rcommand "      if [ \"_${UU_OVERWRITE}\" = \"_1\" ]"
    rcommand "      then"
    lcomment "OK to overwrite TIMESTAMP file if it exists"
    rcommand "      fi"
    lcomment "Command Line = \${0} \${*}"

    rcommand "      if uudecode < \${0}"
    rcommand "      then"
    lcomment "uudecode was successful"

    rcommand "        if [ -f \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\" ]"
    rcommand "        then"
    rcommand "            UU_REMOTE_EXISTS=\"1\""
    lcomment "Remote File already exists"
    lcommand "UU_REMOTE_EXISTS=\\\"1\\\""
    rcommand "        else"
    rcommand "            UU_REMOTE_EXISTS=\"0\""
    lcomment "Remote File does not exist"
    lcommand "UU_REMOTE_EXISTS=\\\"0\\\""
    rcommand "        fi"

    rcommand "        if [ \${UU_OVERWRITE} -eq 1 -o \${UU_REMOTE_EXISTS} -eq 0 ]"
    rcommand "        then"
    rcommand "          if [ \${UU_OVERWRITE} -eq 1 ]"
    rcommand "          then"
    lcomment "Overwriting remote file"
    rcommand "          fi"

    rcommand "          if cp \"\${UU_REMOTE_DIR}/\${TIMESTAMP}.\${UU_LOCAL_UUNAME}.\${UU_REMOTE_FNAME}\" \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\""
    rcommand "          then"
    lcomment "Successfully copied to remote file name"
    rcommand "          else"
    lcomment "ERROR: Unable to copy to remote file name"
    rcommand "          fi"
    rcommand "        else"
    lcomment "ERROR: Remote file already exists or flag not set to OVERWRITE"
    rcommand "        fi"
    rcommand "      else"
    lcomment "ERROR: uudecode failed"

    rcommand "      fi"
    rcommand "    else"
    lcomment "ERROR: TIMESTAMP File already exists"
    lcomment "ERROR: UU_OVERWRITE=\${UU_OVERWRITE}"
    rcommand "    fi"

    rcommand "    if [ -f \${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME} ]"
    rcommand "    then"
    lcomment "Comparing accuracy of transmitted file"

    rcommand "      UU_REMOTE_CHKSUM=\`sum < \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\"\`"
    rcommand "      UU_REMOTE_CHKSUM=\"\${UU_REMOTE_CHKSUM%% *}\""
    lcommand "UU_REMOTE_CHKSUM=\\\"\${UU_REMOTE_CHKSUM}\\\""
    lcomment "UU_LOCAL_CHKSUM=\${UU_LOCAL_CHKSUM}"
    lcomment "UU_REMOTE_CHKSUM=\${UU_REMOTE_CHKSUM}"

    rcommand "      if [ \"_\${UU_LOCAL_CHKSUM}\" = \"_\${UU_REMOTE_CHKSUM}\" ]"
    rcommand "      then"
    lcomment "Check sums match"

    rcommand "      else"
    lcomment "ERROR: Checksums do not match"

    rcommand "      fi"
    rcommand "    else"
    lcomment "ERROR: Remote file was not created"
    rcommand "    fi"


    lcomment "End \${UU_ACTION}"
    lcommand "cat eStorLink\${$}.tmp > \${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_CURRENT_UUNAME}.\${UU_LOCAL_FNAME}.O"
    lcommand "cat \${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_CURRENT_UUNAME}.\${UU_LOCAL_FNAME}.O | mail -s \\\"UU File Send Confirm from \${UU_CURRENT_UUNAME}\\\" dfrench@aignetplex.com"
    lcommand "grep -i \\\"^# \\\" \${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_CURRENT_UUNAME}.\${UU_LOCAL_FNAME}.O >> \${UU_LOGFILE}"
    rcommand "  uux \"\${UU_LOCAL_UUNAME}!/bin/ksh !/tmp/eStorLink\${$}.tmp\""


    rcommand "  fi"
    rcommand " done"

    rcommand "exit"
    rcommand "################################################################"

    if uuencode "${UU_REMOTE_DIR}/${TIMESTAMP}.${UU_LOCAL_UUNAME}.${UU_REMOTE_FNAME}" < "${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}" >> "${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T"
    then
      echo "Uuencoded \"${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}\" successfully"
      echo "Executing ${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T"
      chmod 777 "${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T"
      echo "ksh \"${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T\""
      echo "# ${TIMESTAMP}|${UU_REMOTE_UUNAME}|${UU_REMOTE_DIR}/${UU_REMOTE_FNAME}|${UU_LOCAL_UUNAME}|${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}|${UU_NOTIFY_USER}|${UU_OVERWRITE}|${UU_SENDNOW}|${UU_CURRENT_UUNAME}|Executing Send Request" >> ${UU_LOGFILE}
      ksh "${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_LOCAL_FNAME}.T"
    else
      echo "ERROR Uuencode of \"${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}\" failed"
      exit

    fi
  fi
fi

if [ "_${UU_GET_PATH1}" != "_" ]
then
  UU_LOCAL_UUNAME="`uname -n`"
  UU_LOCAL_UUNAME="${UU_LOCAL_UUNAME%%.*}"
  for UU_REMOTE_UUNAME in `echo ${UU_GET_PATH1} | sed -e "s/:/	/g"`
  do
    UU_OUTPUT_FILE="${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_REMOTE_FNAME}.T"
    echo "#!/bin/ksh" > "${UU_OUTPUT_FILE}"
    rcommand "################################################################"
    rcommand "TIMESTAMP=\"${TIMESTAMP}\""
    rcommand "UU_PROGRAMID=\"${UU_PROGRAMID}\""
    rcommand "UU_OUTPUT_TYPE=\"${UU_OUTPUT_TYPE}\""
    rcommand "UU_ACTION=\"${UU_ACTION}\""
    rcommand "UU_FUNCTION=\"${UU_FUNCTION}\""
    rcommand "UU_TYPE=\"${UU_TYPE}\""
    rcommand "UU_SENDNOW=\"${UU_SENDNOW}\""
    rcommand "UU_PUT_PATH1=\"${UU_LOCAL_UUNAME}\""
    rcommand "UU_GET_PATH1=\"${UU_REMOTE_UUNAME}\""
    rcommand "UU_LOCAL_ALT=\"${UU_LOCAL_ALT}\""
    rcommand "UU_PUT_PATH2=\"${UU_PUT_PATH2}\""

    rcommand "UU_REMOTE_UUNAME=\"${UU_REMOTE_UUNAME}\""
    rcommand "UU_LOCAL_UUNAME=\"${UU_LOCAL_UUNAME}\""
    rcommand "UU_CURRENT_UUNAME=\"\`uname -n\`\""
    rcommand "UU_CURRENT_UUNAME=\"\${UU_CURRENT_UUNAME%%.*}\""

    rcommand "UU_SPOOL_DIR=\"${UU_SPOOL_DIR}\""
    rcommand "UU_LOGFILE=\"${UU_LOGFILE}\""
    rcommand "UU_DATABASE_DIR=\"${UU_DATABASE_DIR}\""
    rcommand "UU_REMOTE_DIR=\"${UU_REMOTE_DIR}\""
    rcommand "UU_LOCAL_DIR=\"${UU_LOCAL_DIR}\""
    rcommand "UU_REMOTE_FNAME=\"${UU_REMOTE_FNAME}\""
    rcommand "UU_LOCAL_FNAME=\"${UU_LOCAL_FNAME}\""
    rcommand "UU_REMOTE_CHKSUM=\"${UU_REMOTE_CHKSUM}\""
    rcommand "UU_OVERWRITE=\"${UU_OVERWRITE}\""
    rcommand "UU_OPTIONS=\"${UU_OPTIONS}\""

    rcommand "if [ \"_\${UU_CURRENT_UUNAME}\" = \"_\${UU_REMOTE_UUNAME}\" ]"
    rcommand "then"

    rcommand "  touch \${UU_LOGFILE}"
    rcommand "  echo \"#!/bin/ksh\" > /tmp/eStorLink\${$}.tmp"
    lcommand "################################################################"
    lcommand "touch \${UU_LOGFILE}"
    lcomment "Begin Retrieve Request"

    lcommand "TIMESTAMP=\\\"\${TIMESTAMP}\\\""
    lcommand "UU_PROGRAMID=\\\"\${UU_PROGRAMID}\\\""
    lcommand "UU_OUTPUT_TYPE=\\\"\${UU_OUTPUT_TYPE}\\\""
    lcommand "UU_ACTION=\\\"\${UU_ACTION}\\\""
    lcommand "UU_FUNCTION=\\\"\${UU_FUNCTION}\\\""
    lcommand "UU_TYPE=\\\"\${UU_TYPE}\\\""
    lcommand "UU_SENDNOW=\\\"\${UU_SENDNOW}\\\""
    lcommand "UU_PUT_PATH1=\\\"\${UU_LOCAL_UUNAME}\\\""
    lcommand "UU_GET_PATH1=\\\"\${UU_REMOTE_UUNAME}\\\""
    lcommand "UU_LOCAL_ALT=\\\"\${UU_LOCAL_ALT}\\\""
    lcommand "UU_PUT_PATH2=\\\"\${UU_PUT_PATH2}\\\""

    lcommand "UU_REMOTE_UUNAME=\\\"\${UU_REMOTE_UUNAME}\\\""
    lcommand "UU_LOCAL_UUNAME=\\\"\${UU_LOCAL_UUNAME}\\\""
    lcommand "UU_CURRENT_UUNAME=\\\"\\\`uname -n\\\`\\\""
    lcommand "UU_CURRENT_UUNAME=\\\"\\\${UU_CURRENT_UUNAME%%.*}\\\""

    lcommand "UU_SPOOL_DIR=\\\"\${UU_SPOOL_DIR}\\\""
    lcommand "UU_LOGFILE=\\\"\${UU_LOGFILE}\\\""
    lcommand "UU_DATABASE_DIR=\\\"\${UU_DATABASE_DIR}\\\""
    lcommand "UU_REMOTE_DIR=\\\"\${UU_REMOTE_DIR}\\\""
    lcommand "UU_LOCAL_DIR=\\\"\${UU_LOCAL_DIR}\\\""
    lcommand "UU_REMOTE_FNAME=\\\"\${UU_REMOTE_FNAME}\\\""
    lcommand "UU_LOCAL_FNAME=\\\"\${UU_LOCAL_FNAME}\\\""
    lcommand "UU_OVERWRITE=\\\"\${UU_OVERWRITE}\\\""

    lcommand "grep -i \\\"^# \\\" \\\${0} >> \${UU_LOGFILE}"
    llogfile "Processing Retrieve Request"

    rcommand "  if [ -f \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\" ]"
    rcommand "  then"
    lcomment "Remote File exists"
    rcommand "    UU_REMOTE_CHKSUM=\`sum < \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\"\`"
    rcommand "    UU_REMOTE_CHKSUM=\"\${UU_REMOTE_CHKSUM%% *}\""
    lcomment "UU_REMOTE_CHKSUM=\${UU_REMOTE_CHKSUM}"
    rcommand "  else"
    rcommand "    UU_REMOTE_CHKSUM=\"0\""
    lcomment "Remote File does not exist"
    rcommand "  fi"

    lcommand "UU_REMOTE_CHKSUM=\\\"\${UU_REMOTE_CHKSUM}\\\""
    lcommand "UU_OPTIONS=\\\"\${UU_OPTIONS}\\\""
    lcommand "UU_LOCAL_UUNAME=\\\"\${UU_LOCAL_UUNAME}\\\""

    lcommand "if uudecode < \\\${0}"
    lcommand "then"
    llogfile "uudecode was successful"
    lcommand "else"
    llogfile "ERROR: Uudecode Failed"
    lcommand "fi"

    lcommand "UU_LOCAL_CHKSUM=\\\`sum < \\\"\\\${UU_LOCAL_DIR}/\\\${TIMESTAMP}.\\\${UU_REMOTE_UUNAME}.\\\${UU_LOCAL_FNAME}\\\"\\\`"
    lcommand "UU_LOCAL_CHKSUM=\\\"\\\${UU_LOCAL_CHKSUM%% *}\\\""
    llogfile "UU_LOCAL_CHKSUM=\\\${UU_LOCAL_CHKSUM}"
    lcommand "if [ \\\"_\\\${UU_REMOTE_CHKSUM}\\\" = \\\"_\\\${UU_LOCAL_CHKSUM}\\\" ]"
    lcommand "then"
    llogfile "Check Sums match"
    lcommand "else"
    llogfile "ERROR: Check Sums do not match"
    lcommand "fi"

    lcommand "if [ -f \\\${UU_LOCAL_DIR}/\\\${UU_LOCAL_FNAME} -a \\\${UU_OVERWRITE} -eq 0 ]"
    lcommand "then"
    llogfile "Local file already exists"
    lcommand "else"

    lcommand "  if [ -f \\\${UU_LOCAL_DIR}/\\\${UU_LOCAL_FNAME} -a \\\${UU_OVERWRITE} -eq 1 ]"
    lcommand "  then"
    llogfile "Overwriting Local File"
    lcommand "  fi"

    lcommand "  if [ -f \\\${UU_LOCAL_DIR}/\\\${TIMESTAMP}.\\\${UU_REMOTE_UUNAME}.\\\${UU_LOCAL_FNAME} ]"
    lcommand "  then"
    llogfile "TIMESTAMP file exists"
    lcommand "  else"
    llogfile "ERROR: TIMESTAMP file does not exist"
    lcommand "  fi"

    lcommand "  if cp \\\${UU_LOCAL_DIR}/\\\${TIMESTAMP}.\\\${UU_REMOTE_UUNAME}.\\\${UU_LOCAL_FNAME} \\\${UU_LOCAL_DIR}/\\\${UU_LOCAL_FNAME}"
    lcommand "  then"
    llogfile "Successfully copied TIMESTAMP file to local file"
    lcommand "  else"
    llogfile "ERROR: Unable to copy TIMESTAMP file to local file"
    lcommand "  fi"
    lcommand "fi"

    lcommand "cat \\\${0} > \\\${UU_SPOOL_DIR}/\\\${TIMESTAMP}.\\\${UU_REMOTE_UUNAME}.\\\${UU_LOCAL_FNAME}.O"
    lcommand "cat \\\${UU_SPOOL_DIR}/\\\${TIMESTAMP}.\\\${UU_REMOTE_UUNAME}.\\\${UU_LOCAL_FNAME}.O | mail -s \\\"UU File Retrieve Confirm from \${UU_REMOTE_UUNAME}\\\" dfrench@aignetplex.com"

    rcommand "  uux \"\${UU_LOCAL_UUNAME}!/bin/ksh !/tmp/eStorLink\${$}.tmp > \${UU_LOCAL_UUNAME}!\${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_LOCAL_UUNAME}.\${UU_REMOTE_FNAME}.S\" 2>&1"

    lcommand "exit"
    lcommand "################################################################"
    rcommand "  if uuencode \"\${UU_LOCAL_DIR}/\${TIMESTAMP}.\${UU_REMOTE_UUNAME}.\${UU_LOCAL_FNAME}\" < \"\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}\" >> /tmp/eStorLink\${$}.tmp"
    rcommand "  then"
    lcomment "Uuencode was successful"
    rcommand "    chmod 777 /tmp/eStorLink\${$}.tmp"
    rcommand "  fi"
    lcomment "End Retrieve Processing"

    rcommand "else"
    rcommand "  uux \${UU_OPTIONS} \"\${UU_REMOTE_UUNAME}!/bin/ksh !\${UU_SPOOL_DIR}/\${TIMESTAMP}.\${UU_CURRENT_UUNAME}.\${UU_REMOTE_FNAME}.T\""
    rcommand "  echo \"# \${TIMESTAMP}|\${UU_REMOTE_UUNAME}|\${UU_REMOTE_DIR}/\${UU_REMOTE_FNAME}|\${UU_LOCAL_UUNAME}|\${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}|\${UU_NOTIFY_USER}|\${UU_OVERWRITE}|\${UU_SENDNOW}|\${UU_CURRENT_UUNAME}|Sending Retrieve Request\" >> \${UU_LOGFILE}"
    rcommand "fi"
    echo "# ${TIMESTAMP}|${UU_REMOTE_UUNAME}|${UU_REMOTE_DIR}/${UU_REMOTE_FNAME}|${UU_LOCAL_UUNAME}|${UU_LOCAL_DIR}/${UU_LOCAL_FNAME}|${UU_NOTIFY_USER}|${UU_OVERWRITE}|${UU_SENDNOW}|${UU_CURRENT_UUNAME}|Executing Retrieve Request" >> ${UU_LOGFILE}
    ksh -x "${UU_SPOOL_DIR}/${TIMESTAMP}.${UU_CURRENT_UUNAME}.${UU_REMOTE_FNAME}.T"
  done
fi