:
# faxmail: deliver a fax via electronic mail.
#          Use Z-Mail with attachments if installed, otherwise
#          uuencode and deliver using existing mailer.
#
# Usage: faxmail <faxfile> <recipient>

if [ $# -lt 2 ]
then
    echo "$0: Deliver a fax document via electronic mail" 1>&2
    echo "Usage: $0 <faxfile> <recipient> [ <recipient> ... ]" 1>&2
    exit 1
fi

TMPDIR=${TMPDIR:-"/tmp"}
faxfile=$1
shift
recipient="$*"

# If fax is on stdin, save to temp file.
if [ "$faxfile" = "-" ]
then
    cat > $TMPDIR/faxmail$$
    faxfile=$TMPDIR/faxmail$$
fi

# Search for zmail in $PATH.
for i in `echo $PATH | sed 's/:/ /g'`
do
    if [ -x $i/zmail ]
    then
	zmail -subject "Received fax document" \
	      -attach fax:$faxfile $recipient < /dev/null > /dev/null 2>&1
        rm -f $TMPDIR/faxmail$$
	exit 0
    fi
done

# If zmail was not found, resort to uuencode and regular mail.
remotename=`basename $faxfile`
uuencode $faxfile $remotename | mail $recipient
rm -f $TMPDIR/faxmail$$
exit 0
