#	Copyright 1985, 1986, 1987, 1988, 1989, 1990 Chris Lewis
#		All Rights Reserved
#
#	This makefile requires System V compatible "make".  *Not* V7
#	vanilla BSD or Ultrix or Sun.  Most modern systems will have
#	a copy of the SV one somewhere (eg: .../s5bin/make).
#
#    Permission to copy and further distribute is freely given provided
#    this copyright notice remains intact and that this software is not
#    sold for profit.
#
#	Project:	Generic Troff drivers
#	Module:		Makefile
#	Author: 	Chris Lewis
#	Specs:		Makefile for Generic Troff drivers
#	Note:		This is a System V Makefile, so you may
#			have some problems making a few of the items.
#
#ident  "@(#)Makefile: 2.1 Copyright 90/07/18 16:47:31 Chris Lewis"

#	Use s5make on Ultrix and Sun!

#	Set to where you want the user-interfaces to go
BINDIR	= /usr/lbin
#BINDIR	= /usr/tmp/bin

#	Testing type (default postscript (ps)):
#	Use lj for laserjet.
TTYPE	= ps

#	Libraries for Troff2 internals, created if it doesn't exist.
#	This *must* be a directory all by itself with no other stuff
#	in it.
LIBDIR	= /usr/lib/troff2
#LIBDIR = /usr/tmp/troff2

#	Directory for Laserjet fonts.
LJF	= $(LIBDIR)/lib/lj

#	nroff/troff macro directory for tmac.t2.
T2DIR	= /usr/lib/tmac

#	Where your real macros are
RTMACDIR = /usr/lib/tmac
#RTMACDIR = /usr/tmp/tmac

#	upper level man directory, with subdirectories man1, man2 etc.
MANDIR	= /usr/man/l_man
#MANDIR	= /usr/tmp/man

#	Change this at your peril - your troff *must* support -F!
#	(font width files (ft??) are placed under FONTDIR/<width>/ft??)
#	(see lib/psrofflib.S)
FONTDIR	= /usr/lib/font
#FONTDIR = /usr/tmp/font

#	Comment this out if you don't have shared libraries ala 386/ix
LIBFLAGS = -lc_s

#	Bourne shell.  *All* Bourne shells with the exception of extremely
#	braindamaged ones (such as Ultrix or vanilla BSD) will work fine.
#	Ultrix/Dynix may need the ... s5bin/sh one.  Korn or bash
#	*should* work just fine, but I've never tested 'em.
#
#	This is also here for make, but if you have SHELL defined in your
#	*own* environment, you can't trust this to work anyways.
#
SHELL	=	/bin/sh

#	Try "make testbug"  If make stops, uncomment IGNORESH and
#	try "make testbug" again.  If it still stops, complain to your
#	vendor - your shell is BADLY broken.
#IGNORESH = set +e ;

#	How to ensure that shell scripts use the *right* shell.
#	"#! /bin/sh" may work for you.  ": /bin/sh" may work for you.
#
#	Frankly, none of this crap is necessary on a sane O/S.
#	(Even Xenix gets this right for crying out loud!  But *noooo*
#	Not Ultrix or Sun or Dynix BSD universe....)
#
#	": /usr/bin/sh5" works for Ultrix.
#	Do not use $(SHELL) in this line because if your environment has
#	SHELL defined, it may *override* the above SHELL definition.
#
#	If you have to get a "#" into STARTSHELL, the Makefile won't
#	like it.  In that case, if you have a System V compatible echo
#	(one that understands \nnn), use:
#	STARTSHELL = "`echo '\043! <your shell here>'`"
#	If you don't have a System V compatible echo (eg: Ultrix or
#	pure BSD), use this instead:
#	STARTSHELL = `cat STARTSHELL`
#	then create a file called STARTSHELL with the right stuff
#	in it.  Eg: echo "#! <your shell here>" > STARTSHELL
#
#	System V usually:
STARTSHELL = : /bin/sh
#	System V echo compliance, but needs #!: (Xenix?)
#STARTSHELL = `echo '\043! /bin/sh'`
#	Pure BSD (ala Ultrix) echo "#! /bin/sh" > STARTSHELL" first.
#STARTSHELL = `cat STARTSHELL`

#	Whether you need "cc -c"'d font width tables for troff.
COMPILE	=

