#!/bin/sh

##########################################################################
# faxmail 
# Copyright (C) Harald Milz (hm@seneca.muc.de) 1996
#
# faxmail builds a MIME-compliant file from the input files, encoding
# them if necessary. The MIME file can be sent to a fax service e-mail
# account which in turn disassembles the file to its parts, spooling
# everything to faxspool. faxmail supports those (and only those) 
# file types that are supported by faxspool. Thus, we can keep this
# toolset handy and simple. 
#
# prerequisites:
# - metamail (esp. mmencode)
#
# FIXME: we should supply some security here, e.g. PGP signatures, a
# password or something like that.
#
# syntax: see usage()
##########################################################################

MAILER="/usr/lib/sendmail"

ME="faxmail v0.9 Copyright (C) Harald Milz (hm@seneca.muc.de) 1996"

usage(){
	cat <<- EOF

	$ME

	Usage:
	faxmail [options] phone-number files...
	-n			normal resolution instead of high res.
	-D <destination>	destination string (only informational)
	-a <faxuser>		e-mail address of fax server
	EOF
}

if [ $# -lt 3 ] ; then
	# command line is obviously too short
	usage
	exit 1
fi

RES=""
while : ; do
	case $1 in 
	-n)	RES="-n"
		shift
		;;
	-f)	RETURN_MAIL="$2"
		shift; shift
		;;
	-D)	DEST="$2"
		shift; shift
		;;
	-a)	FAXUSER="$2"
		shift; shift
		;;
	*) 	
		break
	esac
done

PHONE="$1"
shift
FILES="$*"

if [ -z "$PHONE" -o -z "$DEST" -o -z "$FILES" ] ; then 
	usage
	exit 1
fi

BOUNDARY="--------$(date +"%y%m%d%H%M%S")$$"	# this is hopefully unique

(
cat << EOF
X-Mailer: $ME
MIME-Version: 1.0
Subject: "$RES" -D "$DEST" "$PHONE" 
Content-Type: multipart/mixed; boundary="$BOUNDARY"

This is a multi-part message in MIME format.

--$BOUNDARY
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Fax-Res: $RES
Fax-Dest: $DEST
Fax-No: $PHONE

EOF

for FILE in $FILES ; do 
	echo "--$BOUNDARY"
	NAME=$(basename $FILE)
	case $FILE in 
	*.ps)	
		echo "Content-Type: application/postscript; name=\"$NAME\""
		echo "Content-Transfer-Encoding: 7bit"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo 
		cat $FILE
		;;
	*.t | *.txt)	
		echo "Content-Type: text/plain; charset=iso-8859-1; name=\"$NAME\""
		echo "Content-Transfer-Encoding: 8bit"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		cat $FILE
		;;
	*.gif)
		echo "Content-Type: image/gif; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*.tif | *.tiff)
		echo "Content-Type: image/tiff; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*.xwd)
		echo "Content-Type: image/x-xwindowdump; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*.dvi)
		echo "Content-Type: application/x-tex; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*.ppm |	*.pgm | *.pbm)
		echo "Content-Type: application/x-pnm; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*.g3)
		echo "Content-Type: application/x-fax; name=\"$NAME\""
		echo "Content-Transfer-Encoding: base64"
		echo "Content-Disposition: inline; filename=\"$NAME\""
		echo
		mmencode -b $FILE
		;;
	*)
		;;
	esac
	echo
done
echo "--$BOUNDARY--"	# epilogue boundary determines end of message
echo
) | $MAILER $FAXUSER
