#!/bin/sh
# Filename: sig_lftp
# Sends files via sftp, syntax:
# sig_sftp SourcePath DestPathFile DestHost User Password [RenameName]
trap "" HUP
SourcePath=$1
SourceDir=${SourcePath%/*}
SourceFile=${SourcePath##*/}
DestPath=$2
DestDir=${DestPath%/*}
DestFile=${DestPath##*/}
DestHost=$3
# For security, you can always hard code in the username and/or 
# password here.
User=$4
Password=$5
Rename=$6

# Error checking: Make sure each string has a value.

if [ "${SourceDir}"  = "" ]; then exit 1; fi
if [ "${SourceFile}" = "" ]; then exit 1; fi
if [ "${DestDir}"    = "" ]; then exit 1; fi
if [ "${DestFile}"   = "" ]; then exit 1; fi
if [ "${DestHost}"   = "" ]; then exit 1; fi
if [ "${User}"       = "" ]; then exit 1; fi

# Should be able to work withoput password, if keys are setup
# if [ "${Password}"   = "" ]; then exit 1; fi

cd $SourceDir

export SSHPASS=${Password}

if [ "${Rename}" = "" ]; then

  sshpass -e sftp ${User}@${DestHost} << END_OF_FTP1
    cd "${DestDir}"
    put "${SourceFile}" "${DestFile}"
    quit
END_OF_FTP1
  exit $?

else
    
  sshpass -e sftp ${User}@${DestHost} << END_OF_FTP2
    cd "${DestDir}"
    put "${SourceFile}" "${DestFile}"
    rename "${DestFile}" "${Rename}"
    quit
END_OF_FTP2
  exit $?

fi
