#!/bin/sh
#
# This script is designed to take mail as sent from a RelayFax
# SMTP Client and then push it out through sendfax.
#
# Lee Howard <faxguy@howardsilvan.com>
#

#
# This is for potential debugging.
#
set -x
exec > /tmp/relayfax2sendfax.out.$$ 2>&1

#
# This is where the sendfax and faxcover executables are...
#
EXECDIR=/usr/local/bin

#
# This is where our configuration files will go.
#
CONFDIR=/usr/local/etc/relayfax2sendfax

#
# This is where we'll put our working destinations file.
#
DESTFILE=/tmp/relayfax2sendfax.sendto.$$

#
# This is where metamail will dump everything.
#
export METAMAIL_TMPDIR="/tmp/relayfax2sendfax.$$"

#
# This is where we will store the mail message.
#
MAILFILE=/tmp/relayfax2sendfax.mail.$$

#
# function to read a formatted value from stdin
#
getMatchValue()
{
    cat "$MAILFILE" | grep -m 1 "^$1: " | sed "s/^$1: //"
}

#
# Store the stdin message to disk.
#
cat > $MAILFILE

#
# grab RelayFax client job parameters
#
PRINTEDFAX=`getMatchValue PRINTEDFAX`
CLIENTVERSION=`getMatchValue CLIENTVERSION`
FROMLINE=`getMatchValue FROMLINE`
BILLINGCODE=`getMatchValue BILLINGCODE`
FAXPASSWORD=`getMatchValue FAXPASSWORD`
FROMNAME=`getMatchValue FROMNAME`
PRIORITY=`getMatchValue PRIORITY`
CONFIRMSEND=`getMatchValue CONFIRMSEND`
NOCOVERPAGE=`getMatchValue NOCOVERPAGE`
FAXSUBJECT=`getMatchValue FAXSUBJECT`
COMMENTS=`getMatchValue COMMENTS`
SCHEDULE=`getMatchValue SCHEDULE`
ATTACHMENTNAME=`cat $MAILFILE | grep -e 'Content-Type.*name=' | sed -e 's/^.*name=//g' | sed -e 's/\"//g'`

#
# email is not generally secure, check passwords and usernames
#
PASSWDCHECK=`grep "^$FROMLINE $FAXPASSWORD$" $CONFDIR/faxuserlist`
if [ "$PASSWDCHECK" != "$FROMLINE $FAXPASSWORD" ]; then
    exit
fi

#
# create a list of the destinations to send to
#
cat $MAILFILE | egrep -e "^(FAX|CC)NUMBER: " > $DESTFILE

#
# Adjust CONFIRMSEND to match sendfax synatax.
#
if [ "$CONFIRMSEND" = "TRUE" ]; then
    CONFIRMSEND="-R"
else
    CONFIRMSEND=
fi

#
# Adjust PRIORITY to match sendfax syntax.
#
PRIORITY="-P $[ $PRIORITY * 255 / 100 ]"

#
# Adjust SCHEDULE to match sendfax syntax.
#
if [ -n "$SCHEDULE" ]; then
    if [ "`date -d "$SCHEDULE" +%s`" -lt "`date +%s`" ]; then
	SCHEDULE="now"
    else
	SCHEDULE=`date -d "$SCHEDULE" +"%I:%M %p %b %d"`
    fi
else
    SCHEDULE="now"
fi

#
# Allow for user-specific coverpages in CONFDIR
#
if [ -f "$CONFDIR/$FROMLINE" ]; then
    COVERPAGE="-C $CONFDIR/$FROMLINE"
else
    COVERPAGE=
fi

#
# make the directory where metamail will place the mail parts
#
mkdir $METAMAIL_TMPDIR

#
# run metamail to get the parts out of the mail to disk files
#
metamail -w -x $MAILFILE

#
# delete all unnamed parts (presumably not typical attachments)
#
rm -f $METAMAIL_TMPDIR/mm.*

if [ "$PRINTEDFAX" = "TRUE" ]; then
    DESTINATIONS=`cat $DESTFILE | wc -l`
    while [ $DESTINATIONS -gt 0 ]; do
	TONAME=`head -n 1 $DESTFILE | sed 's/^.*: \([^,]*\), \"\([^"]*\)\", \"\([^"]*\)\".*/\2/'`
	TONUMBER=`head -n 1 $DESTFILE | sed 's/^.*: \([^,]*\), \"\([^"]*\)\", \"\([^"]*\)\".*/\1/'`
	TOCOMPANY=`head -n 1 $DESTFILE | sed 's/^.*: \([^,]*\), \"\([^"]*\)\", \"\([^"]*\)\".*/\3/'`
	if [ "$NOCOVERPAGE" = "TRUE" ]; then
	    $EXECDIR/sendfax $PRIORITY -a "$SCHEDULE" -f "$FROMLINE" $CONFIRMSEND -n -d "$TONAME@$TONUMBER" $METAMAIL_TMPDIR/*
	elif [ "$ATTACHMENTNAME" = "" ]; then
	    $EXECDIR/faxcover -c "$COMMENTS" $COVERPAGE -f "$FROMNAME" -n "$TONUMBER" -r "$FAXSUBJECT" -t "$TONAME" -x "$TOCOMPANY" | \
		$EXECDIR/sendfax $PRIORITY -a "$SCHEDULE" -n -f "$FROMLINE" $CONFIRMSEND -d "$TONAME@$TONUMBER"
	else
	    $EXECDIR/sendfax $PRIORITY -a "$SCHEDULE" $COVERPAGE -f "$FROMLINE" $CONFIRMSEND -r "$FAXSUBJECT" -c "$COMMENTS" -x "$TOCOMPANY" -d "$TONAME@$TONUMBER" $METAMAIL_TMPDIR/*
	fi
	DESTINATIONS=$[ $DESTINATIONS - 1 ]
	tail -n $DESTINATIONS $DESTFILE > $DESTFILE.new
	mv -f $DESTFILE.new $DESTFILE
    done
fi

rm -rf $METAMAIL_TMPDIR
rm -f /tmp/relayfax2sendfax.sendto.$$
rm -f /tmp/relayfax2sendfax.mail.$$
rm -f /tmp/relayfax2sendfax.out.$$

exit