#! /bin/sh
#	$Id: pollrcvd.sh.in,v 1.10 2006/02/21 18:12:58 lhoward Exp $
#
# HylaFAX Facsimile Software
#
# Copyright (c) 1990-1996 Sam Leffler
# Copyright (c) 1991-1996 Silicon Graphics, Inc.
# HylaFAX is a trademark of Silicon Graphics
# 
# Permission to use, copy, modify, distribute, and sell this software and 
# its documentation for any purpose is hereby granted without fee, provided
# that (i) the above copyright notices and this permission notice appear in
# all copies of the software and related documentation, and (ii) the names of
# Sam Leffler and Silicon Graphics may not be used in any advertising or
# publicity relating to the software without the specific, prior written
# permission of Sam Leffler and Silicon Graphics.
# 
# THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
# EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
# WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
# 
# IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
# ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
# LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
# OF THIS SOFTWARE.
#

#
# pollrcvd mailaddr faxfile devID commID error-msg
#
if [ $# != 5 ]
then
    echo "Usage: $0 mailaddr faxfile devID commID error-msg"
    exit 1
fi

test -f etc/setup.cache || {
    SPOOL=`pwd`
    cat<<EOF

FATAL ERROR: $SPOOL/etc/setup.cache is missing!

The file $SPOOL/etc/setup.cache is not present.  This
probably means the machine has not been setup using the faxsetup(8)
command.  Read the documentation on setting up HylaFAX before you
startup a server system.

EOF
    exit 1
}

# These settings may not be present in setup.cache if user upgraded and
# didn't re-run faxsetup; we set them before calling setup.cache for
# backward compatibility.
ENCODING=base64
MIMENCODE=mimencode
TIFF2PDF=bin/tiff2pdf
TTYCMD=tty
CHARSET=us-ascii

. etc/setup.cache
. bin/common-functions

INFO=$SBIN/faxinfo
FAX2PS=$TIFFBIN/fax2ps
TIFF2PS=tiff2ps
TOADDR=FaxMaster
TIFFINFO=tiffinfo
FROMADDR=FaxMaster

# 
# Redirect errors to a tty, if possible, rather than
# dev-nulling them or allowing them to creep into
# the mail.
# 
if $TTYCMD >/dev/null 2>&1; then
    ERRORSTO=`$TTYCMD`
else
    ERRORSTO=/dev/null
fi

#
# Permit various types of attachment types: ps, tif, pdf
# Note that non-ASCII filetypes require metamail.
# pdf requires tiff2ps and tiff2pdf
# 
FILETYPE=ps

#
# There is no good portable way to find out the fully qualified
# domain name (FQDN) of the host or the TCP port for the hylafax
# service so we fudge here.  Folks may want to tailor this to
# their needs; e.g. add a domain or use localhost so the loopback
# interface is used.
#
HOSTNAME=`hostname`                     # XXX no good way to find FQDN
PORT=4559                               # XXX no good way to lookup service

MAILADDR="$1"
FILE="$2"
DEVICE="$3"
COMMID="$4"
MSG="$5"

FILENAME=`echo $FILE | $SED -e 's/\.tif//' -e 's/recvq\///'`

#
# Produce mailable encoding for binary files.
# 
encode()
{
    if [ -x "$MIMENCODE" ]; then
	$MIMENCODE < $1 2>$ERRORSTO
    elif [ -x "$UUENCODE" ]; then
	if [ "$ENCODING" = "base64" ]; then
	    $UUENCODE -m $1 $1 | grep -E -v "^begin|^====$" 2>$ERRORSTO
	else
	    $UUENCODE $1 $1 | grep -E -v "^begin|^====$" 2>$ERRORSTO
	fi
    else
	# Do not use "-x" for backward compatibility; even if it fails
	# this is last chance to encode data, so there's nothing to lose.
	$MIMENCODE < $1 2>$ERRORSTO
    fi
}

if [ -f $FILE ]; then
    SENDER="`$INFO $FILE | $AWK -F: '/Sender/ { print $2 }' 2>/dev/null`"
fi
if [ -f etc/PollDispatch ]; then
    . etc/PollDispatch       # NB: PollDispatch allows customization
fi
# 
# Fetch language settings (after FaxDispatch for customization of $LANG).
# 
. bin/dictionary
if [ -f etc/FaxDictionary ]; then
    . etc/FaxDictionary
fi
setInfoSize

if [ -f $FILE ]; then
    if [ "$TOADDR" != "$MAILADDR" ]; then # don't send FaxMaster duplicate
	(echo "To: $TOADDR"
	 echo "From: $DICTRECEIVEAGENT <$FROMADDR>"
	 eval echo "Subject: $DICTRETRIEVEDFROM";   
	 echo ""
	 echo "$FILE (ftp://$HOSTNAME:$PORT/$FILE):";
	 faxInfo $FILE
	 printf "%`echo $INFOSIZE`s: $DEVICE\n" "$DICTRECEIVEDON"
	if [ "$MSG" ]; then
	    echo ""
	    echo "$DICTMSGINTRO"
	    echo ""
	    echo "    $MSG"
	    echo ""
	    echo "$DICTLOGFOLLOWS"
	    echo ""
	    if [ -f log/c$COMMID ]; then
		$SED -e '/-- data/d' \
		    -e '/start.*timer/d' -e '/stop.*timer/d' \
		    log/c$COMMID
	    elif [ -n "$COMMID" ]; then
		echo "$DICTNOLOGAVAIL ($DICTCOMMID c$COMMID)."
	    else
		echo "$DICTNOLOGAVAIL."
	    fi
	else
	    echo "    $DICTCOMMID: c$COMMID (ftp://$HOSTNAME:$PORT/log/c$COMMID)"
	fi
	if [ -n "$MAILADDR" ]; then
	    echo ""
	    eval echo "$DICTPOLLDISPATCHTO"
	fi
	) 2>$ERRORSTO | $SENDMAIL -ffax -oi $TOADDR
    fi
    if [ -n "$MAILADDR" ]; then
	(MIMEBOUNDARY="NextPart$$"
	 echo "Mime-Version: 1.0"
	 echo "Content-Type: Multipart/Mixed; Boundary=\"$MIMEBOUNDARY\""
	 echo "Content-Transfer-Encoding: 7bit"
	 echo "To: $MAILADDR"
	 echo "From: $DICTRECEIVEAGENT <$FROMADDR>"
	 eval echo "Subject: $DICTRETRIEVEDFROM";
	 echo ""
	 echo "--$MIMEBOUNDARY"
	 echo "Content-Type: text/plain; charset=$CHARSET"
	 echo "Content-Transfer-Encoding: quoted-printable"
	 echo ""
	 echo "$FILE (ftp://$HOSTNAME:$PORT/$FILE):";
	 faxInfo $FILE
	 printf "%`echo $INFOSIZE`s: $DEVICE\n" "$DICTRECEIVEDON"
	if [ "$MSG" ]; then
	    echo ""
	    echo "$DICTMSGINTRO"
	    echo ""
	    echo "    $MSG"
	    echo ""
	    echo "$DICTLOGFOLLOWS"
	    echo ""
	    if [ -f log/c$COMMID ]; then
		sed -e '/-- data/d' \
		    -e '/start.*timer/d' -e '/stop.*timer/d' \
		    log/c$COMMID
	    elif [ -n "$COMMID" ]; then
		echo "$DICTNOLOGAVAIL ($DICTCOMMID c$COMMID)."
	    else
		echo "$DICTNOLOGAVAIL."
	    fi
	else
	    echo "    $DICTCOMMID: c$COMMID (ftp://$HOSTNAME:$PORT/log/c$COMMID)"
	fi
	echo ""
	echo "--$MIMEBOUNDARY"
	if [ "$FILETYPE" = "tif" ]; then
	    echo "Content-Type: image/tiff; name=\"$FILENAME.tif\""
	    echo "Content-Description: FAX document"
	    echo "Content-Transfer-Encoding: $ENCODING"
	    echo "Content-Disposition: attachment; filename=\"$FILENAME.tif\""
	    echo ""
	    encode $FILE
	elif [ "$FILETYPE" = "pdf" ]; then
	    echo "Content-Type: application/pdf; name=\"c$COMMID.pdf\""
	    echo "Content-Description: FAX document"
	    echo "Content-Transfer-Encoding: $ENCODING"
	    echo "Content-Disposition: attachment; filename=\"c$COMMID.pdf\""
	    echo ""
	    $TIFF2PDF -o $FILE.pdf $FILE
	    encode $FILE.pdf
	    $RM -f $FILE.pdf 2>$ERRORSTO   
	elif [ "$FILETYPE" = "ps" ]; then
	    echo "Content-Type: application/postscript; name=\"$FILENAME.ps\""
	    echo "Content-Description: FAX document"
	    echo "Content-Transfer-Encoding: 7bit"
	    echo "Content-Disposition: attachment; filename=\"$FILENAME.ps\""
	    echo ""
	    $FAX2PS $FILE 2>$ERRORSTO
	fi
	 echo ""
	 echo "--$MIMEBOUNDARY--"
	) 2>$ERRORSTO | $SENDMAIL -ffax -oi $MAILADDR
    fi
else
    #
    # Generate notification mail for a failed attempt.
    #
    (echo "To: $TOADDR"
     echo "From: $DICTRECEIVEAGENT <$FROMADDR>"
     echo "Subject: $DICTPOLLFAILED"
     echo ""
     eval echo "$DICTATTEMPTEDPOLLFAILED"
     echo ""
     echo "    $MSG"
     echo ""
     echo "$DICTLOGFOLLOWS"
     echo ""
     if [ -f log/c$COMMID ]; then
	$SED -e '/-- data/d' \
	    -e '/start.*timer/d' -e '/stop.*timer/d' \
	    log/c$COMMID
     elif [ -n "$COMMID" ]; then
	echo "$DICTNOLOGAVAIL ($DICTCOMMID c$COMMID)."
     else
	echo "$DICTNOLOGAVAIL."
     fi
    ) 2>$ERRORSTO | $SENDMAIL -ffax -oi $TOADDR
fi
