#2.1 90/07/18
SCRIPTS	= gfnttab
TD	= testdir
PSW	= \
	widthps/DESC \
	widthps/AB widthps/BR widthps/HB widthps/I  widthps/PX \
	widthps/AI widthps/BX widthps/HO widthps/NB widthps/R \
	widthps/AR widthps/C  widthps/HW widthps/NI widthps/S \
	widthps/AX widthps/CB widthps/HX widthps/NR widthps/S2 \
	widthps/B  widthps/CO widthps/Hb widthps/NX widthps/ZC \
	widthps/BB widthps/CX widthps/Hi widthps/PB widthps/ZD \
	widthps/BI widthps/Hr widthps/PI widthps/BO widthps/H \
	widthps/Hx widthps/PR widthjt/DESC widthlj/DESC

include ../makeincl

all:	subst.done $(PSW) dit2catwid $(SCRIPTS) ljwidths widths

$(PSW):
	@eval `echo $@ | sed -e 's/\(.*\)\/\([^\/]*\)$$/d=\1 f=s.\2/'` ; \
	    cd $$d ; get $(GFLAGS) $$f

widths:	gfnttab dit2catwid
	-rm -f gfnttab.log
	for i in width* ; \
	do \
	    echo "Processing $$i" ; \
	    ./gfnttab $$i >> gfnttab.log ; \
	done

subst.done:	../makeincl ../sedscript
	rm -f $(SCRIPTS)
	touch subst.done

../utils/pk2ditwid:
	cd ../utils ; make pk2ditwid

# The "for ps in" list below is sort of monotonic increasing ratios
# away from point size 10, further, the larger fonts are the ones
# most likely to not be EXACTLY what pointsize they say they are
# (eg: my PK 36 is actually closer to 32)
sepwidths: ../utils/pk2ditwid
	rm -f pk2sep.log pk2sep.err
	@ $(IGNORESH) if [ -d $(LJF) ] ; \
	then \
	    fontlist=`find $(LJF) -type f \
		'(' -name '*.*.pk' -o -name '*.*.sfp' ')' -print | \
		sed -e 's/^\.\///' -e 's/\..*$$//' -e 's/.*\///' | sort -u` ; \
	    for font in $$fontlist ; \
	    do \
		case $$font in \
		?) ;; \
		??) ;; \
		*) \
		    echo "$$font can't be a psroff/troff font!" \
			>> pk2dit.err ; \
		    continue ; \
		    ;; \
		esac ; \
		file= ; \
		for ps in 10 11 9 12 8 7 14 6 16 18 20 22 24 28 36 ; \
		do \
		    file= ; \
		    if [ -r $(LJF)/$$font.$$ps.pk ] ; \
		    then \
			file=$(LJF)/$$font.$$ps.pk ; \
		    elif [ -r $(LJF)/$$font.$$ps.sfp ] ; \
		    then \
			file=$(LJF)/$$font.$$ps.sfp ; \
		    fi ; \
		    if [ -n "$$file" ] ; \
		    then \
			if [ "$$font" = S ] ; \
			then \
			    args=-S ; \
			else \
			    args= ; \
			fi ; \
			echo "pk2ditwid: $$file" >> pk2sep.log ; \
			../utils/pk2ditwid $$args $$file >> pk2sep.log \
			    2>> pk2sep.err ; \
			if [ $$? != 0 ] ; \
			then \
			    echo "pk2ditwid (on $$font) failed - check pk2sep.log" \
				>> pk2sep.err ; \
			else \
			    mv $$font widthlj/$$font.$$ps ; \
			fi ; \
		    fi ; \
		done ; \
	    done ; \
	    for ps in 10 11 9 12 8 7 14 6 16 18 20 22 24 28 36 ; \
	    do \
		for i in $$fontlist ; \
		do \
		    if [ ! -r widthlj/$$i.$$ps ] ; \
		    then \
			continue ; \
		    fi ; \
		    case $$i in \
			S) \
			    args="-s widthlj/R.$$ps widthlj/S.$$ps" ; \
			    if [ ! -r widthlj/R.$$ps ] ; \
			    then \
				continue ; \
			    fi ; \
			    ;; \
			*) \
			    args="widthlj/S.$$ps widthlj/$$i.$$ps" ; \
			    ;; \
		    esac ; \
		    echo "dit2catwid: $$i.$$ps" >> pk2sep.log ; \
		    ./dit2catwid $$args widthlj/ft$$i.$$ps >> pk2sep.log \
			2>> pk2sep.err ; \
		done ; \
	    done ; \
	fi

