#! /bin/sh

#############################################################################
# print preprocessor for invocation of uniprint
# usage: uprint [-font <font>] [-size <size>] filename ...
# print layout configuration:
#   FONT=<font>
#       the name of a font file, e.g. LucidaBrightRegular or bodoni.ttf
#       the file must reside in the configured font path
#   FONTSIZE=<size>
#   FONTPATH=<font directory search path>
#       for use with uniprint only; separate directory names with ":"
#   FONTDPI=<output resolution>
#       for use with paps only; default 300
#   $HOME/.fonts.conf (file)
#       for use with paps only; directories with font files, 
#       preferred fonts for styles "serif", "sans", "mono" etc.
#       see http://fontconfig.org/fontconfig-user.html for patterns
#
# spooling configuration:
#   LPR=<optional print spooling command instead of lpr or lp>
#       if you need to spool files not using the operating system's spooler
#   PRINTER=<printer>


#############################################################################
# detect remove-after print option
case "$1" in
-r)	remove=-r
	shift;;
*)	remove=;;
esac


#############################################################################
# print multiple files
case "$2" in
?*)	for f in "$@"
	do	$0 $remove $f
	done
	exit;;
esac


#############################################################################
# determine print layout program
if type paps
then	paps=true
	# determine print spooling program
	case "$LPR" in
	"")	if type lpr
		then LPR=lpr
		else LPR=lp
		fi;;
	esac
else	paps=false
fi


#############################################################################
# set font and size preference
#font=code2000
font=cyberbit
if $paps
then	font=mono
	size=10
fi

font=${FONT-$font}

size=${FONTSIZE-$size}

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


#############################################################################
# set print preference
FONTDPI=${FONTDPI-300}


#############################################################################
# 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


if $paps
then	true
else	# 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`
fi


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

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


#############################################################################
# print
if $paps
then	paps --family "$font" $sizepaps --dpi $FONTDPI "$1" | $LPR
elif type uniprint
then	if [ "$LPR" != "" ]
	then	uniprint $params -wrap $fontspec $sizespec -out - "$1" | $LPR
		#$LPR "$1.ps"
		#rm -f "$1.ps"
	else	uniprint $params -wrap $fontspec $sizespec "$1"
	fi
else	echo uniprint not found >&2
	false
fi

case "$remove" in
-r)	case "$1" in
	/tmp/*|/???/tmp/*|${MINEDTMP--}/*|${TMPDIR--}/*|${TMP--}/*|${TEMP--}/*)
		rm -f "$1";;
	esac;;
esac


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