#=======================================================================
# Makefile for bibcheck, a BibTeX bibliography file heuristic check
# utility.
#
# Current target list:
#	all			make documentation files
#	bibcheck.hlp		VAX VMS HELP file of manual pages
#	bibcheck.ps		PostScript file of typeset manual pages
#	bibcheck.ser		spelling error file
#	bibcheck.shar		UNIX shar bundle for e-mail distribution
#	bibcheck.tar		UNIX tar archive bundle
#	bibcheck.txt		ASCII text file of manual pages
#	bibcheck.zip		Info-ZIP archive bundle
#	bibcheck.zoo		Zoo archive bundle
#	clean			remove temporary files
#	clobber			remove all automatically recreatable files
#	distclean		remove all automatically recreatable
#				files, including biblex.c
#	install			install bibcheck on system
#	install-ftp		install bibcheck on anonymous ftp directory
#	mostlyclean		same as clean
#	realclean		same as clobber
#	spell			check spelling (with two spellers)
#	test-version		test extraction of version number
#	uninstall		remove installed executable and man pages
#	uninstall-ftp		remove installed files in anonymous ftp
#				directory
#
# In addition, these machine-specific targets are available for
# building bibcheck:
#	generic
#	cdc-mips-epix-cc
#	cray-el94
#	cray-el94-c++
#	dec-alpha-osf1-c++
#	dec-alpha-osf1-cc
#	dec-alpha-osf1-g++
#	dec-alpha-osf1-gcc
#	dec-mips-ultrix-cc
#	dec-mips-ultrix-g++
#	dec-mips-ultrix-gcc
#	dec-mips-ultrix-lcc
#	hp-370-bsd43-c++
#	hp-370-bsd43-cc
#	hp-9000-hpux-c++
#	hp-9000-hpux-cc
#	ibm-370-aix-c++
#	ibm-370-aix-cc
#	ibm-ps2-aix-c++
#	ibm-ps2-aix-cc
#	ibm-rs6000-aix-cc
#	ibm-rs6000-aix-c++
#	ibm-rs6000-aix-g++
#	ibm-rs6000-aix-gcc
#	mips-mips-riscos-cc
#	next-motorola-mach-cc
#	next-motorola-mach-g++
#	next-motorola-mach-gcc
#	next-motorola-mach-lcc
#	sgi-mips-irix-c++
#	sgi-mips-irix-cc
#	sgi-mips-irix-g++
#	sgi-mips-irix-gcc
#	sgi-mips-irix-lcc
#	stardent-mips-os2-cc
#	sun-sparc-solaris2-apcc
#	sun-sparc-sunos5-apcc
#	sun-sparc-solaris2-g++
#	sun-sparc-sunos5-g++
#	sun-sparc-solaris2-gcc
#	sun-sparc-sunos5-gcc
#	sun-sparc-solaris2-c++
#	sun-sparc-sunos5-c++
#	sun-sparc-solaris2-cc
#	sun-sparc-sunos5-cc
#	sun-sparc-solaris2-lcc
#	sun-sparc-sunos5-lcc
#	sun-sparc-solaris1-acc
#	sun-sparc-sunos4-acc
#	sun-sparc-solaris1-c++
#	sun-sparc-sunos4-c++
#	sun-sparc-solaris1-gcc
#	sun-sparc-sunos4-gcc
#	sun-sparc-solaris1-g++
#	sun-sparc-sunos4-g++
# [17-Dec-1994]
#=======================================================================

# Change these two directories to match local conventions if you
# wish to do "make install" and "make install-ftp":
BINDIR		= /usr/local/bin
CATDIR		= /usr/local/man/cat1
LIBDIR		= /usr/local/lib
MANDIR		= /usr/local/man/man1
MANEXT		= 1
FTPDIR		= /usr/spool/ftp/pub/tex/bib
FTPDIR		= /u/ftp/pub/tex/bib

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

CC		= CC
CC		= cc

CFLAGS		= $(OPT) -D__solaris -D__EXTERN_C__
CFLAGS		= $(OPT)

CHMOD		= chmod

COL		= col -b

COMPRESS	= compress

CP		= /bin/cp

