# ======================================================================
# Makefile for biblex (lexically analyze BibTeX file), bibparse (parse
# BibTeX file according to prototype grammar), and bibunlex
# (reconstruct BibTeX file from lexical token stream).  biblex and
# bibunlex are lex/flex-based, and bibparse is yacc/bison-based.
#
# Current target list:
#	all			(default) make biblex and test
#	arch-dir		make minimal subdirectory for
#				architecture-specific build
#	biblex			lex-based BibTeX lexer
#	bibparse		yacc-based BibTeX parser
#	bibtools.shar		make shar bundle for e-mailing
#	bibunlex		filter to convert biblex token stream to
#				BibTeX format
#	clean			clean up trash and object files
#	clobber			clean up everything for a rebuild
#	distclean		same as clobber
#	doc			make PostScript and text files from man pages
#	dw			doubled-word check on manual pages
#	install			install bibdup, biblex, bibparse, and
#				bibunlex in system directories
#	mostlyclean		same as clean
#	programs		compile and link executable programs
#	ps-files		make PostScript files from man pages
#	realclean		remove everything that can be rebuilt
#				by make
#	reallyclean		same as realclean (but better grammar)
#	spell			spell check manual pages
#	TAGS			GNU Emacs tags file for fast editing
#	test			test biblex
#	txt-files		make ASCII text files from man pages
#	uninstall		remove all files installed in system
#				directories by "make install"
#
# [16-Jan-1993]
# ======================================================================

# Change these to suit local conventions

BINDIR	= /usr/local/bin
MANDIR	= /usr/local/man
MANEXT	= 1
FTPDIR	= /u/ftp/pub/tex/bib

# Use lcc or gcc with extra flags for extensive error checking, or use
# C++ compiler (g++ or CC).  DO NOT CHANGE ANY OF THESE VALUES:
# instead, copy one of these to the end of the CC list to select your
# compiler.  You may also set the DEFINES value below too, though that
# is rarely necessary.

# NB: On SGI IRIX 4.0.x, if you do not have C++, then you must use
# 'CC = cc -cckr' to compile biblex.c and bibunlex.c, because the
# lex-generated code contains a line
#	FILE *yyin = {stdin}, *yyout = {stdout};
# which will not compile when the c89 version of stdio.h is used.

CC	= c89			## DEC Alpha OSF/1 Standard C compiler
CC	= cxx			## DEC Alpha OSF/1 C++ compiler
CC	= CC			## C++ for BSD 4.3 UNIX on HP 370
CC	= CC +a1 -xansi +w	## C++ for SGI IRIX 4.0 (see LOADLIBES below)
CC	= cc -ansiposix -fullwarn -woff 22,24,183,205,262,269,302,303
                                ## C for SGI IRIX 4.0
CC	= CC +a1 +p +w -D_POSIX_SOURCE -Hansi -D__STDC__ -D_BSD -D_AIX -Dps2 \
	  -DHAVE_TERMIO_H
                                ## C++ on AIX 370 and PS/2: +a1 (ANSI
                                ## declarations), +p (no anachronisms), +w
                                ## (warnings) -D-BSD (to get toupper() and
                                ## tolower() declared in ctype.h).
                                ## Need -DHAVE_TERMIO_H because termios.h
                                ## has wrong prototypes for C++
CC	= CC +a1 +p +w -D_POSIX_SOURCE -Hansi -D__STDC__ -D_BSD -D_AIX -D_AIX370
                                ## C++ on AIX 370
CC	= cc -Hansi -D_POSIX_SOURCE	## cc on AIX 370 and AIX PS/2
CC	= /usr/CC/sun4/CC -I/usr/CC/incl -D__SUNCC__ ## C++ on SunOS 4.1.1
CC	= acc -Xc -vc -strconst -D__ACC__ -D_POSIX_SOURCE -DHAVE_TERMIOS_H
                                ## SunOS Standard C compiler (neither
                                ## -DHAVE_SGTTY_H nor -DHAVE_TERMIO_H gives
                                ## char-at-a-time input, sigh...; they work
                                ## fine on other systems).