# The "for ps in" list below is sort of monotonic increasing ratios
# away from point size 10, further, the larger fonts are the ones
# most likely to not be EXACTLY what pointsize they say they are
# (eg: my PK 36 is actually closer to 32)
ljwidths: ../utils/pk2ditwid
	rm -f pk2dit.log pk2dit.err
	@ $(IGNORESH) if [ -d $(LJF) ] ; \
	then \
	    fontlist=`find $(LJF) -type f \
		'(' -name '*.*.pk' -o -name '*.*.sfp' ')' -print | \
		sed -e 's/^\.\///' -e 's/\..*$$//' -e 's/.*\///' | sort -u` ; \
	    for font in $$fontlist ; \
	    do \
		case $$font in \
		?) ;; \
		??) ;; \
		*) \
		    echo "$$font can't be a psroff/troff font!" \
			>> pk2dit.err ; \
		    continue ; \
		    ;; \
		esac ; \
		file= ; \
		for ps in 10 11 9 12 8 7 14 6 16 18 20 22 24 28 36 ; \
		do \
		    if [ -r $(LJF)/$$font.$$ps.pk ] ; \
		    then \
			file=$(LJF)/$$font.$$ps.pk ; \
			break ; \
		    elif [ -r $(LJF)/$$font.$$ps.sfp ] ; \
		    then \
			file=$(LJF)/$$font.$$ps.sfp ; \
			break ; \
		    fi ; \
		done ; \
		if [ -n "$$file" ] ; \
		then \
		    echo "Best fit for $$font is $$file" >> pk2dit.log ; \
		    if [ "$$font" = S ] ; \
		    then \
			args=-S ; \
		    else \
			args= ; \
		    fi ; \
		    echo "Creating widthlj/$$font from $$file" ; \
		    ../utils/pk2ditwid $$args $$file >> pk2dit.log \
			2>> pk2dit.err ; \
		    if [ $$? != 0 ] ; \
		    then \
			echo "pk2ditwid (on $$font) failed - check pk2dit.log" \
			    >> pk2dit.err ; \
		    else \
			mv $$font widthlj/$$font ; \
		    fi ; \
		fi ; \
	    done ; \
	fi
	@test ! -s pk2dit.err || (cat pk2dit.err ; exit 1)
	@rm -f pk2dit.err

install:

installwidths:
	@$(IGNORESH) ; \
	test -w $(FONTDIR) || (echo "Install as root" ; exit 1 ; ) ; \
	for i in width* ; \
	do \
	    base=`echo $$i | sed -e 's/.*width//'` ; \
	    test -d $(FONTDIR)/$$base || mkdir $(FONTDIR)/$$base ; \
	    test -w $(FONTDIR)/$$base || (echo "Install as root" ; exit 1 ; ) ; \
	    files=`echo width$$base/ft*` ; \
	    if [ "$$files" = "width$$base"'/ft*' ] ; \
	    then \
		echo "No $$base widths to install" ; \
		continue ; \
	    fi ; \
	    echo "Installing $$base widths" ; \
	    mv width$$base/ft* $(FONTDIR)/$$base ; \
	done

cmp:
	test -d $(TD) || mkdir $(TD)
	@$(IGNORESH)for i in width* ; \
	do \
	    base=`echo $$i | sed -e 's/.*width//'` ; \
	    cd $$i ; \
	    for j in ft* ; \
	    do \
		if [ ! -f $$j ] ; \
		then \
		    continue ; \
		fi ; \
		echo "Comparing $$j to $(FONTDIR)/$$base/$$j" ; \
		set +e ; \
		cmp -l $$j $(FONTDIR)/$$base/$$j ; \
		set -e ; \
	    done ; \
	    cd ../ ; \
	done > $(TD)/widthdiff 2>&1

test:	dit2catwid $(SCRIPTS)
	test -d $(TD) || mkdir $(TD)
	cp widthps/S $(TD)/S
	sed -e '1,/charset/d' widthps/S2 >> $(TD)/S
	./dit2catwid -v $(TD)/S widthps/R $(TD)/ftR > $(TD)/ftR.nout
	@echo "Comparing $(TD)/ftR and $(FONTDIR)/ps/ftR"
	-cmp -l $(TD)/ftR $(FONTDIR)/ps/ftR
	./dit2catwid -vc $(TD)/S widthps/R $(TD)/ftR.c > $(TD)/ftR.c.nout
	./dit2catwid -sv widthps/R $(TD)/S $(TD)/ftS > $(TD)/ftS.nout
	@echo "Comparing $(TD)/ftS and $(FONTDIR)/ps/ftS"
	-cmp -l $(TD)/ftS $(FONTDIR)/ps/ftS
	./dit2catwid -svc widthps/R $(TD)/S $(TD)/ftS.c > $(TD)/ftS.c.nout

clean:
	rm -rf dit2catwid $(SCRIPTS) */ft* *.o $(TD) *.fonts.new
	rm -f gfnttab.log pk2dit.log pk2sep.log pk2dit.err pk2sep.err

dit2catwid.o:	../defs.h

../cattab.o:	../defs.h
	@echo "cattab.o is out of date, please run make troff2ps in top level first"
	exit

dit2catwid:	../cattab.o dit2catwid.o
	$(CC) $(CFLAGS) -o dit2catwid dit2catwid.o ../cattab.o $(LIBFLAGS)
