#! /bin/sh
# sendbsmtp -- send a bsmtp packet given a destination and file.
#
# USAGE:  sendbsmtp mailer dest [ file ]

PATH=/bin:/usr/bin:/usr/local:/usr/ucb:.
export PATH

FROM=`whoami`@ukma
MAILER=${1}
TO=${2}
FILE=${3}

#		 NOTICE!!!!
#
# We're zapping the command line args here, so if you need them
# take care of getting them before now!
set `date`
DAY=${1}
MONTH=${2}
DATE=${3}
TIME=${4}
TIMEZONE=${5}
YEAR=${6}
ARPADATE="${DAY}, ${DATE} ${MONTH} ${YEAR} ${TIME} ${TIMEZONE}"

if test -z "${MAILER}" -o -z "${TO}"; then
	echo Usage: sendbsmtp mailer dest \[ file \]
	echo
	echo where, mailer is user@host id of the mailer to send this to, and
	echo        dest is the address to send the mail to.
	exit 1
fi

if test -z "${FILE}"; then
	echo No file name given, stdin used
fi

( echo HELO ukma
  echo MAIL FROM:\<${FROM}\>
  echo RCPT TO:\<${TO}\>
  echo DATA
  echo "From:    " ${FROM}
  echo "To:      " ${TO}
  echo "Date:    " ${ARPADATE}
  echo -n "Subject: " File sent from ukma.bitnet \(or ms.uky.edu\) " "
  if test -z "${FILE}"; then
  	echo -- Unknown name
  else
  	echo -- ${FILE}
  fi
  echo " "
  if test ! -z "${FILE}"; then
  	if test -f ${FILE} -a -r ${FILE}; then
		# This sed command looks for lines beginning with a '.',
		# and adds a '.' to the beginning of the line.  This is
		# as per the [B]SMTP spec.
  		sed '/^\./s/^\..*$/.&/' ${FILE}
  	fi
  else
	sed '/^\./s/^\..*$/.&/'
  fi
  echo .
  echo QUIT ) >bsmtp.in

exit 0