CC	= c89 -D_POSIX_SOURCE -D_ALL_SOURCE	## IBM RS/6000
CC	= xlC			## IBM RS/6000 C++
CC	= c89 -D_HPUX_SOURCE	## HP 9000/8xx HP-UX A.08
CC	= cc			## most systems
CC	= cc			## Stardent (NB: remove -g in OPT below)
CC	= cc -systype bsd43 -D__CC__	## MIPS RCxxxx RISCos 4.52 and 5.2:
CC	= cc $(GCCFLAGS)	## NeXT (cc is really GNU C compiler)
CC	= g++ $(GCCFLAGS)	## GNU C++
CC	= lcc -A -A -n		## Princeton/AT&T Standard C compiler
CC	= gcc $(GCCFLAGS)	## GNU C

# Compiler for this site:
CC	= g++ $(GCCFLAGS)	## GNU C++
CC	= gcc $(GCCFLAGS)	## GNU C
CC	= cc -Xc -D__ACC__ -D__solaris ## Sun Solaris 2.x
# ======================================================================
# The remainder of this Makefile should not need changes

ARCH		= arch

# These files are needed in architecture-specific subdirectories
ARCH-FILES	= $(ARCH-COPY-FILES) $(ARCH-LINK-FILES)

# These files must be copied into architecture-specific subdirectories
# since they need customization
ARCH-COPY-FILES	= Makefile

# These files do not require architecture-specific customizations, so
# symbolic links are created for them.
ARCH-LINK-FILES	= bibdup.awk bibdup.man bibdup.sh bibfiles.dct \
		  biblex.dat biblex.l biblex.man biblex.ok bibparse.man \
		  bibparse.y bibunlex.l bibunlex.man bibyydcl.h lexmain.c

# Need new awk (nawk) or gawk here:
AWK		= nawk

CHMOD           = chmod

# When HOST, USER, __DATE__, and __TIME__ are available,
# biblex preserves them for its version output
#CFLAGS         = $(OPT) $(DEFINES) -DHOST=\"$(HOST)\" -DUSER=\"$(USER)\"
CFLAGS          = $(OPT) $(DEFINES)

# Extra flags for gcc and g++ to get maximal checking
GCCFLAGS        = -Wall -Wshadow -Wcast-qual -Wpointer-arith -Wwrite-strings
GCCFLAGS        = -Wshadow -Wcast-qual -Wpointer-arith -Wwrite-strings

COMPRESS	= compress

CP              = /bin/cp

DEROFF		= deroff

DIFF            = diff

DITROFF         = ditroff		## AT&T
DITROFF         = groff			## GNU

ETAGS           = etags

# SGI IRIX 4.0 lacks expand, so use cat instead
EXPAND		= cat
EXPAND		= expand

# On HP-UX 9.0, also need -lPW for alloca() (required by bibparse.c)
LDFLAGS         = -ll -lPW
LDFLAGS         = -ll

# For flex, need to manually remove the %a and %o table size lines
LEX             = flex
LEX             = lex

LN		= ln -s

# Define this symbol for additional link libraries
LOADLIBES	= -lC -lc_s		## SGI IRIX 4.0 for shared libraries
LOADLIBES	= 

MAN-FILES       = bibdup.man biblex.man bibparse.man bibunlex.man

MV              = /bin/mv

NROFF		= nroff

OPT             = -g

PROGRAMS	= biblex bibparse bibunlex

PS-FILES        = $(MAN-FILES:.man=.ps)

RM              = /bin/rm -f

SED		= /bin/sed

SHAR            = shar

SHAR-FILES	= Makefile README bibdup.awk bibdup.sh bibdup.man \
		  bibfiles.dct biblex.c biblex.dat biblex.l biblex.man \
		  biblex.ok biblex.ps biblex.txt bibparse.man bibparse.c \
		  bibparse.ps bibparse.txt bibparse.y bibunlex.c \
		  bibunlex.l bibunlex.man bibunlex.ps bibunlex.txt \
		  bibyydcl.h lexmain.c

SHELL		= /bin/sh

SPELL		= spell +bibfiles.dct

TAGS-FILES      = biblex.l bibparse.y bibunlex.l

