#! /bin/sh
#    $Id: notify.sh.in,v 1.24 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.
#
#   2004/02/28  Frank Brock
#
# The notify shell now behaves in a manner like faxrcvd.  
# It is now written in shell with a little embedded awk as needed.

#
# notify qfile why jobtime [nextTry]
#
# Return mail to the submitter of a job when notification is needed.
#
if [ $# != 3 -a $# != 4 ]; then
    echo "Usage: $0 qfile why jobtime [nextTry]"
    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(@MANNUM1_8@)
command.  Read the documentation on setting up HylaFAX before you
startup a server system.

EOF
    exit 1
}

# need to parse out the command line here.  some may be needed
# in the FaxNotify.
QFILE=$1
WHY=$2
JTIME=$3
NEXT=${4:-'??:??'}

# 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

local_seq() {
	if [ $1 -gt $2 ]; then
		return
	fi
	COUNT=$1
	while [ $COUNT -le $2 ]
	do
		echo $COUNT
		COUNT=`expr $COUNT + 1`

	done
}

INFO=$SBIN/faxinfo
TIFFINFO=tiffinfo
FAX2PS=$TIFFBIN/fax2ps
TIFF2PS=tiff2ps
PS2PDF=ps2pdf
PDF2PS=pdf2ps
PS2FAX=bin/ps2fax
PDF2FAX=bin/pdf2fax
TOADDR=FaxMaster
FROMADDR=fax
NOTIFY_FAXMASTER=never
RETURNFILETYPE=
MIMEBOUNDARY="NextPart$$"
RETURNTECHINFO=yes

case $WHY in
    failed|requeued|poll_rejected|poll_no_document|poll_failed)
	RETURNTRANSCRIPT=yes;;
    *)
	RETURNTRANSCRIPT=no;;
esac
#
# 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

adjustNotifyFaxMaster()
{
#  Determine what NOTIFY_FAXMASTER should be set to based
#  on the current value of NOTIFY_FAXMASTER and on other
#  attributes set about this notification.
#  NOTIFY_FAXMASTER can be set to
#    yes - send everything
#    no - send nothing
#    errors - send only failed type faxes that are not from busy, no answer or no carrier
#    always - send everything
#    never - send nothing
#
# known $WHY values we can test to set NOTIFY_FAXMASTER are
# "done" "failed" "rejected" "blocked" "requeued" "removed" "killed" "timedout"
# "format_failed" "no_formatter" "poll_rejected" "poll_no_document" "poll_failed"
    case $NOTIFY_FAXMASTER in
        never|no) NOTIFY_FAXMASTER=no;;
        always|yes) NOTIFY_FAXMASTER=yes;;
        errors) 
            case $WHY in
                timedout|rejected|format_failed|no_formatter|poll_rejected|poll_no_document)
		    NOTIFY_FAXMASTER=yes;;
		"done"|blocked|removed|killed)
		    NOTIFY_FAXMASTER=no;;
                *)
                    if (match "$faxstatus" "Busy signal") || (match "$faxstatus" "No answer") || (match "$faxstatus" "No carrier"); then
                        NOTIFY_FAXMASTER=no
                    else 
                        NOTIFY_FAXMASTER=yes
                    fi;;
            esac;;
        *) NOTIFY_FAXMASTER=no;;
    esac
}

setCustomValues()
{
    #
    # Apply customizations.  All customizable variables should
    # be set to their non-customized defaults prior to this.
    #
    if [ -f etc/FaxNotify ]; then
        # source notify preferecnes
        . etc/FaxNotify
    fi

    #
    # Language settings...
    #
    . bin/dictionary
    if [ -f etc/FaxDictionary ]; then
	. etc/FaxDictionary
    fi
    setItemSize
}

