:
#
# fax-send: script invoked by faxdaemon to send a single
#           fax document.
#
# Usage:
#
# fax-send <sender> <recipient> <file> <pagecount> <fax number>
#          <tries> <device>
#

SHELL=/bin/sh
# Give us an e-mail identity more descriptive than "root".
NAME="Z-Fax Send Program"
export SHELL NAME

translate_results() {

    case "$1" in
	1) results="The transmission program reported getting incorrect
arguments.  Check with your system administrator." ;;

	2) results="Fax device was in use by another program.
Check with your system administrator." ;;

	3) results="The transmission program reported the fax modem did not
respond to commands.  There may be a problem with the modem
on $device.  Check with your system administrator." ;;

        4) results="The transmission program reported the modem did not accept your fax
phone number of \"$faxnumber\".  Please check the number and try again." ;;

        5) results="The remote fax device did not answer after $tries tries.
No additional attempts will be made." ;;

	6) results="The remote fax device was busy after $tries tries.
No additional attempts will be made." ;;

	107) results="The transmission program reported that it could not
verify your Z-Fax license.  Check with your system administrator." ;;

	108) results="The transmission program reported the fax device $device
could not be opened.  Check with your system administrator." ;;

	109) results="The transmission program reported the fax file $faxfile
could not be opened.  Check with your system administrator." ;;

	110) results="The transmission program reported that there was a
problem translating the fax file.  Check with your system administrator." ;;

        *) results="Your fax aborted during transmission.  This could mean many
things, from noise on the phone line to the remote fax machine running
out of paper.  Check with the recipient to see what portion of your fax
you need to resend." ;;

    esac
}


# Begin main body of script.

if [ $# -ne 7 ]
then
    echo "Usage: $0 <sender> <recipient> <file> <fax number> <tries> <device>"
    exit 1
fi

# Set up default values.
TRANSMIT_DIR=/usr/spool/fax/outgoing
TRANSMIT_RETRY=2
TRANSMIT_FAIL=hold
sendprog=c2send
mail=mail

# Get parameters.
sender=$1
recipient=$2
faxfile=$3
pagecount=$4
faxnumber=$5
tries=$6
device=$7

faxid=`basename $faxfile`

if [ "$pagecount" = "1" ]
then
    pagecount="1 page"
else
    pagecount="$pagecount pages"
fi

# Read the configuration file.
ZFAXLIB=${ZFAXLIB:-"/usr/lib/Zfax"}
[ -r $ZFAXLIB/config ] && . $ZFAXLIB/config

# Attempt first transmission.
output=`$sendprog -s "$sender" "$device" "$faxnumber" "$faxfile" 2>&1`

# If transmission fails, attempt to retry.
sts=$?
retry=1
while [ "$sts" -ne 0 -a "$retry" -lt "$tries" ]
do
    sleep 90
    output=`$sendprog -s "$sender" "$device" "$faxnumber" "$faxfile" 2>&1`
    sts=$?
    retry=`expr "$retry" + 1 2> /dev/null`
done

# Handle results.
if [ "$sts" -eq 0 ]
then
    if [ ! -z "$recipient" ]
    then
        echo "Fax $faxid to $recipient at $faxnumber ($pagecount) transmitted successfully" | $mail $sender
    else
        echo "Fax $faxid to $faxnumber ($pagecount) transmitted successfully" | $mail $sender
    fi
    dequeuefax $faxid
    exit 0
else
    translate_results "$sts"
    case "$TRANSMIT_FAIL" in

      discard) dequeuefax $faxid
	       ( if [ ! -z "$recipient" ]
	         then
	             echo "Fax $faxid to $recipient at $faxnumber failed with status $sts:"
	         else
	             echo "Fax $faxid to $faxnumber failed with status $sts:"
	         fi
		 echo ""
	         echo "    $output"
		 echo ""
	         echo "$results"
		 echo ""
               ) | $mail $sender
               exit 1
	       ;;

#     Hold, or anything else.
      *)       qid=`queuefax -p f -R "$recipient" -n "$sender" $faxnumber $faxfile`
	       newID=`echo $qid | sed -e 's/Request ID is //'`
	       dequeuefax $faxid
               ( if [ ! -z "$recipient" ]
	         then
	             echo "Fax $faxid to $recipient at $faxnumber failed with status $sts:"
	         else
	             echo "Fax $faxid to $faxnumber failed with status $sts:"
	         fi
		 echo ""
	         echo "    $output"
		 echo ""
	         echo "$results"
		 echo ""
		 if [ "$newID" = "" ]
		 then
		     echo "The fax document could not be returned to the queue."
		 else
		     echo "Fax document returned to queue as request ID $newID for retry or deletion."
		 fi
               ) | $mail $sender
               exit 1
	       ;;
    esac
fi

exit 1