TXT-FILES       = $(MAN-FILES:.man=.txt)

UNZIP		= unzip

VERSION		= `$(AWK) '/^[ \t]*version *= *"[0-9.]+", *$$/ \
		{ gsub(/[^0-9.]/,"",$$3); print $$3 }' bibparse.y`

YACC            = yacc
YACC            = bison -y

YFLAGS          = -v

ZIP		= zip

ZOO		= zoo

#=======================================================================

.SUFFIXES:	.ps .txt .man .i .c .y

.c.i:
	$(CC) -E $(CFLAGS) $< >$@

.y.i:
	$(MAKE) $*.c	
	$(MAKE) $*.i
	$(MR) $*.c

.man.ps:
	$(DITROFF) -Tps -man $? >$@

.man.txt:
	$(DITROFF) -Tascii -man $? >$@

#=======================================================================

all:	programs doc test

# Make a subdirectory named by the arch program, and install symbolic
# links or copies of needed files to allow building this software
# under a different architecture.

arch-dir:	$(ARCH-FILES)
	-mkdir `arch`
	$(CP) $(ARCH-COPY-FILES) `arch`/
	-for f in $(ARCH-LINK-FILES) ; \
	do \
		cd `arch` ; ln -s ../$$f . ; cd .. ; \
	done
	@echo "Now edit the file(s) $(ARCH-COPY-FILES) in the `arch` \
subdirectory"
	@echo "to customize them for this architecture."
	@echo "You probably need to change CC and maybe YACC."

biblex:	biblex.o lexmain.o
	$(CC) $(CFLAGS) -o biblex lexmain.o biblex.o $(LDFLAGS)

biblex.o:	biblex.c bibyydcl.h

bibparse.c:	bibparse.y
	$(YACC) $(YFLAGS) bibparse.y
	$(MV) y.tab.c bibparse.c

bibparse.o:	bibparse.c bibyydcl.h

bibparse:	bibparse.o biblex.o
	$(CC) $(CFLAGS) -o bibparse bibparse.o biblex.o -ly $(LDFLAGS)

bibparse.shr:	$(SHAR-FILES)
	shar -b -c -v $(SHAR-FILES) >bibparse.shr

bibparse.tar:	$(SHAR-FILES)
	-$(RM) bibparse.tar bibparse.tar-lst
	tar chf bibparse.tar $(SHAR-FILES)
	-mkdir bibparse-$(VERSION)
	cd bibparse-$(VERSION); tar xf ../bibparse.tar
	tar cf bibparse-$(VERSION).tar bibparse-$(VERSION)
	-$(RM) -r bibparse-$(VERSION)
	-$(RM) bibparse.tar
	$(LN) bibparse-$(VERSION).tar bibparse.tar

bibparse.txt:	bibparse.man
	$(NROFF) -man bibparse.man | col -b | $(EXPAND) >$@

bibparse.zip:	$(SHAR-FILES)
	-$(RM) bibparse*.zip
	-$(RM) bibparse*.zip-lst
	$(ZIP) bibparse-$(VERSION).zip $(SHAR-FILES)
	$(UNZIP) -v bibparse-$(VERSION).zip >bibparse-$(VERSION).zip-lst
	$(LN) bibparse-$(VERSION).zip bibparse.zip

bibparse.zoo:	$(SHAR-FILES)
	-$(RM) bibparse*.zoo
	-$(RM) bibparse*.zoo-lst
	$(ZOO) a bibparse-$(VERSION).zoo $(SHAR-FILES)
	$(ZOO) v bibparse-$(VERSION).zoo >bibparse-$(VERSION).zoo-lst
	$(LN) bibparse-$(VERSION).zoo bibparse.zoo

bibtools.shar:	$(SHAR-FILES)
	$(SHAR) $(SHAR-FILES) >bibtools.shar

bibunlex.o:	bibunlex.c bibyydcl.h

bibunlex:	bibunlex.o
	$(CC) $(CFLAGS) -o bibunlex bibunlex.o $(LDFLAGS)

clean mostlyclean:
	-$(RM) *.i
	-$(RM) *.o
	-$(RM) *~
	-$(RM) \#*
	-$(RM) biblex.tmp
	-$(RM) bibparse.output
	-$(RM) core
	-$(RM) y.output

