#! /bin/sh

#############################################################################
# print preprocessor for invocation of uniprint
# usage: uprint [-r(emove)] [-t(itle) <title>] [-f(ont) <font>] [-s(ize) <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 ":"
#   $HOME/.fonts.conf (file)
#       Pango font configuration file, for use with paps;
#       here you can specify:
#       * directories with font files
#       * preferred fonts for styles "serif", "sans-serif", "monospace"
#       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>


#############################################################################
# process options
remove=false
title=
font=
size=
params=

while	case "$1" in
	-r|-remove)
		remove=true
		true;;
	-t|-title)
		title="$2"
		shift
		true;;
	-f|-font)
		font="$2"
		shift
		true;;
	-s|-size)
		size="$2"
		shift
		true;;
	-*)	params="$params $1"
		true;;
	*)	false;;
	esac
do	shift
done

# try to extract font and size specificatios from command line parameters
while
    case "$1" in
    *)		false;;
    esac
do	true
done


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


#############################################################################
# set font and size preference
#font=code2000
font=cyberbit
if $paps
then	font=monospace
	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"


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

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


if $paps
then	true
elif type uniprint 2> /dev/null
then	# 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 paps / uniprint

if $paps
then
	papsopt=

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

	# add header if available
	if paps --help | grep -- --header > /dev/null
	then	papsopt="$papsopt --header"
	fi

	# add font spec in proper syntax for paps version
	if paps --help | grep -- --family > /dev/null
	then	papsfontpar=--family
		papsfont="$font"
		papsopt="$papsopt $sizepaps"
	elif paps --help | grep -- --font= > /dev/null
	then	papsfontpar=--font
		papsfont="$font $size"
	fi

	# add dpi spec for old paps version
	if paps --help | grep -- --dpi > /dev/null
	then	papsopt="$papsopt --dpi 300"
	fi

elif type uniprint 2> /dev/null
then
	case "$fontfile" in
	"")	fontspec=;;
	*)	fontspec="-font $fontfile";;
	esac

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

fi


#############################################################################
# print all files

for file in "$@"
do

printed=true

if $paps
then	if [ -n "$papsfontpar" ]
	then	paps $papsfontpar "$papsfont" $papsopt "$file" | $LPR
	else	paps $papsopt "$file" | $LPR
	fi
elif type uniprint 2> /dev/null
then	if [ "$LPR" != "" ]
	then	uniprint $params -wrap $fontspec $sizespec -out - "$file" | $LPR
		#$LPR "$file.ps"
		#rm -f "$file.ps"
	else	uniprint $params -wrap $fontspec $sizespec "$file"
	fi
else	case `uname` in
	CYGWIN*|Windows*)
		if type notepad 2> /dev/null
		then
			dir=`dirname "$file"`
			base=`basename "$file"`
			cd "$dir"
			if type cmd.exe > /dev/null 2> /dev/null
			then	cmd.exe /c start /min notepad /p "$base"
			elif type command.com > /dev/null 2> /dev/null
			then	command.com /c start /min notepad /p "$base"
			else	notepad /p "$base"
			fi
		else	printed=false
		fi
		;;
	*)	printed=false
		;;
	esac
fi

if $printed
then	if $remove
	then	case "$file" in
		/tmp/*|/???/tmp/*|${MINEDTMP--}/*|${TMPDIR--}/*|${TMP--}/*|${TEMP--}/*)
			rm -f "$file";;
		esac
	fi
else	false
fi

done


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