#!/bin/sh
#    BIABAM: Biabam Is A Bash Attachment Mailer
#    Copyright (C) 2000 Mads Martin Jrgensen <mmj@mmj.dk>
#
#    This program is free software; you can redistribute it
#    and/or modify it under the terms of the GNU General Public
#    License as published by the Free Software Foundation; either
#    version 2 of the License, or (at your option) any later
#    version.
#
#    This program is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied
#    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#    PURPOSE.  See the GNU General Public License for more
#    details.
#
#    You should have received a copy of the GNU General Public
#    License along with this program; if not, write to the Free
#    Software Foundation, Inc., 59 Temple Place, Suite 330,
#    Boston, MA 02111-1307 USA

# change these to suit your needs
SENDMAIL=/usr/sbin/sendmail  # your Mail-Transfer-Agent
SENDMAIL_OPTS=               # and its required options

# no user serviceable parts below this point
VERSION=0.9.2

if  [ $# -lt 2 ]; then
  echo "Usage:"
  echo "$0 filename [-s subject] recipient..."
  exit 1
fi

if ! which uuencode > /dev/null; then
  echo "This program needs the uuencode utility to perform base64 encoding."
  exit 1
fi

if ! TEMPFILE="`mktemp /tmp/biabam.XXXXXX`"; then
  echo "Biabam is unable to create the temporary file."
  exit 1
fi

BASETEMP="`basename $TEMPFILE`"
BASEATTACHMENT="`basename $1`"

if ! TEMPUUENCODED="`mktemp /tmp/biabam.uu.XXXXXX`"; then
  echo "Biabam is unable to create the temporary uuencoded file."
  exit 1
fi

BOUNDARY="$BASETEMP$BASETEMP"
uuencode --base64 $1 $BASEATTACHMENT | sed '1d;$d' > $TEMPUUENCODED

shift 		# skip over filename

# have they supplied a subject
SUBJECT="File delivery"
if [ a"$1" = "a-s" ]; then
  shift
  SUBJECT="$1"
  shift
fi

echo "To: $@ " >> $TEMPFILE
echo "Subject: $SUBJECT" >> $TEMPFILE
echo "X-Mailer: BIABAM $VERSION" >> $TEMPFILE
echo "Message-ID: <`date +%Y%m%d%H%M%S`.$BASETEMP@biabam>" >> $TEMPFILE
echo "Mime-Version: 1.0" >> $TEMPFILE 
echo "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"" >> $TEMPFILE
echo "Content-Disposition: inline" >> $TEMPFILE
echo >> $TEMPFILE
echo >> $TEMPFILE
echo "--$BOUNDARY" >> $TEMPFILE
echo "Content-Type: text/plain; charset=us-ascii" >> $TEMPFILE
echo "Content-Disposition: inline" >> $TEMPFILE
echo >> $TEMPFILE
echo "Email body (type CTRL-d on a blank line to finish):"
cat >> $TEMPFILE
echo >> $TEMPFILE
echo "--$BOUNDARY" >> $TEMPFILE
echo "Content-Type: application/unknown" >> $TEMPFILE
echo "Content-Disposition: attachment; filename=\"$BASEATTACHMENT\"" >> $TEMPFILE
echo "Content-Transfer-Encoding: base64" >> $TEMPFILE
echo >> $TEMPFILE
cat $TEMPUUENCODED >> $TEMPFILE
echo >> $TEMPFILE
echo "--$BOUNDARY--" >> $TEMPFILE
echo >> $TEMPFILE

cat $TEMPFILE | $SENDMAIL $SENDMAIL_OPTS "$@"

/bin/rm -f $TEMPFILE
/bin/rm -f $TEMPUUENCODED

