:
#
#	faxnotify spooldir subdir cfile reason remote_id
#
#	This script is automatically called by "faxsrv" after
#	a FAX is sent or a new FAX is received.  If you change
#	this script, be careful to run CPU intensive processes
#	in the background to avoid tying up the server for long
#	periods of time.
#
#	spooldir	/usr/spool/fax
#
#	subdir==tx
#			cfile		[cC]?????
#					U sender_login_name
#					u faxsend_-uflag_name
#					P phone
#					N nickname
#					T qtime ascii_qtime
#					L pages [pagesOK]
#					H localid
#					h page_header
#					s original-source-file-name
#					A? text file in job
#					FL low res fax file in job
#					FH high res fax file in job
#			reason		normal			[job sent]
#					retries (subreason)	[failed]
#					retry (number)		[warning]
#					bogus
#			remote_id	(up to 20 characters)
#
#	subdir==rx
#			cfile		[cC]?????
#					H header
#					T rtime ascii_rtime
#					F  fax_file_name
#					FH fax_file_name (high res)
#					FL fax_file_name (low res)
#					L pages
#
#			reason		normal
#			remote_id	(up to 20 characters)
#

#
#	Define the default user to notify on receive
#		(also, make change in /usr/spool/fax/faxroute)
#
FAXADM=root; export FAXADM

#
#	Select automatic printing, no is 0, yes is 1
#
AUTOPRINT=0
#
#	Select automatic deletion when auto printing, no is 0, yes is 1
#
AUTODELETE=0
#
#	Select mail to sender
#		0 - never mail notifications
#		1 - always mail notifications
#		2 - only mail failure notifications
#
MAILONSEND=1
#
#	Select mail on receive
#
MAILONRECV=1
#
#	Select mailer to use
#
if [ -f /xenix ]
then
	MAILER=mail
else
	MAILER=mailx
fi
#
#	Select whether to save undeliverable faxes in $HOME/fax/dead/*
#	Users that want this must also create the directory beforehand.
#
SAVEDEAD=0

if [ $# -lt 5 ]
then
	exit 1;
fi

spooldir="$1"
subdir="$2"
cfile="$3"
reason="$4"
shift; shift; shift; shift; remote_id="$*"

case "$subdir" in
tx)
	exec 0< $spooldir/$subdir/$cfile
	sender=
	phone="?"
	qtime="first opportunity"
	pages="? pages"
	nickname=
	sourcefiles=
	while read i x y
	do
		case "$i" in
		U)	sender=$x;;
		P)	phone=$x;;
		N)	nickname=$x;;
		T)	qtime="$y";;
		s)	sourcefiles="$sourcefiles	$x
"
			;;
		L)	if [ "Q$x" = Q1 ]; then pages="1 page"
			else pages="$x pages"; fi
			okpages=$x
			;;
		l)	okpages=$x;;
		M)	MAILONSEND=$x;;
		esac
	done

	if [ "$MAILONSEND" = 0 ]; then exit 0; fi

	if [ "Q$sender" = Q ]; then exit 0; fi
	#
	#	If some users always want mail, even if MAILONSEND is not 1,
	#	then list their names in this case statement.
	#
	case "$sender" in
	USER-1|USER-2)	MAILONSEND=1;;
	esac
	#
	ereason=
	mail=1
	case $reason in
	normal)
		ereason="was transmitted. Remote FAX ID was '$remote_id'"
		if [ "$MAILONSEND" != 1 ]; then mail=0; fi
		;;
	"retry"*)
		ereason="is on $reason.  Continuing to attempt sending this job"
		;;
	"retries"*)
		ereason="was deleted -- too many $reason"
		;;
	bogus)
		ereason="was deleted - invalid request"
		;;
	esac
	saved=
	if [ "$SAVEDEAD" = 1 -a "$reason" != normal ]
	then
		home=`grep "^$sender:" /etc/passwd | cut -f6 -d:`
		dead=$home/fax/dead
		if [ -d "$dead" ]
		then
			cp $spooldir/$subdir/$cfile $dead
			chown $sender $dead/$cfile
			exec 0< $spooldir/$subdir/$cfile
			while read i x y
			do
				case "$i" in
				A*|F*)	cp $x $dead
					chown $sender $dead/`basename $x`
					;;
				esac
			done
			saved="Files have been saved in \$HOME/fax/dead/*."
		fi
	fi
	if [ "$mail" = 1 ]
	then
		cat <<-EOF | $MAILER -s "Fax Request $cfile" $sender \
			>/dev/null 2>&1

		Your fax job $cfile ($pages):
		to be sent to $nickname ($phone) at $qtime,
		consisting of files:
			$sourcefiles
		$ereason.
		$saved

		EOF
	fi
	;;
rx)
	#	List of remote ID's we never notify or print for
	case "$remote_id" in
	no_notify*)	exit 0;;
	esac

	exec 0< $spooldir/$subdir/$cfile
	header="?"
	rtime="?"
	pages="? pages"
	user=$FAXADM
	while read i x y
	do
		case "$i" in
		F*)	dfile="$spooldir/$subdir/$x";;
		T)	rtime="$y";;
		U)	user="$x";;
		L)	if [ "Q$x" = Q1 ]; then pages="1 page"
			else pages="$x pages"; fi
			;;
		esac
	done
	if [ "Q$user" != Q -a "$MAILONRECV" = 1 ]
	then
		cat <<-EOF | $MAILER -s "Fax $dfile Rcvd from $remote_id" \
				"$user" >/dev/null 2>&1

		Fax job $dfile ($pages) from $remote_id
		was received at $rtime.

		EOF
	fi
	if [ "Q$AUTOPRINT" = Q1 ]
	then
		(
			faxprint $dfile >/dev/null 2>&1
			if [ "Q$AUTODELETE" = Q1 ]
			then
				rm -f $spooldir/$subdir/$cfile $dfile
			fi
		) &	# Done in background to keep from tying up server
	fi
	;;
esac
exit 0
