#!/bin/sh
USAGE="USAGE: `basename $0` [-c cols] [-l lines] [files ...]"
text2ps=/usr/local/text2ps
ul='/usr/ucb/ul -t pslpr'
lines=66
cols=80
xmargin=18
ymargin=18
underline=false
while getopts "ul:c:x:y:" opt
do
	case "$opt" in
	x)
		xmargin="$OPTARG"
		;;
	y)
		ymargin="$OPTARG"
		;;
	l)
		lines="$OPTARG"
		;;
	c)
		cols="$OPTARG"
		;;
	u)
		underline=true
		;;
	\?)
		echo "$USAGE"
		exit 2
		;;
	esac
done
shift `expr $OPTIND - 1`

cat <<'<EOF>'
%!PS-Adobe-1.0
/lpprint
{
64 dict begin
/lines exch def
/columns exch def
/ymargin exch def
/xmargin exch def
/do_underline exch def

/f
{
	begin
		font setfont
		underline
	end
	/underline exch def
}
def
/s
{
	/dirty true def
	underline
	{
		dup length 1 1 3 -1 roll
		{
			pop (_) show
		}
		for
		dup stringwidth pop neg 0 rmoveto
	}
	if
	show
}
def
/p
{
	showpage
	restore
	save
}
def
/l
{
	/dirty true def
	/cy cy ls sub def
	cy y lt
	{
		p
	}
	{
		x cy moveto
	}
	ifelse
}
def
/e
{
	dirty
	{
		p
	}
	if
}
def

clippath pathbbox
/Y exch ymargin sub def
/X exch xmargin sub def
/y exch ymargin add def
/x exch xmargin add def

/ls Y y sub lines div def

/Courier findfont setfont

[
	X x sub
	(a) stringwidth pop columns mul
	div

	0 0 ls 0 0
]

dup
2 dict dup /fn exch def
begin
	/Courier findfont exch makefont /font exch def
	/underline false def
end
dup
2 dict dup /fb exch def
begin
	/Courier-Bold findfont exch makefont /font exch def
	/underline false def
end
dup
2 dict dup /fu exch def
begin
	do_underline
	{
		/Courier findfont exch makefont /font exch def
		/underline true def
	}
	{
		/Courier-Oblique findfont exch makefont /font exch def
		/underline false def
	}
	ifelse
end
pop

fn f
/cy Y ls sub ls 4 div add def
x cy moveto
/dirty false def
/lpend
{
	restore
	end
}
def
save
}
bind
def
<EOF>

echo $underline $xmargin $ymargin $cols $lines lpprint
$text2ps -u "$ul" ${1+"$@"}
echo lpend
