#! /bin/sh
#	$Header: /usr/people/sam/fax/./util/RCS/faxrcvd.sh.in,v 1.21 1995/04/08 21:44:53 sam Rel $
#
# Warning, this file was automatically created by the HylaFAX configure script
#
# HylaFAX Facsimile Software
#
# Copyright (c) 1990-1995 Sam Leffler
# Copyright (c) 1991-1995 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.
#

#
# VERSION:	v3.0pl1113
# TARGET:	sparc-sun-sunos4.1.4
#

SENDMAIL=/usr/lib/sendmail
AWK=/usr/local/bin/gawk
INFO=/usr/local/spool/fax/bin/faxinfo
FAX2PS=/usr/local/fax/bin/fax2ps

#
# faxrcvd qfile time protocol sigrate error-msg [devID]
#
if test $# != 5 -a $# != 6
then
    echo "Usage: $0 qfile time sigrate protocol error-msg [devID]"
    exit 1
fi
FILE="$1"
TIME="$2"
RATE="$3"
FORMAT="$4"
MSG="$5"
DEVICE="${6:-Unspecified Device}"

#
# Check the sender's TSI and setup to dispatch
# facsimile received from well-known senders.
#
SENDER="`$INFO $1 | $AWK -F: '/Sender/ { print $2 }' 2>/dev/null`"
SENDTO=
if [ -f etc/FaxDispatch ]; then
    . etc/FaxDispatch	# NB: FaxDispatch should use $SENDER to set $SENDTO
fi

Notify()
{
    echo "To: $*"
    echo "From: The HylaFAX Receive Agent <fax>"
    echo "Subject: facsimile received from $SENDER";
    echo "";
    $INFO $FILE
    cat<<EOF
TimeToRecv: $TIME minutes
SignalRate: $RATE
DataFormat: $FORMAT
ReceivedOn: $DEVICE
EOF
    if [ "$MSG" ]; then
	echo ""
	echo "The full document was not received because:"
	echo ""
	echo "    $MSG"
    fi
}

(Notify FaxMaster
if [ -n "$SENDTO" ]; then
    echo ""
    echo "The facsimile was automatically dispatched to: $SENDTO." 
fi
) | 2>&1 $SENDMAIL -ffax -oi FaxMaster
if [ -n "$SENDTO" ]; then
    (MIMEBOUNDARY="NextPart$$"
     echo "Mime-Version: 1.0"
     echo "Content-Type: Multipart/Mixed; Boundary=\"$MIMEBOUNDARY\""
     echo "Content-Transfer-Encoding: 7bit"
     Notify $SENDTO
     echo ""
     echo "--$MIMEBOUNDARY"
     echo "Content-Type: application/postscript"
     echo "Content-Description: FAX document"
     echo "Content-Transfer-Encoding: 7bit"
     echo ""
     $FAX2PS $FILE 2>/dev/null
     echo ""
     echo "--$MIMEBOUNDARY--"
    ) | 2>&1 $SENDMAIL -ffax -oi $SENDTO
fi
