:
# mailfax: accept an incoming electronic mail message, and send
#          it as a fax document.  Assumes mail message will arrive
#          on standard input.
#
# Usage: mailfax <fax number>

if [ $# -ne 1 ]
then
    echo "$0: send an electronic mail message as a fax" 1>&2
    echo "Usage: $0 <fax number>" 1>&2
    exit 1
fi

TMPDIR=${TMPDIR:-"/tmp"}
faxnumber="$1"

trap 'rm -f $TMPDIR/mailfax$$; exit 1' 1 2 3 15

# Save message into temp file.
cat > $TMPDIR/mailfax$$

# Get sender and subject.
subject=`sed -e '/^$/q' -e 's/^Subject: //p' -e d $TMPDIR/mailfax$$`
sender=`sed -e '/^$/q' -e '/^From: /!d' -e 's/.*(//' -e 's/).*//' $TMPDIR/mailfax$$`

# Build and queue fax.
buildfax -s "$sender" -S "$subject" < $TMPDIR/mailfax$$ | queuefax "$faxnumber"

# Remove temp file.
rm -f $TMPDIR/mailfax$$

exit 0
