# Look over config.X before building.
#
# You will probably want to edit where it says "EDIT HERE" below;
# the Makefile will update all other files to match.
#
# If you want to build non-English hash files, search for the string
# "foreignhashes" and add command lines there.
#
# the argument syntax for buildhash to make alternate dictionary files
# is simply:
#
#   buildhash <dictfile> <affix-file> <outfile>

# EDIT HERE
#
CC = cc
CC = gcc -b i386-linux -s -jump
EMACS = emacs
LINT = lint
DEFINES = -DUSG -DSHORT_FILENAME
CFLAGS = -O2 -fomit-frame-pointer $(DEFINES)
LINTFLAGS = $(DEFINES)
#
# If you need any special libraries to get things to link, put them here.
#
LIBES =
#
# BINDIR - where to install user-executable binaries
#
# Don't forget CC above!
BINDIR = /usr/bin
#
# LIBDIR - where library files (notably the hash file) go.
#
LIBDIR = /usr/lib
#
# EMACSDIR - where emacs files go
#
EMACSDIR = /usr/lib/emacs
ELISPDIR = $(EMACSDIR)/lisp
TEXINFODIR = $(EMACSDIR)/info
#
# DEFHASH - default name for the hash file
# FOREIGNHASHES - list of other hash files
#
DEFHASH = english.hash
#FOREIGNHASHES = german.hash
FOREIGNHASHES =
#
# DEFLANG - default language file to use when building DEFHASH
# FOREIGNLANGS - list of other language files to use when building
# FOREIGNHASHES.
#
DEFLANG	= english.aff
#FOREIGNLANGS = german.aff
FOREIGNLANGS =
#
# DEFDICT - default dictionary to use to build DEFHASH.  Choices, in
# approximate order of increasing size: english.sml, english.sml+,
# english.med, english.med+, english.lrg, english.lrg+.  Note that larger
# dictionaries increase ispell's startup time.
#
# Note that making the "+" dictionaries requires LOTS of temp file space
# (in either /usr/tmp or wherever your environment variable TMPDIR points).
# The biggest, "english.lrg+", takes up to 30000 blocks of temp file space
# to create.  (By contrast, the non-"+" versions take temp space of about
# twice the size of the completed dictionary, except for "sml", which needs
# no temp space at all).
#
# Also, you should be aware that the two smaller dictionaries are much more
# accurate than the "lrg" and "lrg+" dictionaries.  The smaller dictionaries
# have been checked for errors and are believed to be error-free, but the
# ones based on "english.lrg" are known to contain spelling errors.  Because
# of the nature of ispell, these errors are not fatal flaws, but you should
# still be aware that they exist and be suspicious of the "lrg*" dictionaries.
#
# FOREIGNDICTS - list of foreign dictionaries.
#
DEFDICT = english.med
#FOREIGNDICTS = german.med
FOREIGNDICTS =
GERMANDICTS = german.0
#
# DICTVARIANTS - variants of the English dictionary that are desired.
# Choose from "british", "american", or "altamer".  (The latter is an
# extension to the American dictionary, with alternate spellings, and
# should be used together with the American list).  It is possible to
# include all three variants, or to add your own, if you wish.
# British users should be a bit suspicious of the British lists,
# especially the larger ones, as the final versions were compiled by
# an American.
#
DICTVARIANTS = american altamer
#
# EXTRADICT - system dictionary (dictionaries) to be included with
# the distributed dictionaries to make the "+" versions above.  Usually
# /usr/dict/words, /usr/dict/web2, or both.  (Warning:  /usr/dict/web2 is
# extremely big.  You don't want it in most cases.)
#
#EXTRADICT = /usr/dict/words
#
# MAN1DIR - where chapter-1 man pages go
#
MAN1DIR	= /usr/man/man1
#
# MAN4DIR - where chapter-4 man pages go
#
MAN4DIR	= /usr/man/man4
#
# MAN1EXT - what extension to put on chapter-1 man pages
#
MAN1EXT	= .1
#
# MAN4EXT - what extension to put on chapter-4 man pages
#
MAN4EXT	= .4
#
# TERMLIB - where to get the termcap library.  Should be -ltermcap or -lcurses
# on most systems.
#
# TERMLIB = -lcurses
TERMLIB = -ltermcap
#
# REGLIB - where to get the regular-expression routines, if SYSTEM_LOOKUP is
# not defined.  Should be -lPW on USG systems, null (I think) on BSD systems.
#
REGLIB =
#
# If your sort command accepts the -T switch to set temp file locations
# (try it out;  it exists but is undocumented on some systems), make the
# following variable null.  Otherwise make it the sed script.
#
SORTTMP = -e '/!!SORTTMP!!/s/=.*$$/=/'
#SORTTMP =
#
# If your sort command accepts the -T switch (see above), make the following
# variable refer to a temporary directory with lots of space.  Otherwise
# make it null.
#
#MAKE_SORTTMP = -T $${TMPDIR-/usr/tmp}
#
#	END OF EDITING
#
SHELL = /bin/sh