parseQfile()
{
    # In shell scripts, there are no special characters in hard-quoted
    # strings (quoted with (')). Single-quotes can't even be escaped
    # inside such strings and must be put outside of them. We thus replace
    # (') with ('\'') which terminates the current string, adds a single
    # quote and starts a new string.
    #
    # New lines in eval could cause problems so we escape them. As with
    # single quotes above, we must first close the current string, add
    # the escaped new line (double quoted) and start a new string.
    #
    # print out variable name and value so we can eval it in the shell
    eval `($AWK -F: ' 
    function p(varname,val)
    {
        gsub(/\\047/, "\047\\\\\047\047", val);
        gsub(/\n/, "\047\042\\\\n\042\047", val);
        printf "%s=\\047%s\\047\n",varname,val
    }
    BEGIN {
        nfiles = 0;
        npins = 0;
        pagewidth = 0;
        pagelength = 0;
        resolution = 0;
        jobtype = "facsimile";
        signalrate = "unknown";
        dataformat = "unknown";
        doneop = "default";
        pagernum = "unknown";
        commid = "";
	csi = "";
	equipment = "";
	station = "";
    }
    /^csi/	{ p("csi",$2); }
    /^nsf/	{ p("equipment",$3); }
    /^nsf/	{ p("station",$5); }
    /^jobid/    { p("jobid",$2); }
    /^groupid/    { p("groupid", $2); }
    /^state/    { p("state", $2+0); }  
    /^doneop/    { p("doneop", $2); }
    /^number/    { p("number", $2); }
    /^external/    { p("number", $2); }        # override unprocessed number
    /^receiver/    { p("receiver", $2); }
    /^company/    { p("company", $2); }
    /^sender/    { p("sender", $2); }
    /^mailaddr/    { p("mailaddr", $2); }
    /^owner/    { p("owner", $2); }
    /^jobtag/    { jobtag = $0; sub("jobtag:", "", jobtag); p("jobtag", jobtag)}
    /^jobtype/    { p("jobtype", $2); }
    # status needs to be used in the shell as faxstatus since status is reserved word
    /^status/    { status = $0; sub("status:", "", status);
              while ($0 ~ /\\\\$/ && getline > 0) {
                  sub(/\\\\$/, "\\n", status);
                  status = status $0;
              } p("faxstatus", status);
            }
    /^resolution/    { p("resolution", $2); }
    /^npages/    { p("npages", $2); }
    /^totpages/    { p("totpages", $2); }
    /^dirnum/    { p("dirnum", $2); }
    /^commid/    { p("commid", $2); }
    /^ntries/    { p("ntries", $2); }
    /^ndials/    { p("ndials", $2); }
    /^pagewidth/    { p("pagewidth", $2); }
    /^pagelength/    { p("pagelength", $2); }
    /^signalrate/    { p("signalrate", $2); }
    /^dataformat/    { p("dataformat", $2); }
    /^modem/    { p("modem", $2); }
    /^totdials/    { p("totdials", $2); }
    /^tottries/    { p("tottries", $2); }
    /^client/    { p("client", $2); }
    /^[!]*post/    { p("files_"++nfiles, $4); p("filetype_"nfiles, "PostScript"); }
    /^[!]*tiff/    { p("files_"++nfiles, $4); p("filetype_"nfiles, "TIFF"); }
    /^[!]*pdf/    { p("files_"++nfiles, $4); p("filetype_"nfiles, "PDF"); }
    /^[!]*pcl/    { p("files_"++nfiles, $4); p("filetype_"nfiles, "PCL"); }
    /^page:/    { p("pins_"++npins, $4); }
    /^data:/    { p("files_"++nfiles, $4); }
    /^poll/        { p("poll", " -p"); }
    END { p("nfiles", nfiles); p("npins", npins) } ' $QFILE )`

}

fileType()
# determine the type of file passed using the unix 'file' command
# with the '-i' option (mime type output)
{
    FILENAME=$1
    if [ -f "$FILENAME" ] ; then
	FILETYPE=`file $FILENAME`
        if (match "$FILETYPE" "postscript") ; then 
            echo "PostScript"
        elif (match "$FILETYPE" "tiff") ; then 
            echo "TIFF"
        elif (match "$FILETYPE" "pdf") ; then 
            echo "PDF"
        else
            echo "$DICTUNKNOWNDOCTYPE"
        fi
    else
        echo "$DICTNOFILEEXISTS"
    fi
}