DIST-FILES      = CHANGELOG Makefile README bibcheck.awk \
		  bibcheck.c bibcheck.man bibcheck.hlp bibcheck.ps \
		  bibcheck.sh bibcheck.sok bibcheck.txt biblex.l \
		  bibtest1.bib bibyydcl.h hash.c hash.h man2ps os.h \
		  regexp/Makefile regexp/README regexp/args.h \
		  regexp/regerror.c regexp/regexp.3 regexp/regexp.c \
		  regexp/regexp.h regexp/regmagic.h regexp/regsub.c \
		  regexp/tests regexp/timer.c regexp/try.c \
		  regexp/try.errs rofvms.awk strdup.c stricm.c \
		  strnic.c typedefs.h unixlib.h xalloc.c xalloc.h \
		  xctype.h xerrno.h xstat.h xstddef.h xstdlib.h \
		  xstring.h xtypes.h

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

ISPELL		= ispell

LDFLAGS		=

LIBS		= -ll

LN		= ln -s

MAN2PS		= ./man2ps

MKDIR		= mkdir

NROFF		= nroff

OPT		= -g

RM		= /bin/rm -f

RMDIR		= rmdir

SED		= /bin/sed

SHELL		= /bin/sh

SORT		= sort

SPELL		= spell

UNZIP		= unzip

# NB: The version number of the FTP bibcheck archive files is extracted
# from that of bibcheck.awk.
# VERSION	= `$(AWK) '/^\#\#\#[ \t]*version *= *"[0-9.]+", *$$/ \
#		{ gsub(/[^0-9.]/,"",$$4); print $$4 }' bibcheck.awk`
# The version number comes from bibcheck.c, starting with version 0.08.
VERSION		= `$(AWK) '/^[ \t]*version *= *"[0-9.]+", *$$/ \
		{ gsub(/[^0-9.]/,"",$$3); print $$3 }' bibcheck.c`

ZIP		= zip

ZOO		= zoo

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

.SUFFIXES:	.i .c

# Preprocess only and eliminate lines with only white space.
.c.i:
	$(CC) $(CFLAGS) -E $< | grep -v '^[ 	]*$$' >$*.i

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

all:	bibcheck.txt bibcheck.hlp

BIBCHECK_OBJS = bibcheck.o biblex.o hash.o strdup.o stricm.o strnic.o \
		xalloc.o regexp/regexp.o regexp/regerror.o

bibcheck:	$(BIBCHECK_OBJS)
	$(CC) $(CFLAGS) -o $@ $(BIBCHECK_OBJS) $(LDFLAGS) $(LIBS)

regexp/regexp.o:	 regexp/regexp.c regexp/regexp.h regexp/regmagic.h
	(cd regexp ; $(MAKE) CC='$(CC)' CFLAGS='$(CFLAGS)' regexp.o)

regexp/regerror.o:	 regexp/regerror.c
	(cd regexp ; $(MAKE) CC='$(CC)' CFLAGS='$(CFLAGS)' regerror.o)

# VAX VMS help file format from bibcheck.txt
bibcheck.hlp:	bibcheck.txt rofvms.awk
	$(AWK) -f rofvms.awk <bibcheck.txt >bibcheck.hlp

bibcheck.ps:	bibcheck.man
	$(MAN2PS) <bibcheck.man >bibcheck.ps

bibcheck.shar:	$(DIST-FILES)
	shar -b -c -v $(DIST-FILES) >bibcheck.shar

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

bibcheck.txt:	bibcheck.man
	$(NROFF) -man $? | $(COL) >$@

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

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

biblex.o:	biblex.c bibyydcl.h
	$(CC) $(CFLAGS) -c $<

clean mostlyclean:
	-$(RM) *.o
	-$(RM) *.o
	-$(RM) *.shar
	-$(RM) *.tar
	-$(RM) *.tar-lst
	-$(RM) *.zip
	-$(RM) *.zip-lst
	-$(RM) *.zoo
	-$(RM) *.zoo-lst
	-$(RM) *~
	-$(RM) \#*
	-$(RM) a.out
	-$(RM) core
	(cd regexp; $(MAKE) clean)