all: programs ispell.info
all: $(DEFHASH)
all: foreignhashes

programs: buildhash findaffix tryaffix fixdict ispell icombine munchlist
programs: subset sq unsq zapdups

$(DEFHASH): buildhash $(DEFDICT) $(DEFLANG)
	./buildhash $(DEFDICT) $(DEFLANG) $(DEFHASH)

#
# The following target provides an easy way to rebuild your default
# dictionary when you wish to do so.  This dependency isn't all that
# smart, but it tries to handle the possibility that you might have
# run "make dictclean" or "make kitclean".
#
defaultdict:
	if [ -r english.0 ]; then \
	    $(MAKE) DBUILD= $(DEFDICT); \
	else \
	    $(MAKE) BUILD= $(DEFDICT); \
	fi

install: all
	cd $(BINDIR); rm -f ispell buildhash munchlist findaffix tryaffix
	cp ispell buildhash munchlist findaffix tryaffix sq unsq $(BINDIR)
	cd $(LIBDIR); rm -f $(DEFHASH) $(DEFLANG) $(FOREIGNHASHES) \
	  $(FOREIGNLANGS) icombine
	cp $(DEFHASH) $(DEFLANG) $(FOREIGNHASHES) $(FOREIGNLANGS) icombine \
	  $(LIBDIR)
	chmod 755 $(LIBDIR)/icombine $(BINDIR)/ispell $(BINDIR)/munchlist \
	  $(BINDIR)/findaffix $(BINDIR)/tryaffix $(BINDIR)/buildhash \
	  $(BINDIR)/sq $(BINDIR)/unsq
	cd $(LIBDIR); chmod 644 $(DEFHASH) $(DEFLANG) \
	  $(FOREIGNHASHES) $(FOREIGNLANGS)
	rm -f $(MAN1DIR)/ispell$(MAN1EXT) $(MAN4DIR)/ispell$(MAN4EXT) \
	  $(MAN4DIR)/english$(MAN4EXT)
	cp ispell.1 $(MAN1DIR)/ispell$(MAN1EXT)
	cp sq.1 $(MAN1DIR)/sq$(MAN1EXT)
	cp ispell.4 $(MAN4DIR)/ispell$(MAN4EXT)
	cp english.4 $(MAN4DIR)/english$(MAN4EXT)
	if [ -d $(ELISPDIR) ]; then \
	    rm -f $(TEXINFODIR)/ispell $(ELISPDIR)/ispell.el; \
	    cp ispell.info $(TEXINFODIR)/ispell; \
	    chmod 644 $(TEXINFODIR)/ispell; \
	    cp ispell.el $(ELISPDIR); \
	    echo '(byte-compile-file "$(ELISPDIR)/ispell.el")' \
	      > /tmp/emi$$$$; \
	    $(EMACS) -batch $(ELISPDIR)/ispell.el -l /tmp/emi$$$$; \
	    rm -f /tmp/emi$$$$; \
	    chmod 644 $(ELISPDIR)/ispell.el $(ELISPDIR)/ispell.elc; \
	fi

ispell.info:	ispell.texinfo
	if [ -d $(ELISPDIR) ]; then \
	    $(EMACS) -batch dummy -i ispell.texinfo \
	      -f texinfo-format-buffer -f save-buffer; \
	    rm -f ispell.info~; \
	fi