returnFaxImage()
# output a mime encoded set of file attachments with mime headers
# for each file listed in the files_XX set, make sure that it is 
# a valid, non zero file.  Then test the file format of the source
# file we are encoding that is is a known file type that we can 
# convert from.  Out put to standard out the appropriate mime header.
# Then based on the argument passed to this function,
# convert the source file to the file format given as the argument.
# finally, encode this converted file and put it out on std out.
# Supported values of RETURNFORMAT are "tif","pdf", and "ps"
# A ReturnFomat=original reutns the file in its original format.
# Supported file formats of the source are "tiff", "pdf" and "ps" 
{
   RETURNFORMAT=$1
   if [ $nfiles -gt 0 ] ; then
    for i in `local_seq 1 $nfiles`; do 
        name="files_$i"
	eval filename=`echo "$"$name`
	if [ -s $filename ] ; then # file is > 0 size
	    type="filetype_$i"
	    eval FROMFMT=`echo "$"$type`
	    #
	    # Because Ghostscript accepts PDF identically to PostScript
	    # and because HylaFAX has historically handled PDF named as
	    # "postscript" we have to double-check the PostScript filetype.
	    #
	    if [ "$FROMFMT" = "PostScript" ]; then
		if [ "`fileType $filename`" != "PostScript" ]; then
		    FROMFMT=PDF
		fi
	    fi
            case "$FROMFMT" in "PostScript" | "TIFF" | "PDF" ) # test we know the source format
                if [ "$RETURNFORMAT" = "tif" ] ; then
                    ENCODEDFILENAME="$number-$i.tif"
                    putMimeImageTIFHeader "$ENCODEDFILENAME"
		    putTifEncodedImage "$filename" "$FROMFMT"
                elif [ "$RETURNFORMAT" = "pdf" ] ; then
                    ENCODEDFILENAME="$number-$i.pdf"
                    putMimeAppPDFHeader "$ENCODEDFILENAME"
		    putPdfEncodedImage "$filename" "$FROMFMT"
                elif [ "$RETURNFORMAT" = "ps" ] ; then
                    ENCODEDFILENAME="$number-$i.ps"
                    putMimeAppPSHeader "$ENCODEDFILENAME"
		    putPsEncodedImage "$filename" "$FROMFMT"
                elif [ "$RETURNFORMAT" = "original" ] ; then
                    case "$FROMFMT" in
                    "TIFF")
			ENCODEDFILENAME="$number-$i.tif"
                        putMimeImageTIFHeader "$ENCODEDFILENAME"
			putTifEncodedImage "$filename" "$FROMFMT"
                        ;;
                    "PostScript")
			ENCODEDFILENAME="$number-$i.ps"
                        putMimeAppPSHeader "$ENCODEDFILENAME"
			putPsEncodedImage "$filename" "$FROMFMT"
                        ;;
                    "PDF")
			ENCODEDFILENAME="$number-$i.pdf"
                        putMimeAppPDFHeader "$ENCODEDFILENAME"
			putPdfEncodedImage "$filename" "$FROMFMT"
                        ;;
                    esac
                fi
            ;;
            esac
        fi # file[i] exists
    done # for each nfiles
   fi  # nfiles > 0
}

putPdfEncodedImage()
# Convert the source file from the CONVERTFROM type into a pdf file and 
# then do a mimeEndode of the file
{
    SOURCEFILE=$1
    CONVERTFROM=$2
    OUTFILE="tmp/conv2pdf$$.out" ;
    if [ "$CONVERTFROM" = "PDF" ] ; then
        mimeEncode "$SOURCEFILE"
        return # all done here
    elif [ "$CONVERTFROM" = "TIFF" ] ; then
        CONVERTCMD="$TIFF2PDF -o $OUTFILE $SOURCEFILE" 
    elif [ "$CONVERTFROM" = "PostScript" ] ; then
        CONVERTCMD="$PS2PDF $SOURCEFILE $OUTFILE" 
    else
        return # unknow convert from format
    fi
    $CONVERTCMD > $ERRORSTO 2>&1
    mimeEncode "$OUTFILE"
    $RM -f $OUTFILE > $ERRORSTO 2>&1
}

