#!/bin/sh
#	(c) Copyright 1993 Digi, Intl.  All Rights Reserved.
#
#	/usr/bin/fax_uux	- Fake UUX for simulating mail based FAX
#
#	A kludge for those without the stomach to install/customize either
#	sendmail, smail, or mmdf (urp!).  The 'proper' way is, of course, to
#	set up the mailer to route addresses in the FAX domain to the faxsend
#	command.
#
#	This script looks for command line args that appear like this:
#
#		uux [options] fax!rmail [(]nickname_or_number[)]
#
#	If this command line is detected, it is converted into the appropriate
#	faxsend command line.  Else, the regular uux is executed.  This command
#	line is usually what your mailer will execute to deliver remote mail,
#	but you may have to change the recognition code if you discover the
#	command line used on your system is different.
#
#	When installed, people can access the fax service via
#	mail addressed like these:
#		mail fax!555-1212			(telno given)
#		mail fax!555-1212!Rick.Richardson	(telno and name given)
#
#	to install this script, as root:
#		mv /usr/bin/uux /usr/bin/uucp_uux
#		cp /usr/bin/fax_uux /usr/bin/uux
#		ls -l /usr/bin/*uux
# -rwsr-xr-x   1 uucp     uucp        2025 Mar 28 10:57 /usr/bin/fax_uux*
# ---s--x--x   1 uucp     uucp       63408 Mar 27 23:43 /usr/bin/uucp_uux*
# -rwsr-xr-x   1 uucp     uucp        1942 Mar 28 11:03 /usr/bin/uux*
#
#	to deinstall this script, as root:
#		mv /usr/bin/uucp_uux /usr/bin/uux
#		ls -l /usr/bin/*uux
# -rwsr-xr-x   1 uucp     uucp        2025 Mar 28 10:57 /usr/bin/fax_uux*
# ---s--x--x   1 uucp     uucp       63408 Mar 27 23:43 /usr/bin/uux*
#
#	NOTE:	On SCO UNIX, the mmdf system *still* gets in the way,
#		and these additional steps are required:
#		1) Add the following line to /usr/lib/uucp/Systems:
#			fax Never
#		2) login as mmdf (su - mmdf from root is ok)
#		3) cd /usr/mmdf/table
#		4) tools/uulist
#		5) ./dbmbuild
#

# mail fax!555-1212		->	uux - -r fax!rmail (555-1212)
# mail fax!555-1212!Tom.Smith	->	uux - -r fax!rmail (555-1212!Tom.Smith)

#
#	Find out if its a FAX, and record the destinations in $who
#
mode=uux
for arg in "$@"
do
	case "$arg" in
	fax!rmail)	mode=fax; who="";;
	*)		who="$who $arg";;
	esac
done

case $mode in
uux)
	#
	# Hand off to real uux
	#
	exec /usr/bin/uucp_uux "$@"
	;;
fax)
	cat - >/tmp/faxmail$$			# Make copy of message
	for w in "$who"				# Process each destination
	do
		# Delete enclosing parens, if any
		telno=`echo "$w" | sed 's/[()]//g'`
		name=`expr "$telno" ':' '.*!\(.*\)'`
		if [ "Q$name" = Q ]; then
			# Just a number given
			/usr/bin/mailfaxer "$telno" < /tmp/faxmail$$
			rc=$?
		else
			# number!name given
			telno=`expr $telno ':' '\(.*\)!.*'`
			/usr/bin/mailfaxer -t "$name" "$telno" < /tmp/faxmail$$
			rc=$?
		fi
	done
	rm -f /tmp/faxmail$$			# Remove copy
	exit $rc
	;;
esac
