#! /bin/sh

#############################################################################
# print preprocessor for invocation of uniprint
# usage: uprint [-font <font>] [-size <size>] filename ...
# environment:
#   FONT=<font>
#       the name of a font file, e.g. LucidaBrightRegular or bodoni.ttf
#       the file must reside in the configured font path
#   FONTPATH=<font directory search path>
#       separate directory names with :
#   FONTSIZE=<size>
#   LPR=<optional print spooling command instead of lpr>
#       if you need to spool files not using the operating system's spooler
#   PRINTER=<printer>

#############################################################################
# set font and size preference
#font=cyberbit
#font=code2000
#font=georgia
#font=comic

font=${FONT-cyberbit}

#size=11
size=$FONTSIZE

# additional font directories to search for desired font
morefonts="$HOME/fonts:$HOME/ttfonts:$HOME/opt/fonts:$HOME/opt/ttfonts:/usr/ttfonts:/usr/X11/lib/X11/fonts/truetype"


#############################################################################
# setup up parameters

# font directories to search for desired font
FONTPATH=${FONTPATH-$YUDITDATA}
case "$FONTPATH" in
"")	FONTPATH="$morefonts";;
*)	FONTPATH="$FONTPATH:$morefonts";;
esac


# try to extract font and size specificatios from command line parameters
params=
while
    case "$1" in
    -f|-font)	font="$2"
		shift; shift
		true;;
    -s|-size)	size="$2"
		shift; shift
		true;;
    -*)		params="$params $1"
		shift
		true;;
    *)		false;;
    esac
do	true
done


# try to find font file
font=`basename $font .ttf`
fontfiles=`echo ${FONTPATH}: | sed -e "s,:,/$font.ttf ,g"`
fontfile=`ls $fontfiles 2> /dev/null | sed -e 1q`


#############################################################################
# construct parameters for uniprint
case "$fontfile" in
"")	fontspec=;;
*)	fontspec="-font $fontfile";;
esac

case "$size" in
"")	sizespec=;;
*)	sizespec="-size $size";;
esac


#############################################################################
# print files
for f in "$@"
do
	if type uniprint
	then	if [ "$LPR" != "" ]
		then	uniprint $params -wrap $fontspec $sizespec -out - "$f" | $LPR "$f"
		else	uniprint $params -wrap $fontspec $sizespec "$f"
		fi
	else	false
	fi
done


#############################################################################
# end