putPsEncodedImage()
# Convert the source file from the CONVERTFROM type into a ps file and 
# then do a mimeEndode of the file
{
    SOURCEFILE=$1
    CONVERTFROM=$2
    OUTFILE="tmp/conv2ps$$.out" ;
    if [ "$CONVERTFROM" = "PostScript" ] ; then
        mimeEncode "$SOURCEFILE"
        return # all done here
    elif [ "$CONVERTFROM" = "TIFF" ] ; then
        #  tiff2ps -a for all pages, 
        CONVERTCMD="$TIFF2PS -a  $SOURCEFILE > $OUTFILE" 
    elif [ "$CONVERTFROM" = "PDF" ] ; then
        CONVERTCMD="$PDF2PS $SOURCEFILE $OUTFILE" 
    else
        return # unknow convert from format
    fi
    $CONVERTCMD > $ERRORSTO 2>&1
    mimeEncode "$OUTFILE"
    $RM -f $OUTFILE > $ERRORSTO 2>&1
}

putTifEncodedImage()
# Convert the source file from the CONVERTFROM type into a tif file and 
# then do a mimeEndode of the file
{
    SOURCEFILE=$1
    CONVERTFROM=$2
    OUTFILE="tmp/conv2tif$$.out" ;
    if [ $CONVERTFROM = "TIFF" ] ; then 
        mimeEncode "$SOURCEFILE"
        return # all done here
    elif [ $CONVERTFROM = "PDF" ] ; then 
        CONVERTCMD="$PDF2FAX -r $resolution -o $OUTFILE $SOURCEFILE" 
    elif [ $CONVERTFROM = "PostScript" ] ; then 
        CONVERTCMD="$PS2FAX -r $resolution -o $OUTFILE $SOURCEFILE" 
    else
        return # unknow convert from format
    fi
    $CONVERTCMD > $ERRORSTO 2>&1
    mimeEncode "$OUTFILE"
    $RM -f $OUTFILE > $ERRORSTO 2>&1
}

putMimeSetup()
# write out the setup portions of the mime encoded message
#
# NOTES ABOUT MIME that I have figured out thus far since I am not a mime expert in any way.
# The mimeboundary is defined in the setup of the email.
# Mimeboundary, as defined, with a leading "--" must be used to seperate mime sections.
# The mime email must be terminated by a mimeboundary with a leading AND trailing "--"
# A MimeBoundary must immediately preceed the mime content definition section for each
# of the mime sections. No blank lines between the mimeboundary and the content definition.
# I think a blank line in front of the mimeboundary is required.
# So, the code hear is organized such that all functions putting out a mime contect section
# are respnosible for putting in their own leading mimeboundary. But they are NOT to put in 
# any mime boundary trailers.
# The last thing of the action section is to put in terminating mime boundary.
#
{
    echo "MIME-Version: 1.0"
    echo "Content-Type: Multipart/Mixed; Boundary=\"$MIMEBOUNDARY\""
    echo "Content-Transfer-Encoding: 7bit"
}

putMimeTextHeader()
# write out the beginning mime boundary for plain/text part
{
    echo ""
    echo "This is a multi-part message in MIME format."
    echo ""
    echo "--$MIMEBOUNDARY"
    echo "Content-Type: text/plain; charset=$CHARSET"
    echo "Content-Transfer-Encoding: quoted-printable"
    echo ""
}

putMimeAppPDFHeader()
# filename passed should already include the proper .pdf extension
# write out the beginning mime boundary for the pdf file application part
# filename is just the name of the file for mime header encoding and is what is
# shown as the name of the attahced file.
{
    echo ""
    echo "--$MIMEBOUNDARY"
    echo "Content-Type: application/pdf; name=\"$1\""
    echo "Content-Description: FAX document"
    echo "Content-Transfer-Encoding: $ENCODING"
    echo "Content-Disposition: attachment; filename=\"$1\""
    echo ""
}