clobber distclean:	clean
	-$(RM) $(PROGRAMS)
	-$(RM) $(PS-FILES)
	-$(RM) $(TXT-FILES)
	-$(RM) bibparse.tab.c
	-$(RM) bibtools.shar
	-$(RM) y.tab.c

doc:	ps-files txt-files

dw:
	@for f in $(MAN-FILES) ; \
	do \
		echo "==================== doubled words in $$f\
====================" ; \
		$(DEROFF) $$f | dw ; \
	done

install:	$(PROGRAMS) bibdup.sh
	for program in $(PROGRAMS) ; \
	do \
		$(CP) $$program $(BINDIR)/$$program ; \
		$(CHMOD) 775 $(BINDIR)/$$program ; \
		$(CP) $$program.man $(MANDIR)/man$(MANEXT)/$$program.1 ; \
		$(RM) $(MANDIR)/man$(MANEXT)/../cat$(MANEXT)/$$program.1 ; \
		$(CHMOD) 664 $(MANDIR)/man$(MANEXT)/$$program.1 ; \
	done
	$(SED) -e 's=bibdup.awk=$(BINDIR)/bibdup.awk=' \
		<bibdup.sh >$(BINDIR)/bibdup
	$(CP) bibdup.awk $(BINDIR)/bibdup.awk
	$(CP) bibdup.man $(MANDIR)/man$(MANEXT)/bibdup.1
	-$(RM) $(MANDIR)/man$(MANEXT)/../cat$(MANEXT)/bibdup.1
	$(CHMOD) 775 $(BINDIR)/bibdup
	$(CHMOD) 664 $(BINDIR)/bibdup.awk
	$(CHMOD) 664 $(MANDIR)/man$(MANEXT)/bibdup.1

install-ftp:	install-ftp.time

install-ftp.time:	bibparse.tar bibparse.zip bibparse.zoo
	tar tvf bibparse-$(VERSION).tar >$(FTPDIR)/bibparse-$(VERSION).tar-lst
	$(COMPRESS) <bibparse-$(VERSION).tar \
		>$(FTPDIR)/bibparse-$(VERSION).trz
	$(CP) bibparse-$(VERSION).zip $(FTPDIR)
	$(CP) bibparse-$(VERSION).zip-lst $(FTPDIR)
	$(CP) bibparse-$(VERSION).zoo $(FTPDIR)
	$(CP) bibparse-$(VERSION).zoo-lst $(FTPDIR)
	ls -l $(FTPDIR)/bibparse*
	date >install-ftp.time

programs:	$(PROGRAMS)

ps-files:	$(PS-FILES)

realclean reallyclean:	distclean
	-$(RM) biblex.c
	-$(RM) bibparse.c
	-$(RM) bibunlex.c
	-$(RM) TAGS

spell:
	@for f in $(MAN-FILES) ; \
	do \
		echo "==================== spelling exceptions in $$f\
====================" ; \
		$(DEROFF) $$f | $(SPELL) ; \
	done

TAGS:	$(TAGS-FILES)
	$(ETAGS) $(TAGS-FILES)

test:	biblex
	@ echo "The following test should produce no output"
	./biblex <biblex.dat >biblex.tmp
	$(DIFF) biblex.ok biblex.tmp
	@-$(RM) biblex.tmp

test-version:	Makefile
	@echo "Version number is ""'"$(VERSION)"'"

txt-files:	$(TXT-FILES)

uninstall:
	-for program in $(PROGRAMS) ; \
	do \
		$(RM) $(BINDIR)/$$program ; \
		$(RM) $(MANDIR)/man$(MANEXT)/$$program.1 ; \
		$(RM) $(MANDIR)/man$(MANEXT)/../cat$(MANEXT)/$$program.1 ; \
	done
	-$(RM) $(BINDIR)/bibdup.awk $(BINDIR)/bibdup
	-$(RM) $(MANDIR)/man$(MANEXT)/bibdup.1
	-$(RM) $(MANDIR)/man$(MANEXT)/../cat$(MANEXT)/bibdup.1