clobber realclean:	clean
	-$(RM) bibcheck
	-$(RM) bibcheck.hlp
	-$(RM) bibcheck.txt
	-$(RM) bibcheck.ps
	-$(RM) bibcheck.ser
	(cd regexp; $(MAKE) clobber)

# The C code from lex is generally system-dependent, sigh...
distclean:	clobber
	-$(RM) biblex.c

install:	bibcheck
	$(CP) bibcheck $(BINDIR)/bibcheck
	$(CHMOD) 755 $(BINDIR)/bibcheck
	$(CP) bibcheck.man $(MANDIR)/bibcheck.$(MANEXT)
	-$(RM) $(CATDIR)/bibcheck.$(MANEXT)
	$(CHMOD) 644 $(MANDIR)/bibcheck.$(MANEXT)

# This step is obsolete, but is retained in case the C version
# cannot be installed.
install-obsolete-awk-version:
	if [ ! -d $(LIBDIR)/bibcheck ] ; then $(MKDIR) $(LIBDIR)/bibcheck; fi
	$(CP) bibcheck.awk $(LIBDIR)/bibcheck/bibcheck.awk
	$(SED) -e "s@LIBDIR@$(LIBDIR)@" \
		-e "s@AWK@$(AWK)@" \
		-e "s@PWD@`pwd`@" \
		-e "s@DATE@`date`@" \
		-e "s@USER@$$LOGNAME@" \
		-e "s@HOSTNAME@`hostname`@" \
		bibcheck.sh > $(BINDIR)/bibcheck
	$(CHMOD) 755 $(BINDIR)/bibcheck
	$(CP) bibcheck.man $(MANDIR)/bibcheck.$(MANEXT)
	-$(RM) $(CATDIR)/bibcheck.$(MANEXT)
	$(CHMOD) 644 $(MANDIR)/bibcheck.$(MANEXT)

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

# Check the spelling in the bibcheck script and manual pages, leaving
# exceptions in bibcheck.ser.  Both spell and ispell are used.
SPELL-FILTER = cat bibcheck.awk bibcheck.man bibcheck.sh
spell:	bibcheck.ser

bibcheck.ser:
	-if [ ! -f bibcheck.sok ] ; then touch bibcheck.sok ; fi
	-$(SPELL-FILTER) | $(SPELL) +bibcheck.sok >/tmp/bibcheck.ser
	-$(SPELL-FILTER) | $(ISPELL) -l -p bibcheck.sok >>/tmp/bibcheck.ser
	-$(SORT) -u /tmp/bibcheck.ser >bibcheck.ser
	-if [ ! -s bibcheck.ser ] ; then $(RM) bibcheck.ser ; fi
	-$(RM) /tmp/bibcheck.ser

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

uninstall:
	-$(RM) $(BINDIR)/bibcheck
	-$(RM) $(LIBDIR)/bibcheck/bibcheck.awk
	-$(RM) $(CATDIR)/bibcheck.$(MANEXT)
	-$(RM) $(MANDIR)/bibcheck.$(MANEXT)
	-$(RMDIR) $(LIBDIR)/bibcheck

uninstall-ftp:
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).tar.z
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).tar-lst
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).zip
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).zip-lst
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).zoo
	-$(RM) $(FTPDIR)/bibcheck-$(VERSION).zoo-lst

HASH_H	      = hash.h

OS_H          = os.h            xerrno.h        xstddef.h

UNIXLIB_H     = unixlib.h       $(OS_H)         $(XSTAT_H)      $(XTYPES_H)

XALLOC_H      = xalloc.h        $(OS_H)

XCTYPE_H      = xctype.h

XSTAT_H       = xstat.h         $(OS_H)         $(XTYPES_H)

XSTRING_H     = xstring.h       $(OS_H)         $(XTYPES_H)

XTYPES_H      = xtypes.h        $(OS_H)

bibcheck.o:	bibcheck.c	hash.h		$(OS_H)		$(XSTDLIB_H) \
		$(XSTRING_H)	regexp/args.h	regexp/regexp.h	$(XCTYPE_H)

hash.o:		hash.c          $(OS_H)         $(XALLOC_H)     $(XSTRING_H) \
		hash.h          $(XCTYPE_H)

strdup.o:	strdup.c        $(OS_H)         $(UNIXLIB_H)    $(XSTDLIB_H) \
		$(XSTRING_H)