putMimeAppPSHeader()
# filename passed should already include the proper .ps extension
# write out the beginning mime boundary for the ps file application part
# filename is just the name of the file for mime header encoding and is what is
# shown as the name of the attahced file.
{
    echo ""
    echo "--$MIMEBOUNDARY"
    echo "Content-Type: application/postscript; name=\"$1\""
    echo "Content-Description: FAX document"
    echo "Content-Transfer-Encoding: $ENCODING"
    echo "Content-Disposition: attachment; filename=\"$1\""
    echo ""
}

putMimeImageTIFHeader()
# filename passed should already include the proper .tif extension
# write out the beginning mime boundary for the tif file image  part
# filename is just the name of the file for mime header encoding and is what is
# shown as the name of the attahced file.
{
    echo ""
    echo "--$MIMEBOUNDARY"
    echo "Content-Type: image/tiff; name=\"$1\""
    echo "Content-Description: FAX document"
    echo "Content-Transfer-Encoding: $ENCODING"
    echo "Content-Disposition: attachment; filename=\"$1\""
    echo ""
}

mimeEncode()
# out put a mime encoded form of the given file
{
    FILENAME=$1
    if [ ! -f "$FILENAME" ] ; then
        return # cannot do much more without a file
    fi
    if [ -x "$MIMENCODE" ]; then
        $MIMENCODE < $FILENAME 2>$ERRORSTO
    elif [ -x "$UUENCODE" ]; then
        if [ "$ENCODING" = "base64" ]; then
            $UUENCODE -m $FILENAME $FILENAME \
	    	| $GREP -v "^begin" \
		| $GREP -v "^====$" \
		2>$ERRORSTO
        else
            $UUENCODE $FILENAME $FILENAME 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 < $FILENAME 2>$ERRORSTO
    fi
}

printItem()
{
    FMT="`echo %$ITEMSIZE\s: $1`"
    TAG="$2"
    VALUE="$3"
    printf "$FMT\n" "$TAG" "$VALUE"
}


printBanner()
{
    echo ""
    echo "    ---- $1 ----"
    echo ""
}

match()
#  look for substring in fullsting.  substring can be a regular expression or plain string
#  if the substring is found anywhere in the full string, true(0) is returned.
{
    FULLSTR="$1"
    SUBSTR="$2"
    echo "$FULLSTR" | $GREP -i "$SUBSTR" > /dev/null 2>&1
    if [ $? -eq 0 ] ; then
        return 0
    else
        return 1
    fi
}

