#! /bin/sh
name=$1
tmp=/tmp/txttosrc.$$
trap 'rm -f $tmp.* ; exit' 0 1 2 15
pbmtext -font font.pbm < "$name" > $tmp.1
set `< $tmp.1 tr -cs '[P0-9]' '\012' | sed -n -e 2,3p`
x=$1
y=$2
# UTSLing reveals that the left border is two characters wide and the top
#  border is one character high.  Why isn't this documented and/or variable?
# We want to preserve one X and two Y pixels of the border.
x=`expr $x - 17`
y=`expr $y - 12`
pnmcut 17 12 $x $y < $tmp.1 > $tmp.2
if [ $x -ge 480 ]; then
	pnmcut 0 0 480 $y < $tmp.2 > $tmp.1
else
	# pbmmake has -white and -black reversed; it should take -0 and -1!
	pbmmake -white `expr 480 - $x` $y | pnmcat -lr $tmp.2 - > $tmp.1
fi
if [ $y -ge 288 ]; then
	pnmcut 0 0 480 288 < $tmp.1 > $tmp.2
else
	# ditto above comment on pbmmake
	pbmmake -white 480 `expr 288 - $y` | pnmcat -tb $tmp.1 - > $tmp.2
fi
(
	echo '#ifndef lint'
	echo '/* As far as I can tell the copyright requires me to include'
	echo '   this notice even in the resulting binary, sigh.... */'
	echo ''
	echo static char copyright'[] = "'\\
	echo 'Characters in this bitmap are taken from the font'\\n\\
	egrep '^#' < font.pbm | sed -e 's/^# *//' -e s/\"/\\\\\"/g -e 's/^/	/' -e 's/^[ 	]*$//' -e s/\$/\\\\n\\\\/
	echo \\n\\
	echo Their arrangement is in the public domain.\\n\\
	echo '";'
	echo '#endif /* #ifndef lint */'
	echo unsigned char "$name"_img_bits'[] = {'
	pbmtoxbm < $tmp.2 |
		sed -e 1,3d -e s/0x//g -e 's/};//' |
		cvtbase x d |
		sed -e 's/^ //' |
		awk '{ if ((NR%4) == 0) print; else printf("%s",$0); }' |
		sed -e 's/^/ /'
	echo '};'
) > blockade-info-"$name".c
