:
#!/bin/sh
#
#	faxcover [-l]:	Make a cover sheet for a FAX transmission
#	Options:	-l	Request low resolution
#
#	Called automatically by faxsend.  May be modified to suit your tastes.
#
#	On entry the environment contains string variables which hold the
#	senders and receivers address, etc.:
#
#		$tokey	"Nickname" or number of recipient
#		$fromkey login name or -u USER option of sender
#		$note	Filename of cover sheet notes file
#
#		$NP	Number of pages following cover sheet
#		$P	Priority of Job
#
#		$n	name of sender
#		$c	company name of sender
#		$a1	first address line of sender
#		$a2	second address line of sender
#		$a3	third address line of sender
#		$a4	fourth address line of sender
#		$a5	fifth address line of sender
#		$v	voice telephone number of sender
#		$f	fax telephone number of sender
#		$e	E-mail address of sender
#
#		$N	name of recipient
#		$C	company name of recipient
#		$A1	first address line of recipient
#		$A2	second address line of recipient
#		$A3	third address line of recipient
#		$A4	fourth address line of recipient
#		$A5	fifth address line of recipient
#		$V	voice telephone number of recipient
#		$F	fax telephone number of recipient
#		$E	E-mail address of recipient
#
#	Standard output is connected to the output file.
#
#	If what you create is FAX format output, return
#	exit status 2.  If ASCII, return exit status 1.
#	If you fail, return some other exit status.
#
#	The optional [-l] flag requests a low resolution cover page.
#

#
#	Select type of coversheet, by setting this variable to
#	ascii or troff.  If the troff typesetting system isn't
#	available but has been selected here, then ascii will be used.
#
CoverSheetType=ascii

#
#	Overlay final cover sheet with a fax image (e.g. letterhead)
#	by setting this variable to the full pathname of the image.
#
CoverSheetOverlay=/usr/spool/fax/CoverSheet.fax

#
#	If set to "yes", use FaxJet (instead of text2fax) to process the Ascii
#	cover sheet text and notes file.  This allows, the notes to be PCL 4.
#
FaxJet=no

#
#	Give a name to the low res flag passed on the command line
#
lowres="$1"
mergeres="${lowres:--h}"

#
#	Location of the troff font library
#
fontlib=${EROFF_FONTDIR:-/usr/lib/font}

#
#	Allow override of variables passed by faxsend.
#	i.e. so that one can issue commands like this:
#
#		FAX_N="John Smith" faxsend 555-1212 file.txt
#
#	The override variables are named with a "FAX_" prefix.
#
#	NOTE: the "Quicker Send" feature of faxdir relies on having
#	$FAX_N override $N in this script.
#
n="${FAX_n:-$n}"
c="${FAX_c:-$c}"
a1="${FAX_a1:-$a1}"
a2="${FAX_a2:-$a2}"
a3="${FAX_a3:-$a3}"
a4="${FAX_a4:-$a4}"
a5="${FAX_a5:-$a5}"
v="${FAX_v:-$v}"
f="${FAX_f:-$f}"
e="${FAX_e:-$e}"

N="${FAX_N:-$N}"
C="${FAX_C:-$C}"
A1="${FAX_A1:-$A1}"
A2="${FAX_A2:-$A2}"
A3="${FAX_A3:-$A3}"
A4="${FAX_A4:-$A4}"
A5="${FAX_A5:-$A5}"
V="${FAX_V:-$V}"
F="${FAX_F:-$F}"
E="${FAX_E:-$E}"

#
#	Define a function to create an ASCII cover sheet
#		Note: feel free to modify this as needed
#
ascii_cover() {
	#
	#	gives names to the various line drawing characters
	#	
	ul=0xda		#upper left corner
	ur=0xbf		#upper right corner
	ll=0xc0		#lower left corner
	lr=0xd9		#lower right corner
	utee=0xc1	#tee going up
	dtee=0xc2	#tee going up
	rtee=0xc3	#tee going right
	ltee=0xb4	#tee going left
	hor=0xc4	#horizontal line
	ver=0xb3	#vertical line
	cross=0xc5	#cross
	#
	#	/usr/bin/digiprintf is a command line version of the
	#	printf(3 or S) subroutine.  It is much more powerful than
	#	echo(1 or C).  In addition to the usual printf style
	#	format specifiers, two new ones have been added:
	#		%P	Take a character argument and switches
	#			the pad character for strings from blank
	#			to that character. Used for drawing lines.
	#		%S	Like "%s" excepts centers string within
	#			the field width.
	#
	digiprintf "%P%c%77s%c\n" $hor $ul "" $ur
	digiprintf "%c%77s%c\n" $ver "" $ver
	digiprintf "%c%77S%c\n" $ver "F A C S I M I L E " $ver
	digiprintf "%c%77s%c\n" $ver "" $ver
	digiprintf "%c%77S%c\n" $ver "C O V E R   S H E E T" $ver
	digiprintf "%c%77s%c\n" $ver "" $ver
	digiprintf "%P%c%38s%c%38s%c\n" $hor $rtee "" $dtee "" $ltee
	digiprintf "%c%-38s%c%-38s%c\n" \
		$ver " DATE: `date`" $ver " PAGES: $NP plus cover" $ver
	digiprintf "%P%c%38s%c%38s%c\n" $hor $rtee "" $cross "" $ltee
	digiprintf "%c%38S%c%38S%c\n" $ver "F R O M" $ver "T O" $ver
	digiprintf "%P%c%38s%c%38s%c\n" $hor $rtee "" $cross "" $ltee
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $n" $ver " $N" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $c" $ver " $C" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $a1" $ver " $A1" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $a2" $ver " $A2" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $a3" $ver " $A3" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $a4" $ver " $A4" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $a5" $ver " $A5" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $v" $ver " $V" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" \
		$ver " $f (fax)" $ver " $F (fax)" $ver
	digiprintf "%c%-38.38s%c%-38.38s%c\n" $ver " $e" $ver " $E" $ver
	digiprintf "%P%c%38s%c%38s%c\n" $hor $ll "" $utee "" $lr
	#
	#	If a note file is given, tack it on the end of the cover sheet
	#
	echo; echo "NOTES:"
	if [ "Q$note" != Q ]
	then
		cat $note
	fi
}