#
# Construct a return-to-sender message.
#
returnToSender()
{
    printBanner "Unsent job status"
    printItem "%s" "$DICTDESTINATION" "$number"
    printItem "%s" "$DICTJOBID" "$jobid"
    printItem "%s" "$DICTGROUPID" "$groupid"
    printItem "%s" "$DICTSENDER" "$sender"
    printItem "%s" "$DICTMAILADDR" "$mailaddr"
    if [ -n "$commid" ] ; then
        printItem "%s" "$DICTCOMMID" "$commid"
    fi
    if [ "$modem" != "any" -a "$RETURNTECHINFO" = "yes" ] ; then
        printItem "%s" "$DICTMODEM" "$modem"
    fi
    printItem "%s" "$DICTSUBMITTEDFROM" "$client"
    if [ "$jobtype" = "facsimile" -a "$RETURNTECHINFO" = "yes" ] ; then
        printItem "%u (mm)" "$DICTPAGEWIDTH" "$pagewidth"
        printItem "%.0f (mm)" "$DICTPAGELENGTH" "$pagelength"
        printItem "%.0f (lpi)" "$DICTRES" "$resolution"
    fi
    if [ -z "$faxstatus" ] ; then faxstatus="  ($DICTNOTHINGAVAIL)" ; fi
    # we need to use the %b (instead of the %s) so the embedded escape characters will
    # be interpreted. 
    printItem "%b" "$DICTSTATUS" "$faxstatus"
    printItem "%u ($DICTREMOTEEXCHANGES)" "$DICTDIALOGS" "$tottries"
    printItem "%u ($DICTFAILEDCALLS)" "$DICTDIALS" "$ndials"
    printItem "%u ($DICTTOTALCALLS)" "$DICTCALLS" "$totdials"
    if [ "$jobtype" = "facsimile" ] ; then
        printItem "%u ($DICTPAGESTRANSMITTED)" "$DICTPAGES" "$npages"
        printItem "%u ($DICTTOTALPAGES)" "$DICTTOTPAGES" "$totpages"
        printItem "%u ($DICTATTEMPTSPAGE)" "$DICTATTEMPS" "$ntries"
        printItem "%u ($DICTDIRNEXTPAGE)" "$DICTDIRNUM" "$dirnum"
        if [ $nfiles -gt 0 -a "$RETURNTECHINFO" = "yes" ] ; then
            printBanner "$DICTDOCSSUBMITTED"
            eval echo "$DICTDOCSTEXT1"
            eval echo "$DICTDOCSTEXT2"
            eval echo "$DICTDOCSTEXT3"
            eval echo "$DICTDOCSTEXT4"
            echo ""
            printf "%-20s %8s %s\n" "$DICTFILENAME" "$DICTSIZE" "$DICTTYPE"
	    for i in `local_seq 1 $nfiles`; do
                name="files_$i"
                eval filename=`echo "$"$name`
		if [ -f $filename ] ; then
			set - `wc -c "$filename"`
                    FILESIZE=$1
		    type="filetype_$i"
		    eval filetype=`echo "$"$type`
		    #
		    # Because Ghostscript accepts PDF identically to PostScript
		    # and because HylaFAX has historically handled PDF named as
		    # "postscript" we have to double-check the PostScript filetype.
		    #
		    if [ "$filetype" = "PostScript" ]; then
			if [ "`fileType $filename`" != "PostScript" ]; then
			    filetype=PDF
			fi
		    fi
		    printf "%-20s %8d %s\n" "$filename" "$FILESIZE" "$filetype"
                fi
            done
        fi
    elif [ "$jobtype" = "pager" ] ; then
        if [ $npins -ne 0 ] ; then
            printBanner "$DICTUNSENTPAGES"
	    for i in `local_seq 1 $npins`; do
                name="files_$i"
		eval pin=`echo "$"$name`
                printf "%15s\n" "$DICTPIN" $pin
            done
        fi
        if [ $nfiles -ne 0 -a -s $files_0 ] ; then
            printBanner "$DICTMESSAGETEXT"
            cat $files_0
        fi
    fi
}

returnTranscript()
{
    if [ "$RETURNTRANSCRIPT" = "yes" ] ; then
        printBanner "$DICTLOGFOLLOWS2"
        COMFILE="log/c$commid"
        if [ -f "$COMFILE" ] ; then
            # dump the comfile to output except for '-- data' lines
            cat $COMFILE | $GREP -v "\-\- data"
        else 
            printf "$DICTNOLOGAVAIL"
            if [ -n "$commid" ] ; then  # non 0 len commid value
                    printf "($DICTCOMMID c$commid)"
            fi
            echo "."
        fi
    fi
}

printStatus()
{
    if [ -z "$1" ] ; then # 0 string len
        echo "<$DICTNOREASON>"
    else
	# we need to change the '\n' in the strings to real newlines
	echo "$1" | sed -e 's/\\n/\n/g'
    fi
}

putHeaders()
{
    putMimeSetup
    echo "To: $mailaddr"
    echo "Subject: $1"
    putMimeTextHeader
    THISJOBTYPE=`eval echo "$"DICT$jobtype`
    eval printf \"$DICTYOURJOBTO\"
}

##########
##  MAIN
##########

#  exports used for debug tracing
#export -f returnTranscript
#export -f putMimeAppPDFHeader
#export -f putMimeImageTIFHeader
#export -f mimeEncode
#export -f printItem
#export -f putTifEncodedImage
#export -f putMimeSetup
#export -f printStatus
#export -f putHeaders
#export -f returnFaxImage
#export -f putPdfEncodedImage
#export -f putMimeTextHeader
#export -f printBanner
#export -f returnToSender
#export -f parseQfile
#export -f match
# sh -x

