#=======================================================================
# 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.shar		UNIX shar bundle for e-mail distribution
#	bibcheck.ps		PostScript file of typeset manual pages
#	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		same as clobber
#	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
#
# [30-Sep-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

CHMOD		= chmod

COL		= col -b

COMPRESS	= compress

CP		= /bin/cp

DIST-FILES	= CHANGELOG README Makefile bibcheck.man bibcheck.hlp \
		  bibcheck.ps bibcheck.sh bibcheck.sok bibcheck.txt man2ps \
		  rofvms.awk

ISPELL		= ispell

LN		= ln -s

MAN2PS		= ./man2ps

MKDIR		= mkdir

NROFF		= nroff

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`

ZIP		= zip

ZOO		= zoo

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

all:	bibcheck.txt bibcheck.hlp

# 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

clean mostlyclean:
	-$(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

clobber distclean realclean:	clean
	-$(RM) bibcheck.hlp
	-$(RM) bibcheck.txt
	-$(RM) bibcheck.ps
	-$(RM) bibcheck.ser

install:
	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