stricm.o:	stricm.c        $(OS_H)         $(XSTDLIB_H)    $(XSTRING_H) \
		$(XCTYPE_H)

xalloc.o:	xalloc.c        $(OS_H)         $(UNIXLIB_H)    $(XALLOC_H) \
		$(XSTDLIB_H)    typedefs.h

#=======================================================================
# Public convenience targets.   You can supply a TARGETS=target setting
# when you invoke make to select a particular target; if you do not,
# then the default target, all, will be made.

TARGETS = bibcheck

generic:
	$(MAKE) CC=cc $(TARGETS)

cdc-mips-epix-cc:
	$(MAKE) CC='cc -systype bsd43 -D__CC__' $(TARGETS)

# c89 on Cray erroneously tries to load lib-l.a instead of libl.a when
# -ll is specified, so override it.
cray-el94:
	$(MAKE) CC='c89' $(TARGETS) LIBS=/usr/lib/libl.a

cray-el94-c++:
	$(MAKE) CC='CC' $(TARGETS) LIBS=/usr/lib/libl.a
 
dec-alpha-osf1-c++:
	$(MAKE) CC='cxx -D_POSIX_SOURCE' $(TARGETS)

# NB: At OSF/1 version 1.x, can omit -std1, but need it for OSF/1 version 2.x
dec-alpha-osf1-cc:
	$(MAKE) CC='c89 -std1' $(TARGETS)

dec-alpha-osf1-g++:
	$(MAKE) CC='g++ $(GCCFLAGS)' $(TARGETS)

dec-alpha-osf1-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' $(TARGETS)

dec-mips-ultrix-cc:
	$(MAKE) CC=cc $(TARGETS)

dec-mips-ultrix-g++:
	$(MAKE) CC='g++ $(GCCFLAGS)' $(TARGETS)

dec-mips-ultrix-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' $(TARGETS)

dec-mips-ultrix-lcc:
	$(MAKE) CC='lcc -A -A -n' $(TARGETS)

hp-370-bsd43-c++:
	$(MAKE) CC=CC $(TARGETS)

hp-370-bsd43-cc:
	$(MAKE) CC=CC $(TARGETS)

hp-9000-hpux-c++:
	$(MAKE) CC='CC -D_HPUX_SOURCE' $(TARGETS)

hp-9000-hpux-cc:
	$(MAKE) CC='c89 -D_HPUX_SOURCE' $(TARGETS)

IBM370CC = 'CC +a1 +p +w -D_POSIX_SOURCE -Hansi -D__STDC__ -D_BSD -D_AIX \
	-D_AIX370'
ibm-370-aix-c++:
	$(MAKE) CC=$(IBMPS2CC) $(TARGETS)

ibm-370-aix-cc:
	$(MAKE) CC=cc $(TARGETS)

IBMPS2CC = 'CC +a1 +p +w -D_POSIX_SOURCE -Hansi -D__STDC__ -D_BSD -D_AIX \
	-Dps2'
ibm-ps2-aix-c++:
	$(MAKE) CC=$(IBMPS2CC) $(TARGETS)

ibm-ps2-aix-cc:
	$(MAKE) CC=cc $(TARGETS)

ibm-rs6000-aix-cc:
	$(MAKE) CC='c89 -D_POSIX_SOURCE -D_ALL_SOURCE' $(TARGETS)

ibm-rs6000-aix-c++:
	$(MAKE) CC='xlC' $(TARGETS)

ibm-rs6000-aix-g++:
	$(MAKE) CC='g++ $(GCCFLAGS)' $(TARGETS)

ibm-rs6000-aix-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' $(TARGETS)

mips-mips-riscos-cc:
	$(MAKE) CC='cc -systype bsd43 -D__CC__' $(TARGETS)

next-motorola-mach-cc:
	$(MAKE) CC='cc $(GCCFLAGS)' EXTRA-OBJS= $(TARGETS)

next-motorola-mach-g++:
	$(MAKE) CC='g++ $(GCCFLAGS)' EXTRA-OBJS= $(TARGETS)

next-motorola-mach-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' EXTRA-OBJS= $(TARGETS)

