#! /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 ":"
#   $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>


#############################################################################
# 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
		elif type lp
		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


# 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 paps / uniprint

if $paps
then
	papsopt=

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

	# add header option if configurable header 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

else

	case "$fontfile" in
	"")	fontspec=;;
	*)	fontspec="-font $fontfile";;
	esac

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

fi


#############################################################################
# print

printed=true

if $paps
then	if [ -n "$papsfontpar" ]
	then	paps $papsfontpar "$papsfont" $papsopt "$1" | $LPR
	else	paps $papsopt "$1" | $LPR
	fi
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	case `uname` in
	CYGWIN*|Windows*)
		if type notepad
		then
			dir=`dirname "$1"`
			base=`basename "$1"`
			cd "$dir"
			if type cmd.exe > /dev/null
			then	cmd.exe /c start /min notepad /p "$base"
			elif type command.com > /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
  case "$remove" in
  -r)	case "$1" in
	/tmp/*|/???/tmp/*|${MINEDTMP--}/*|${TMPDIR--}/*|${TMP--}/*|${TEMP--}/*)
		rm -f "$1";;
	esac;;
  esac
else
  false
fi


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