#	Compile flags needed to compile things on your machine.
#	Usually just -O
#
#	PROFBUG is for 386/ix 1.0.6 mcrt making a mistake on argument
#	vector on entry to main.  Only troff2.c has the additional parts
#	to make profiling work on 386ix.  You'd normally want just -p
#	and/or -g.
#
#CCFLAGS = -p -g -DPROFBUG
#	Chris's wierd, not yet quite working NULL dereference checker
#CCFLAGS	= -O -Ya,`pwd/t` -g -DNULLCHECK
#	Chris's other wierd, but working, NULL dereference checker.
#CCFLAGS	= -O `pwd`/ifile
CCFLAGS = -O

#	If you're using gcc or something else, change this:
CC	= cc

#	Do not comment any of the following out.  If you aren't going
#	to use TeX PK fonts with HP Laserjet output, just NEVER execute
#	makebuildljfonts.
#
#	If you have Rick Richardson's sfp2pk, set it's full path here:
#	Only of interest for LJ/buildfonts.  If you don't have sfp2pk,
#	leave this alone, the code won't try to run it.
SFP2PK	= /usr4/public/bin/sfp2pk
#	If you have TeX fonts onboard, list the directories that
#	contain them.  Only of interest for LJ/buildfonts.
#	You can always set this to "/", it'll just take a long time, but
#	it will find ALL PK's on your system.
PKFONTS	= /usr/lib/font/devjet /usr5/texfonts/fonts
#	Where you want to build fonts, set this to the name of the
#	directory where you want to make them.  Caution: this could be
#	enormous...  (5000+ blocks)
NEWFONTS = /tmp/newfonts

#	Dinna touch from here on

.SUFFIXES: .S .S~

.S~:
	@rm -f $@
	$(GET) -p $(GFLAGS) $< | ./sedscript > $@
	@chmod 444 $@

.S~.S:
	@rm -f $@
	$(GET) -p $(GFLAGS) $< > $@
	@chmod 444 $@

.S:
	rm -f $@
	./sedscript < $@.S > $@
	@chmod 444 $@

#	Main stuff:

MAIN	= troff2.o opt.o utils.o t2conf.o cattab.o debug.o

#	Drivers:

BACKENDS = lj.o pk.o pkc.o ps.o dt.o

#	Creation of sedscript for parameterization.

SEDSCRIPT =	"sed -e 's;%%LIBDIR%%;$(LIBDIR);g' \
		     -e 's;%%LJF%%;$(LJF);g' \
		     -e 's;%%STARTSHELL%%;'\"$(STARTSHELL)\"';g' \
		     -e 's;%%COMPILE%%;$(COMPILE);g' \
		     -e 's;%%FONTDIR%%;$(FONTDIR);g' \
		     -e 's;%%MANDIR%%;$(MANDIR);g' \
		     -e 's;%%CCFLAGS%%;$(CCFLAGS);' \
		     -e 's;%%CC%%;$(CC);' \
		     -e 's;%%LIBFLAGS%%;$(LIBFLAGS);' \
		     -e 's;%%SFP2PK%%;$(SFP2PK);' \
		     -e 's;%%PKFONTS%%;$(PKFONTS);' \
		     -e 's;%%NEWFONTS%%;$(NEWFONTS);' \
		     -e 's^%%IGNORESH%%^$(IGNORESH)^' \
		     -e 's;%%RTMACDIR%%;$(RTMACDIR);g'"

DEFINES	= '-DLIBDIR="$(LIBDIR)/lib"' '-DFONTDIR="$(FONTDIR)"'\
	'-DLJF="$(LJF)"' $(CCFLAGS)
CFLAGS	= $(DEFINES)


all:	troff2ps subst.done makeincl psroff README TROUBLE LASERFONTS submakes

subst.done:	sedscript
	rm -f psroff makeincl
	touch subst.done

submakes:
	cd adapters ; $(MAKE) all
	cd lib ; $(MAKE) all
	cd man ; $(MAKE) all
	cd utils ; $(MAKE) all
	cd widths ; $(MAKE) all
	cd tests ; $(MAKE) all

psroff:	psroff.S sedscript
	@echo "Extracting $@ with variable substitutions"
	@rm -f $(@) ; ./sedscript < $@.S > T ; chmod 555 T ; mv T $@

makeincl:	makeincl.S sedscript
	@echo "Extracting $@ with variable substitutions"
	@rm -f $(@) ; ./sedscript < $@.S > T ; chmod 555 T ; mv T $@

audit:	audit.S sedscript
	@echo "Extracting $@ with variable substitutions"
	@rm -f $(@) ; ./sedscript < $@.S > T ; chmod 555 T ; mv T $@