# we parse the q file fisrt in case any of the varialbe setting 
# operations may want to know about the details of the fax
parseQfile  

#process the FaxNotify script after parse q file in case the admin wants to 
# set any values based on what is in the q file
setCustomValues 

# adjust faxmaster notify based on some rules and what is found 
# possibly in the q file.
adjustNotifyFaxMaster 

(if [ -z "$jobtag" ] ; then
    THISJOBTYPE=`eval echo "$"DICT$jobtype`
    jobtag="`eval echo $DICTJOB`"
fi
if [ "$doneop" = "default" ] ; then
    doneop="remove"
fi
if [ "$jobtype" = "pager" ] ; then
    number=$pagernum
fi
DESTINATION="$receiver"
if [ -n "$receiver" ] && [ -n "$company" ]; then
    DESTINATION="$receiver $DICTAT "
fi
DESTINATION="$DESTINATION$company"
if [ -n "$DESTINATION" ]; then
    DESTINATION="$DESTINATION ($number)"
else
    DESTINATION="$number"
fi
if [ "$WHY" = "done" ] ; then
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTCOMPLETED"
    echo "$DICTCOMPLETEDSUCCESSFULLY";
    echo "";
    if [ "$jobtype" = "facsimile" ] ; then
        printItem "%u" "$DICTPAGES" "$npages"
        printItem "%s" "$DICTRECEIVER" "$csi"
        if [ "$RETURNTECHINFO" = "yes" ] ; then
            if [ "$resolution" = "196" ] ; then 
                printItem "%s" "$DICTQUALITY" "$DICTFINE"
            else
                printItem "%s" "$DICTQUALITY" "$DICTNORMAL"
            fi
            printItem "%u (mm)" "$DICTPAGEWIDTH" "$pagewidth"
            printItem "%.0f (mm)" "$DICTPAGELENGTH" "$pagelength"
            printItem "%s" "$DICTSIGNALRATE" "$signalrate"
            printItem "%s" "$DICTDATAFORMAT" "$dataformat"
            printItem "%s" "$DICTREMOTEEQUIPMENT" "$equipment"
            printItem "%s" "$DICTREMOTESTATION" "$station"
        fi
    fi
    if [ "$RETURNTECHINFO" = "yes" ] ; then
        if [ "$tottries" != "1" ] ; then 
            printItem "%s ($DICTREMOTEEXCHANGES)" "$DICTDIALOGS" "$tottries"
        fi
        if [ "$totdials" != "1" ] ; then 
            printItem "%s ($DICTTOTALCALLS)" "$DICTCALLS" "$totdials"
        fi
        if [ "$modem" != "any" ] ; then 
            printItem "%s" "$DICTMODEM" "$modem"
        fi
        printItem "%s" "$DICTSUBMITTEDFROM" "$client"
        printItem "%s" "$DICTJOBID" "$jobid"
        printItem "%s" "$DICTGROUPID" "$groupid"
        printItem "%s" "$DICTCOMMID" "c$commid"
        printf "\n$DICTPROCESSINGTIME %s.\n" "$JTIME"
    fi
    if [ -n "$faxstatus" -a "$RETURNTRANSCRIPT" = "yes" ] ; then
        # use -e in echo to interpret escape characters in faxstatus
	echo "$DICTADDITIONALINFO"
	printStatus "     $faxstatus"
        returnTranscript
    fi
    if [ -n "$RETURNFILETYPE" ] ; then 
        returnFaxImage "$RETURNFILETYPE"
    fi
elif [ "$WHY" = "failed" ] ; then 
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTFAILED"
    printf " $DICTFAILEDBECAUSE\n    "
    printStatus "$faxstatus"
    returnTranscript
    returnToSender
    if [ -n "$RETURNFILETYPE" ] ; then 
        returnFaxImage "$RETURNFILETYPE"
    fi
elif [ "$WHY" = "rejected" ] ; then 
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTFAILED"
    printf " $REJECTEDBECAUSE\n    "
    printStatus "$faxstatus"
    returnToSender
    if [ -n "$RETURNFILETYPE" ] ; then
        returnFaxImage "$RETURNFILETYPE"
    fi
elif [ "$WHY" = "blocked" ] ; then
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTBLOCKED"
    printf " $DICTDELAYEDBECAUSE\n    "
    printStatus "$faxstatus" 
    echo ""
    echo "$DICTASSOONASPOSSIBLE"
elif [ "$WHY" = "requeued" ] ; then
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTREQUEUED" 
    printf " $DICTWASNOTSENT\n    "
    printStatus "$faxstatus" 
    echo ""
    echo "$DICTWILLBERETRIED $NEXT."
    returnTranscript
elif [ "$WHY" = "removed" ] || [ "$WHY" = "killed" ] ; then 
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTREMOVEDFROMQUEUE" 
    echo " $DICTWASDELETED"
    if [ "$WHY" = "killed" ] ; then
        returnToSender
    fi
    if [ -n "$RETURNFILETYPE" ] ; then
	returnFaxImage "$RETURNFILETYPE"
    fi
elif [ "$WHY" = "timedout" ]; then 
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTFAILED" 
    echo " $DICTTIMEDOUT"
    returnToSender
    if [ -n "$RETURNFILETYPE" ] ; then
        returnFaxImage "$RETURNFILETYPE"
    fi
elif [ "$WHY" = "format_failed" ] ; then 
    putHeaders "$jobtag to $DESTINATION $DICTFAILED" 
    echo " $DICTCONVERSION1"
    echo "$DICTCONVERSION2"
    echo ""
    printStatus "$faxstatus"
    echo ""
    echo "$DICTCONVERSION3"
    returnToSender
elif [ "$WHY" = "no_formatter" ] ; then 
    putHeaders "$jobtag $DICTTO $DESTINATION $DICTFAILED"
    echo " $DICTNOFORMATTER1"
    echo "$DICTNOFORMATTER2"
    returnToSender
elif (match "$WHY"  "poll_*") ; then 
    putHeaders "$DICTNOTICEABOUT $jobtag" 
    printf "$DICTPOLLINGFAILED"
    if [ "$WHY" = "poll_rejected" ] ; then
        echo "$DICTREMOTEREJECTED"
    elif [ "$WHY" = "poll_no_document" ] ; then
        echo "$DICTNODOCTOPOLL"
    elif [ "$WHY" = "poll_failed" ] ; then
        echo "$DICTUNSPECIFIEDPROBLEM"
    fi
    echo "";
    printf "$DICTPROCESSINGTIME %s.\n" "$JTIME"
    returnTranscript
else 
    putHeaders "$DICTNOTICEABOUT $jobtag" 
    echo " $DICTUNKNOWNREASON1"
    echo "$DICTUNKNOWNREASON2"
    echo "$DICTUNKNOWNREASON3"
    echo ""
    echo "why: $WHY"
    echo "jobTime: $JTIME"
    echo "nextTry: $NEXT"
    echo  ""
    echo "$DICTUNKNOWNREASON4"
    returnTranscript
    returnToSender
fi
# put out a terminating MIME boundary
echo ""
echo "--$MIMEBOUNDARY--"
) 2>$ERRORSTO | $SENDMAIL -f$FROMADDR -oi -t

if [ "$NOTIFY_FAXMASTER" = "yes" ]; then
    # make sure that FAXMASTER gets all information in email by forcing some RETURN values
    # and then reset them later
    origRETURNTRANSCRIPT=$RETURNTRANSCRIPT
    origRETURNTECHINFO=$RETURNTECHINFO
    RETURNTRANSCRIPT="yes"
    RETURNTECHINFO="yes"
    (
	if [ -z "$jobtag" ] ; then
	    jobtag="$jobtype job $jobid"
	fi
	echo "To: $TOADDR"
	echo "Subject: $jobtag to $number $WHY"
	echo
	returnTranscript
	returnToSender
    ) 2>$ERRORSTO | $SENDMAIL -f$FROMADDR -oi -t
    RETURNTRANSCRIPT=$origRETURNTRANSCRIPT
    RETURNTECHINFO=$origRETURNTECHINFO
fi
