#!/bin/sh
#From: Martin Spott <martin@onyx.dirnet.com>
#Subject: aaaiiiiiaaaaiiiiiii ....
#To: gert@greenie.muc.de
#Date: Mon, 5 Aug 1996 22:55:24 +0200 (MET DST)


#!/bin/sh
#
# I normally run this with a statically linked '/bin/pdksh'

# /usr/local/lib/mgetty+sendfax/new_fax
#
# A script to send mgetty's incoming faxes via MIME encoded EMail;
# by Martin Spott (martin.spott@uni-duisburg.de), I borrowed one or two
# lines from other's ....
#
# I wrote this one because I wanted a program using as little specialized
# MIME tools as possible.
#
# This 'new_fax' only needs ' g32pbm', 'pnmscale' and 'pnmtotiff' from the
# 'pbmplus' package, 'mmencode' from the 'multimail' packages, and the rest
# is standard Unix tools.
# It was tested with thsmail under Linux and PMMail under OS/2 as frontends.

# This script is called when a message was recorded. 
# It gets the following arguments:
#      $1 : the hangup code
#      $2 : the remote id
#      $3 : the number of pages
#      $4... : the file names

# Place the correct EMail-adresses here !!!
USER="<faxuser-EMail>"
ADMIN="<administrator-EMail>"

# How mgetty calls us.
HANGUP_CODE="$1"
SENDER_ID="$2"
NUMBER_PAGES="$3"

# Some miscellaneous data and filenames.
TMP=/tmp
XSCALE=0.58
TIMESTAMP=`/bin/date +%m.%d_%H:%M:%Sh`
MIME_TIFF=$TMP/TIFF_$TIMESTAMP
MIME_MAIL=$TMP/MAIL_$TIMESTAMP

# The binaries we need; please check carefully !!!
BASENAME=/bin/basename
CAT=/bin/cat
ECHO=/bin/echo
ELM=/usr/local/bin/elm
G3TOPBM=/usr/local/bin/g32pbm
MMENCODE=/usr/local/bin/mmencode
PNMSCALE=/usr/local/pbmplus/bin/pnmscale
PNMTOTIFF=/usr/local/pbmplus/bin/pnmtotiff
RM=/bin/rm
SED=/bin/sed
SENDMAIL=/usr/sbin/sendmail

# Essential lines to put into the header of a MIME mail.
HEADERLINE_1="MIME-Version: 1.0"
HEADERLINE_2="Content-Type: multipart/mixed; boundary="attachment""

# Lines to put into the header of each MIME attachment.
ATTACHMENT_HEADERLINE_1="--attachment"
ATTACHMENT_HEADERLINE_2="Content-Type: image/tiff"
ATTACHMENT_HEADERLINE_3="Content-Transfer-Encoding: base64"

# Line to close the attachment section of a MIME mail.
ATTACHMENT_ENDLINE="--attachment--"

# Now we build our MIME mailheader using commandline arguments.
$ECHO "Subject: incoming FAX from $2 with $3 pages" > $MIME_MAIL
$ECHO "$HEADERLINE_1" >> $MIME_MAIL
$ECHO "$HEADERLINE_2" >> $MIME_MAIL
$ECHO "" >> $MIME_MAIL

# To handle each attachment we skip the first three arguments (those we
# already used for the header).
shift 3

# Handling of each fax page, whose names are given via commandline arguments.
# We have to cut off the absolute path via 'basename' and remove the dot
# which separates the page number. Also we add a filename extension and fit
# the result as filename into our attachment header - some mail frontends
# need this.
#
for i in $@
do
	# We use the second character in the filename to identify the
	# resolution of our incoming fax, so we can easily scale the fax for
	# display on a screen.
	RESOLUTION=`$BASENAME $i | $SED 's/.\(.\).*/\1/'`
	if [ "$RESOLUTION" = "n" ]
	then
		YSCALE=1.16
	else
		YSCALE=0.58
	fi
#
	# The fax is converted from G3 to PBM, it is scaled and then
	# converted to TIFF.
	# We write it into a temporary file, because my 'mmencode' doesn't
	# handle standard input correctly.
	$CAT $i | $G3TOPBM | $PNMSCALE -xscale $XSCALE -yscale $YSCALE \
	| $PNMTOTIFF > $MIME_TIFF
#
	# Now we put the header for each attachment into our MIME mail.
	$ECHO "$ATTACHMENT_HEADERLINE_1" >> $MIME_MAIL
	$ECHO "$ATTACHMENT_HEADERLINE_2; name=\"`$BASENAME $i|cut -f1 -d\.``$BASENAME $i|cut -f2 -d \.`.TIF\"" >> $MIME_MAIL
	$ECHO "$ATTACHMENT_HEADERLINE_3" >> $MIME_MAIL
	$ECHO "" >> $MIME_MAIL
#
	# Here we do base64 encoding of out TIFF data and add the result
	# into our MIME mail as attachment.
	$MMENCODE -b $MIME_TIFF >> $MIME_MAIL
#
	# To clean up temporary TIFF data.
	$RM -f $MIME_TIFF
#
	# Each attachment has to end with a blank line (I believe).
	$ECHO "" >> $MIME_MAIL
done

# To close the attachment section of our mail.
$ECHO "$ATTACHMENT_ENDLINE" >> $MIME_MAIL

# Sending the mail.
$SENDMAIL < $MIME_MAIL $USER

# Cleaning up mail data.
$RM -f $MIME_MAIL

# Cheerio !
exit 0


Martin.
-- 
EMail:              I prefer correspondence to: Martin.Spott@onyx.dirnet.com
    If necessary, business mail can be sent to: Martin.Spott@uni-duisburg.de
                                                Maus: Martin Spott @ DU3
----------------------------------------------------------------------------
 Unix _IS_ user friendly - it's just selective about who its friends are !
----------------------------------------------------------------------------

