#!/bin/sh
#
# Install this script as mgetty+sendfax's new_fax script (usually in
# /usr/local/lib/etc/mgetty+sendfax, or /etc/mgetty+sendfax), to automatically
# convert incoming faxes to PNG attachments, and mailing them

prefix="/usr"
exec_prefix="/usr"
bindir="/usr/bin"
sysconfdir="/etc/courier"

. $sysconfdir/faxnotifyrc

if test "$3" -lt 1
then
	exit 0
fi

makemessage() {

STATUS="$1"
shift
FAXID="$1"
shift
PAGES="$1"
shift

echo "To: $TOHDR"
echo "From: $FROMHDR"
echo "Subject: $SUBJECTHDR"
echo "Mime-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=courierfax"
echo ""
echo "This is a MIME formatted message"
echo ""
echo "--courierfax"


FILE="$1"
BASENAME="`echo $1 | /usr/bin/sed 's:.*/::'`"
RES="`echo $BASENAME | /usr/bin/cut -c2`"
TTY="`echo $BASENAME | /usr/bin/sed 's/-.*//;s/.*\(..\)$/\1/'`"
FAXID="`echo $BASENAME | /usr/bin/sed 's/^[^-]*-//;s/\.[0-9][0-9]*$//'`"

if test "$RES" = "f"
then
	RES="Fine"
else
	RES="Low"
fi

(

if test "$STATUS" = 0
then
	:
else
	echo "PARTIAL FAX - TRANSMISSION INTERRUPTED AFTER $PAGES PAGES"
fi

echo ""
echo "Pages:           $PAGES"
echo "Resolution:      $RES"
echo "Modem line:      tty$TTY"

if test "$FAXID" = ""
then
	:
else
	echo "Sender's FAX ID: $FAXID"
fi
echo ""
) | /usr/bin/makemime -c "text/plain" -e "quoted-printable" -

PAGENUM=1

while test $PAGENUM -le $PAGES
do
	echo ""
	echo "--courierfax"

	FILE="$1"
	BASENAME="`echo $1 | /usr/bin/sed 's:.*/::'`"
	RES="`echo $BASENAME | /usr/bin/cut -c2`"

	if test -f "$FILE"
	then
		if test "$RES" = "f"
		then
			RES=""
		else
			RES="-s"
		fi

		/usr/bin/g3topbm $RES $FILE | /usr/bin/pnmtopng | \
			/usr/bin/makemime -c "image/png" -e "base64" \
				 -N "Page $PAGENUM" \
				 -a "Content-Disposition: attachment; filename=\"page$PAGENUM.png\"" -
	else
		echo "ERROR: $FILE - not found" | makemime -c "text/plain" -e "quoted-printable" -
	fi

	PAGENUM=`expr $PAGENUM + 1`
	shift
done

echo ""
echo "--courierfax--"
}

makemessage "$@" | $bindir/sendmail -f "$MAILFROM" "$RCPTTO"

if test $? = 0
then
	if test "$DODELETE" = 1
	then
		shift 3
		while test $# -gt 0
		do
			rm -f "$1"
			shift
		done
	fi
fi