#
#	Define a function to create a TROFF cover sheet
#		Note: feel free to modify this as needed
#
troff_cover() {
	#	Format cover sheet using troff
	#
	#	The first "cat...EOF" sets up the troff strings
	#	from the environment.  The second "cat...EOF"
	#	copies verbatim the text of the cover sheet.
	#
	#	The troff command line below is very basic,
	#	no fancy pic or tbl stuff is done in the document
	#
	res=
	if [ "Q$lowres" = Q-l ]
	then
		res=-Tlowres
	fi
	(cat <<EOF; cat <<\EOF) | troff -Tfax \
		| djet -Tfax -F$fontlib/devfax -R$fontlib/devfax/pk $res
.ds NP $NP
.nr pr $P
.ds n $n
.ds c $c
.ds a1 $a1
.ds a2 $a2
.ds a3 $a3
.ds a4 $a4
.ds a5 $a5
.ds v $v
.ds f $f
.ds e $e
.ds N $N
.ds C $C
.ds A1 $A1
.ds A2 $A2
.ds A3 $A3
.ds A4 $A4
.ds A5 $A5
.ds V $V
.ds F $F
.ds E $E
.ds NO $note
EOF
.in 0i
.ft HB
.ps 32
.vs 36
\D'l 7i 0'
.tl ''FACSIMILE COVER SHEET''
.sp -.5v
\D'l 7i 0'
.nf
.ps 12
.vs 14
.ft H
.ta .8i
.sp .25i
.in .3i
.mk zz
DATE:	\n(mo/\n(dy/\n(yr

FROM:	\*n
	\*c
	\*(a1
	\*(a2
	\*(a3
	\*(a4
	\*(a5
VOICE:	\*v
FAX:	\*f
E-MAIL:	\*e
.ft HB
.in 3.5i
.sp |\n(zzu
PAGES:	\*(NP plus cover

TO:	\*N
	\*C
	\*(A1
	\*(A2
	\*(A3
	\*(A4
	\*(A5
VOICE:	\*V
FAX:	\*F
E-MAIL:	\*E
.if \n(pr>0\{\
.de zz
.tl ''\f(HB\s24\(rh URGENT \(lh\s0\fP''
.rm zz
..
.wh -.75i zz\}
.if !'\*(NO''\{\
.in 0
.ta 8u*\w'0'u +8u*\w'0'u +8u*\w'0'u +8u*\w'0'u +8u*\w'0'u +8u*\w'0'u \
   8u*\w'0'u +8u*\w'0'u +8u*\w'0'u +8u*\w'0'u
\D'l 7i 0'
.sp 1v
.ps 10
.vs 12
.ft CW
.so \*(NO\}
EOF
}

#
#	If troff cover sheet has been requested, see if the
#	necessary tools are present
#
if [ "$CoverSheetType" = "troff" -a "$EROFF_DIR" = "" ]
then
	if [ ! -x /usr/bin/troff -o ! -x /usr/bin/djet -o ! -d /usr/lib/dwb ]
	then
		# If we can't find troff or djet, then force ascii.
		# If troff is there, but no dwb directory, then it must
		# be the much older C/A/T troff (xenix) which we can't use.
		CoverSheetType=ascii
	fi
fi

#
#	Now, output one of the cover sheets, optionally overlaid with
#	the image in $CoverSheetOverlay.
#
case "$CoverSheetType" in
troff)
	if [ "$CoverSheetOverlay" != ""  -a -r "$CoverSheetOverlay" ]
	then
		tmpi=/tmp/coveri$$
		tmpo=/tmp/covero$$
		troff_cover > $tmpi
		faxmerge $mergeres -o $CoverSheetOverlay,1 $tmpi $tmpo
		cat $tmpo
		rm -f $tmpi $tmpo
		exit 2
	else
		troff_cover
		exit 2
	fi
	;;
ascii|*)
	if [ "$CoverSheetOverlay" != ""  -a -r "$CoverSheetOverlay" ]
	then
		tmpi=/tmp/coveri$$
		tmpo=/tmp/covero$$
		if [ $FaxJet = yes ]; then
			ascii_cover | faxjet -T2 $lowres -o $tmpi
		else
			ascii_cover | text2fax $lowres - $tmpi
		fi
		faxmerge $mergeres -o $CoverSheetOverlay,1 $tmpi $tmpo
		cat $tmpo
		rm -f $tmpi $tmpo
		exit 2
	else
		if [ $FaxJet = yes ]; then
			tmpi=/tmp/coveri$$
			ascii_cover | faxjet -T2 $lowres -o $tmpi
			cat $tmpi
			rm -f $tmpi
			exit 2
		else
			ascii_cover
			exit 1
		fi
	fi
esac