unpackljfonts:
	@ $(IGNORESH) find . -name '*.UU' -print | \
	while read i ; \
	do \
	    ft=`echo $$i | sed -e 's/.UU//'` ; \
	    if [ -s $$ft ] ; \
	    then \
		echo "Removing $$i" ; \
		rm -f $$i ; \
	    else \
		rm -f $$ft ; \
		echo "uudecoding $$i -> $$ft" ; \
		uudecode < $$i ; \
		if [ ! -s $$ft ] ; \
		then \
		    echo "Whoops - uudecode failed on $$i - bad path?" ; \
		    exit 1 ; \
		else \
		    rm $$i ; \
		fi ; \
	    fi ; \
	done

buildljfonts:	unpackljfonts
	make troff2ps makeincl
	cd lib ; make lj.fonts lj.lib
	cd utils ; make buildfonts

installljfonts:
	test -d $(LIBDIR) || mkdir $(LIBDIR)
	test -d $(LIBDIR)/lib || mkdir $(LIBDIR)/lib
	test -d $(LIBDIR)/lib/lj || mkdir $(LIBDIR)/lib/lj
	$(IGNORESH) files=`echo fonts.lj/*.pk` ; \
	if [ "$$files" != 'fonts.lj/*.pk' ] ; \
	then \
	    cp fonts.lj/*.pk $(LJF) ; \
	fi
	$(IGNORESH) files=`echo fonts.lj/*.sfp` ; \
	if [ "$$files" != 'fonts.lj/*.sfp' ] ; \
	then \
	    cp fonts.lj/*.sfp $(LJF) ; \
	fi
	$(IGNORESH) test -z "$(PKFONTS)" -o ! -d "$(NEWFONTS)" \
	    || cp $(NEWFONTS)/* $(LJF)

register:
	make troff2ps makeincl
	cd lib ; make lj.fonts lj.lib
	cd widths ; make ljwidths widths
	@echo "Now su to root and type 'make installwidths'"

installwidths:	makeincl
	cd widths; $(MAKE) installwidths

install:	makeincl
	test -d $(BINDIR) || mkdir $(BINDIR)
	test -d $(MANDIR) || mkdir $(MANDIR)
	test -d $(LIBDIR) || mkdir $(LIBDIR)
	test -d $(LIBDIR)/adapters || mkdir $(LIBDIR)/adapters
	test -d $(LIBDIR)/lib || mkdir $(LIBDIR)/lib
	test -d $(RTMACDIR) || mkdir $(RTMACDIR)
	test -d $(FONTDIR) || mkdir $(FONTDIR)
	rm -f $(BINDIR)/psroff.old
	-cp $(BINDIR)/psroff $(BINDIR)/psroff.old
	cp psroff $(BINDIR)/psroff
	rm -f $(LIBDIR)/troff2ps.old
	-cp $(LIBDIR)/troff2ps $(LIBDIR)/troff2ps.old
	cp troff2ps $(LIBDIR)/troff2ps
	cd lib ; $(MAKE) install
	cd adapters ; $(MAKE) install
	cd man ; $(MAKE) install
	cd utils ; $(MAKE) install
	cd widths; $(MAKE) install

troff2ps:	$(MAIN) $(BACKENDS)
	$(CC) $(CFLAGS) -o troff2ps $(MAIN) $(BACKENDS) $(LIBFLAGS)

#	The following is commented out because make has a bug in that
#	even if you don't want to make lint, it insists on making
#	the .c's from the SCCS files.  Dumb stupid....
#lint:	$(MAIN:.o=.c) $(BACKENDS:.o=.c)
#	lint -p $(CFLAGS) $(MAIN:.o=.c) $(BACKENDS:.o=.c) > output.lint

#	Drivers include dependencies - add to this list if you're adding
#	drivers.
t2conf.o:	defs.h ps.h lj.h dt.h
ps.o:	defs.h ps.h
dt.o:	defs.h dt.h
lj.o:	defs.h lj.h pkc.h pk.h

pk.o:	defs.h pk.h pkc.h
pkc.o:	defs.h pk.h pkc.h
cattab.o:	defs.h pk.h
debug.o:	defs.h pk.h

troff2.o utils.o opt.o: defs.h

sedscript:	Makefile
	rm -f sedscript ; echo $(SEDSCRIPT) > T ; chmod 755 T ; mv T sedscript

clean:	makeincl
	rm -f core *.o troff2ps psroff diagnostics audit
	rm -fr mon.out output.lint TEST*
	rm -fr FONTS
	cd adapters ; $(MAKE) clean
	cd man ; $(MAKE) clean
	cd utils ; $(MAKE) clean
	cd lib ; $(MAKE) clean
	cd widths ; $(MAKE) clean
	cd tests ; $(MAKE) clean
	rm -f sedscript makeincl subst.done */subst.done