fix-dictionary:	all
	PATH=:$$PATH sh fixdict $(DEFDICT)

munchable:	findaffix tryaffix munchlist buildhash ispell icombine sq unsq

munch-dictionary:	munchable
	PATH=:$$PATH munchlist -l $(DEFLANG) $(DEFDICT)

#
#	Dependencies to build extra hash files
#
allhashes:	normhashes plushashes foreignhashes
normhashes:	sml.hash med.hash lrg.hash
plushashes:	sml+.hash med+.hash lrg+.hash

#
#	Add dependencies here to build non-English hash files.
#
foreignhashes: $(FOREIGNHASHES)

german.hash:	buildhash german.aff german.med
	rm -f german.hash
	if [ "$(DEFDICT)" = german.med ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash german.med german.aff $@; \
	fi

$(DBUILD)german.med:	munchable $(GERMANDICTS)
	PATH=:$$PATH munchlist -l german.aff $(GERMANDICTS) > german.med

#
# Note that we don't use $(MAKE) in the following dependencies.  There
# is a good reason for this -- if we did, "make -n" would still run buildhash.
#
sml.hash:	buildhash english.aff english.sml
	rm -f sml.hash
	if [ "$(DEFDICT)" = english.sml ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.sml english.aff $@; \
	fi

sml+.hash:	buildhash english.aff english.sml+
	rm -f sml+.hash
	if [ "$(DEFDICT)" = english.sml+ ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.sml+ english.aff $@; \
	fi

med.hash:	buildhash english.aff english.med
	rm -f med.hash
	if [ "$(DEFDICT)" = english.med ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.med english.aff $@; \
	fi

med+.hash:	buildhash english.aff english.med+
	rm -f med+.hash
	if [ "$(DEFDICT)" = english.med+ ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.med+ english.aff $@; \
	fi

lrg.hash:	buildhash english.aff english.lrg
	rm -f lrg.hash
	if [ "$(DEFDICT)" = english.lrg ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.lrg english.aff $@; \
	fi

lrg+.hash:	buildhash english.aff english.lrg+
	rm -f lrg+.hash
	if [ "$(DEFDICT)" = english.lrg+ ]; then \
	    make $(DEFHASH); ln $(DEFHASH) $@; \
	else \
	    ./buildhash english.lrg+ english.aff $@; \
	fi

#
#	The six dictionaries, english.sml through english.lrg+, are built
#	by the following dependencies.  The macros "CBUILD" and
#	"DBUILD" are a sneaky trick used to keep make from trying to
#	run munchlist every time a minor change is made to one of the
#	C programs.  If you define these macros as null, "make all"
#	will automatically build the dictionaries.  If you leave them
#	with their default definitions, "make all" will leave the
#	dictionaries alone, and you will have to type something like
#	"make buildenglish.med+" to build the dictionary using
#	munchlist.
#
#	Why two macros?  So you can delete {eng,amer,altamer,brit}.[012].??
#	to save space, and still type "make DBUILD= all".
#
#	The following dependencies are convenient ways to make some or
#	all of the dictionaries.
#
BUILD	=	build
CBUILD	=	$(BUILD)
DBUILD	=	$(BUILD)

alldicts:	normdicts plusdicts
normdicts:	$(DBUILD)english.sml $(DBUILD)english.med $(DBUILD)english.lrg
plusdicts:	$(DBUILD)english.sml+ $(DBUILD)english.med+
plusdicts:	$(DBUILD)english.lrg+

dictcomponents:	$(CBUILD)english.0 $(CBUILD)english.1 $(CBUILD)english.2
dictcomponents: $(CBUILD)american.0 $(CBUILD)american.1 $(CBUILD)american.2
dictcomponents: $(CBUILD)altamer.0 $(CBUILD)altamer.1 $(CBUILD)altamer.2
dictcomponents: $(CBUILD)british.0 $(CBUILD)british.1 $(CBUILD)british.2

$(CBUILD)english.0:	unsq eng.0.??
	cat eng.0.?? | ./unsq > english.0

$(CBUILD)english.1:	unsq eng.1.??
	cat eng.1.?? | ./unsq > english.1

$(CBUILD)english.2:	unsq eng.2.??
	cat eng.2.?? | ./unsq > english.2

$(CBUILD)american.0:	unsq amer.0.??
	cat amer.0.?? | ./unsq > american.0

$(CBUILD)american.1:	unsq amer.1.??
	cat amer.1.?? | ./unsq > american.1

$(CBUILD)american.2:	unsq amer.2.??
	cat amer.2.?? | ./unsq > american.2

$(CBUILD)altamer.0:	unsq altamer.0.??
	cat altamer.0.?? | ./unsq > altamer.0

$(CBUILD)altamer.1:	unsq altamer.1.??
	cat altamer.1.?? | ./unsq > altamer.1

$(CBUILD)altamer.2:	unsq altamer.2.??
	cat altamer.2.?? | ./unsq > altamer.2

$(CBUILD)british.0:	unsq brit.0.??
	cat brit.0.?? | ./unsq > british.0

$(CBUILD)british.1:	unsq brit.1.??
	cat brit.1.?? | ./unsq > british.1

$(CBUILD)british.2:	unsq brit.2.??
	cat brit.2.?? | ./unsq > british.2

$(DBUILD)english.sml:	english.0 american.0 altamer.0 british.0
	dicts="english.0"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0"; \
	    done; \
	    set -x; \
	    sort -u -t/ +0f -1 +0 $(MAKE_SORTTMP) -o english.sml $$dicts

$(DBUILD)english.sml+:	munchable $(EXTRADICT)
$(DBUILD)english.sml+:	english.0 american.0 altamer.0 british.0
	dicts="english.0 $(EXTRADICT)"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0"; \
	    done; \
	    set -x; \
	    PATH=:$$PATH munchlist -l $(DEFLANG) $$dicts > english.sml+

$(DBUILD)english.med:	english.0 american.0 altamer.0 british.0
$(DBUILD)english.med:	english.1 american.1 altamer.1 british.1
	dicts="english.0 english.1"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0 $$i.1"; \
	    done; \
	    set -x; \
	    sort -u -t/ +0f -1 +0 $(MAKE_SORTTMP) -o english.med $$dicts

$(DBUILD)english.med+:	munchable $(EXTRADICT)
$(DBUILD)english.med+:	english.0 american.0 altamer.0 british.0
$(DBUILD)english.med+:	english.1 american.1 altamer.1 british.1
	dicts="english.0 english.1 $(EXTRADICT)"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0 $$i.1"; \
	    done; \
	    set -x; \
	    PATH=:$$PATH munchlist -l $(DEFLANG) $$dicts > english.med+

$(DBUILD)english.lrg:	english.0 american.0 altamer.0 british.0
$(DBUILD)english.lrg:	english.1 american.1 altamer.1 british.1
$(DBUILD)english.lrg:	english.2 american.2 altamer.2 british.2
	dicts="english.0 english.1 english.2"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0 $$i.1 $$i.2"; \
	    done; \
	    set -x; \
	    sort -u -t/ +0f -1 +0 $(MAKE_SORTTMP) -o english.lrg $$dicts

$(DBUILD)english.lrg+:	munchable $(EXTRADICT)
$(DBUILD)english.lrg+:	english.0 american.0 altamer.0 british.0
$(DBUILD)english.lrg+:	english.1 american.1 altamer.1 british.1
$(DBUILD)english.lrg+:	english.2 american.2 altamer.2 british.2
	dicts="english.0 english.1 english.2 $(EXTRADICT)"; \
	    for i in $(DICTVARIANTS); do \
		dicts="$$dicts $$i.0 $$i.1 $$i.2"; \
	    done; \
	    set -x; \
	    PATH=:$$PATH munchlist -l $(DEFLANG) $$dicts > english.lrg+

buildhash: buildhash.o hash.o makedent.o parse.o
	$(CC) $(CFLAGS) -o buildhash buildhash.o hash.o makedent.o parse.o \
	  $(LIBES)

icombine: icombine.o makedent.o parse.o
	$(CC) $(CFLAGS) -o icombine icombine.o makedent.o parse.o \
	  $(LIBES)

EDITFILE	=	notthere
OUTFILE		=	/dev/null

doedit:
	sed -e 's@!!LIBDIR!!@$(LIBDIR)@' -e 's@!!DEFDICT!!@$(DEFDICT)@' \
	    -e 's@!!DEFHASH!!@$(DEFHASH)@' -e 's@!!DEFLANG!!@$(DEFLANG)@' \
	    $(SORTTMP) < $(EDITFILE) > $(OUTFILE)

findaffix:	findaffix.X Makefile
	@$(MAKE) EDITFILE=findaffix.X OUTFILE=findaffix doedit
	chmod +x findaffix

fixdict:	fixdict.X Makefile
	@$(MAKE) EDITFILE=fixdict.X OUTFILE=fixdict doedit
	chmod +x fixdict

munchlist:	munchlist.X Makefile
	@$(MAKE) EDITFILE=munchlist.X OUTFILE=munchlist doedit
	chmod +x munchlist

subset:	subset.X Makefile
	@$(MAKE) EDITFILE=subset.X OUTFILE=subset doedit
	chmod +x subset

tryaffix:	tryaffix.X Makefile
	@$(MAKE) EDITFILE=tryaffix.X OUTFILE=tryaffix doedit
	chmod +x tryaffix

zapdups:	zapdups.X Makefile
	@$(MAKE) EDITFILE=zapdups.X OUTFILE=zapdups doedit
	chmod +x zapdups

OBJS	=	ispell.o correct.o defmt.o dump.o good.o lookup.o hash.o \
		makedent.o term.o tgood.o tree.o xgets.o

ispell: $(OBJS)
	$(CC) $(CFLAGS) -o ispell $(OBJS) $(TERMLIB) $(REGLIB) $(LIBES)

$(OBJS) buildhash.o icombine.o hash.o parse.o: config.h ispell.h local.h msgs.h
ispell.o: version.h

config.h:	config.X Makefile
	@$(MAKE) EDITFILE=config.X OUTFILE=config.h doedit

#	Create a sample local.h if no such file currently exists
local.h:
	[ -r local.h ]  ||  cp local.h.samp local.h

parse.c: parse.y
	bison parse.y
	mv parse.tab.c parse.c

tags:	config.h *.[chy]
	ctags -w -t *.[chy]
	sed -e s/config.h/config.X/ tags > ntags
	mv ntags tags

TAGS:	config.h *.[chy]
	etags -t *.[chy]
	sed -e s/config.h/config.X/ TAGS > NTAGS
	mv NTAGS TAGS

lint:	config.h ispell.h msgs.h *.[cy]
	@$(LINT) $(LINTFLAGS) ispell.c correct.c defmt.c dump.c good.c \
	    hash.c lookup.c makedent.c tgood.c term.c tree.c xgets.c
	@bison parse.y
	@$(LINT) $(LINTFLAGS) buildhash.c hash.c makedent.c y.tab.c
	@$(LINT) $(LINTFLAGS) icombine.c makedent.c y.tab.c
	@rm -f y.tab.c

clean:
	rm -f *.o core a.out mon.out hash.out \
		*.stat *.cnt config.h $(DEFHASH) $(FOREIGNHASHES)
	rm -f buildhash findaffix tryaffix fixdict ispell icombine \
		munchlist subset sq unsq zapdups

#
#	The following target allows you to clean out the leftover raw
#	files gotten from unpacking the kit.  It makes sure that you
#	have the combined files first, so it may take a little while
#	to run.
#
kitclean:
	$(MAKE) dictcomponents
	rm -f eng.[012].?? amer.[012].?? altamer.[012].?? brit.[012].??

#
#	The following target allows you to clean out the combined
#	dictionary files.  For safety, so you don't lose your files,
#	it makes sure that there is something to work from, but it can
#	only be so smart, so be careful!
#
dictclean:
	rm -f english.sml english.sml+ english.med english.med+
	rm -f english.lrg english.lrg+
	ok=yes; \
	    for i in eng amer altamer brit; do \
		for j in 0 1 2; do \
		    [ -r $$i.$$j.aa ]  ||  ok=no; \
		done; \
	    done; \
	    if [ $$ok = yes ]; then \
		rm -f english.[012] american.[012]; \
		rm -f altamer.[012] british.[012]; \
	    fi