next-motorola-mach-lcc:
	$(MAKE) CC='lcc -A -A -n' EXTRA-OBJS= $(TARGETS)

sgi-mips-irix-c++:
	$(MAKE) CC='CC +a1 -xansi +w' $(TARGETS)

SGICC	= 'cc -ansiposix -fullwarn -woff 22,24,183,205,262,269,302,303'
sgi-mips-irix-cc:
	$(MAKE) CC=$(SGICC) $(TARGETS)

sgi-mips-irix-g++:
	$(MAKE) CC='g++ $(GCCFLAGS)' $(TARGETS)

sgi-mips-irix-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' $(TARGETS)

sgi-mips-irix-lcc:
	$(MAKE) CC='lcc -A -A -n' $(TARGETS)

stardent-mips-os2-cc:
	$(MAKE) CC='cc' OPT= $(TARGETS)

SUNAPCC	= 'apcc -Xc=ansi -D__ACC__ -D__solaris'
sun-sparc-solaris2-apcc sun-sparc-sunos5-apcc:
	$(MAKE) CC=$(SUNAPCC) LINT='$(LINT) -D__ACC__ -D__solaris' $(TARGETS)

sun-sparc-solaris2-g++ sun-sparc-sunos5-g++:
	$(MAKE) CC='g++ $(GCCFLAGS) -D__solaris -D__EXTERN_C__' \
		LINT='$(LINT) -D__solaris' $(TARGETS)

sun-sparc-solaris2-gcc sun-sparc-sunos5-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS) -D__solaris' \
		LINT='$(LINT) -D__solaris' $(TARGETS)

sun-sparc-solaris2-c++ sun-sparc-sunos5-c++:
	$(MAKE) CC='CC -D__solaris -D__EXTERN_C__' \
		LINT='$(LINT) -D__solaris' $(TARGETS)

sun-sparc-solaris2-cc sun-sparc-sunos5-cc:
	$(MAKE) CC='cc -Xc -D__ACC__ -D__solaris' \
		LINT='$(LINT) -D__ACC__ -D__solaris' $(TARGETS)

# NB: We normally specify -n for checking for dereferencing of NULL
# pointers, but there is a bug in lcc 3.1 that results in bogus error
# messages.
sun-sparc-solaris2-lcc sun-sparc-sunos5-lcc:
	$(MAKE) CC='lcc -A -A -D__solaris' \
		LINT='$(LINT) -D__solaris' \
		CFLAGS='$(DEFINES) -DHOST=\"$(HOST)\" -DUSER=\"$(USER)\" $(XCFLAGS)' \
		$(TARGETS)

SUNACC	= 'acc -Xc -vc -strconst -D__ACC__ -D_POSIX_SOURCE'
sun-sparc-solaris1-acc sun-sparc-sunos4-acc:
	$(MAKE) CC=$(SUNACC) LINT='alint -D__ACC__ -D_POSIX_SOURCE' $(TARGETS)

sun-sparc-solaris1-c++ sun-sparc-sunos4-c++:
	$(MAKE) CC='/usr/CC/sun4/CC -I/usr/CC/incl -D__SUNCC__' \
		LINT='alint -D__ACC__ -D_POSIX_SOURCE' $(TARGETS)

sun-sparc-solaris1-gcc sun-sparc-sunos4-gcc:
	$(MAKE) CC='gcc $(GCCFLAGS)' \
		LINT='alint -D__ACC__ -D_POSIX_SOURCE' $(TARGETS)

# NB: We normally specify -n for checking for dereferencing of NULL
# pointers, but there is a bug in lcc 3.1 that results in bogus error
# messages.
sun-sparc-solaris1-lcc sun-sparc-sunos4-lcc:
	$(MAKE) CC='lcc -A -A' \
		LINT='alint -D__LCC__ -D_POSIX_SOURCE' $(TARGETS)

sun-sparc-solaris1-g++ sun-sparc-sunos4-g++:
	$(MAKE) CC='g++ $(GCCFLAGS) -I/usr/lang/SC1.0/include -D__EXTERN_C__' \
		LEX=lex++ \
		LINT='alint -D__ACC__ -D_POSIX_SOURCE' $(TARGETS)

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