#	Yes, this is a little wierd, but in this way I can figure
#	out what you are...
TEST:	Makefile defs.h tests/testtab.m makeincl
	cd lib ; make
	rm -f TEST1 TEST2
	PATH=.:$(LIBDIR):$$PATH \
		troff2ps -V | sed -e 's/^.*- \(.*\)/.ds 2v "\1/' > TEST1
	echo ".ds 2d \"`date`" >> TEST1
	echo ".sp" > TEST2
	echo ".ps 10" >> TEST2
	echo ".vs 12p" >> TEST2
	echo ".ad 1" >> TEST2
	echo "DEFS.H DEFINITIONS:" >> TEST2
	sed -n -e '/Edit no more/q' \
		-e '/T2VERSION/d' \
		-e '/LIBDIR/d' \
		-e 's/[ 	]*\/\*.*//' \
		-e '/^.define[ 	]*\([^ 	]*\)[ 	]*\(.*\)$$/s//define \1=\2;/' \
		-e 's/=;/;/' \
		-e 's/^define //p' \
		-e '/^.undef[ 	]*\([^ 	]*\).*/s//!\1;/p' \
		defs.h >> TEST2
	#echo ".br" >> TEST2
	echo "MAKEFILE DEFINITIONS:" >> TEST2
	echo "LIBDIR = $(LIBDIR);" >> TEST2
	echo "LJF = $(LJF);" >> TEST2
	echo "STARTSHELL = $(STARTSHELL); " >> TEST2
	echo "SHELL = $(SHELL); " >> TEST2
	echo "COMPILE = $(COMPILE);" >> TEST2
	echo "FONTDIR = $(FONTDIR);" >> TEST2
	echo "MANDIR = $(MANDIR);" >> TEST2
	echo "CCFLAGS = $(CCFLAGS);" >> TEST2
	echo "CC = $(CC);" >> TEST2
	echo "LIBFLAGS = $(LIBFLAGS);" >> TEST2
	echo "RTMACDIR = $(RTMACDIR);" >> TEST2
	echo "PKFONTS = $(PKFONTS);" >> TEST2
	echo "NEWFONTS = $(NEWFONTS);" >> TEST2
	echo "IGNORESH = $(IGNORESH);" >> TEST2
	echo "SFP2PK = $(SFP2PK)." >> TEST2
	echo "TTYPE = $(TTYPE)." >> TEST2
	@echo "Don't worry if the uname/grep fails"
	-grep '^trofftype' lib/psrofflib >> TEST2
	echo "Uname results:" >> TEST2
	-uname -a >> TEST2
	cat TEST1 tests/testtab.m TEST2 > TEST
	rm -f TEST1 TEST2

test:	TEST
	tbl TEST | PATH=.:$(LIBDIR):$$PATH psroff -T$(TTYPE)

check:	audit sedscript makeincl
	cd lib ; $(MAKE) psrofflib
	./audit

checkinstall:	audit sedscript makeincl
	cd lib ; $(MAKE) psrofflib
	./audit install

#	This is how I build a release - don't even try running this...!
buildrelease:	clean unpackljfonts
	$(IGNORESH) t=`find . -name 'p.*' -print`; \
	if [ -n "$$t" ] ; \
	then \
	    echo "files still out: $$t" ; \
	    exit 1 ; \
	fi
	make buildfinal

buildfinal:
	make troff2ps makeincl
	cd lib ; make lj.fonts lj.lib
	cd utils ; make tmaps buildSfonts
	cd utils ; make keepbuiltfonts
	make test
	make clean
	t/cleantree
	t/mkmanifest
	t/mkkits
	t/cleantree

#	Impossible file, we hope....
WF	= /cowsnovskibumsteinpastafazoola/bdkdkdk
testbug:
	@ $(IGNORESH) if [ -r $(WF) ] ; \
	then \
	    echo "Bizarre - are you sure you have $(WF)?" ; \
	else \
	    echo "IGNORESH set correctly" ; \
	fi
