#=======================================================================
# Makefile for bibjoin, a BibTeX bibliography file field ordering
# utility.
#
# Current target list:
#	all			make documentation files
#	bibjoin.hlp		VAX VMS HELP file of manual pages
#	bibjoin.shar		UNIX shar bundle for e-mail distribution
#	bibjoin.ps		PostScript file of typeset manual pages
#	bibjoin.tar		UNIX tar archive bundle
#	bibjoin.txt		ASCII text file of manual pages
#	bibjoin.zip		Info-ZIP archive bundle
#	bibjoin.zoo		Zoo archive bundle
#	check			run validation suite (not yet implemented)
#	clean			remove temporary files
#	clobber			remove all automatically recreatable files
#	distclean		same as clobber
#	install			install bibjoin on system
#	install-ftp		install bibjoin on anonymous ftp directory
#	maintainer-clean	do NOT use: this is for the developer only!
#	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
# [14-Dec-1996]
#=======================================================================

# 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
MANDIR		= /usr/local/man/man1
MANEXT		= 1
FTPDIR		= /u/ftp/pub/tex/bib
HTMLMANDIR	= /usr/local/share/html/man/man1

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

CHMOD		= chmod

COL		= col -b

COMPRESS	= compress
COMPRESS	= gzip

# We prefer to retain file modification times on installed files, but
# not all cp implementations have the -p (preserve timestamp) switch.
# Fortunately, all rcp implementations seem to support -p, so we use
# rcp instead.  Its directory location varies, so we assume that it
# can be found in the search path.
CP		= /bin/cp
CP		= rcp -p

DIST-FILES      = CHANGELOG README README.AWK Makefile bibjoin.hlp \
		  bibjoin.html bibjoin.man bibjoin.ps bibjoin.pdf \
		  bibjoin.sh bibjoin.sok bibjoin.txt configure man2ps \
		  rofvms.awk

DISTILL		= distill

ISPELL		= ispell

LN		= /bin/ln

LS		= /bin/ls

# man2html is available at ftp://ftp.math.utah.edu/pub/sgml/man2html-x.yy.*
MAN2HTML	= man2html

MAN2PS		= ./man2ps

# Not all mkdir commands support -p, but the install-html target
# will simply skip its installation work if the HTML directory
# is not available, perhaps because -p was unrecognized.
MKDIR		= mkdir -p

NROFF		= nroff

RM		= /bin/rm -f

SED		= /bin/sed

SHELL		= /bin/sh

SORT		= sort

SPELL		= spell

TAR		= tar

UNZIP		= unzip

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

ZIP		= zip

ZOO		= zoo

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

all:	bibjoin.txt bibjoin.hlp

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

bibjoin.html:	bibjoin.man
	$(MAN2HTML) bibjoin.man

bibjoin.pdf:	bibjoin.ps
	$(DISTILL) bibjoin.ps

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

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

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

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

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

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

check:	all
	@echo bibjoin does not yet have a validation suite

clean mostlyclean:
	-$(RM) *.o
	-$(RM) *.shar
	-$(RM) *.tar
	-$(RM) *.tar-lst
	-$(RM) *.zip
	-$(RM) *.zip-lst
	-$(RM) *.zoo
	-$(RM) *.zoo-lst
	-$(RM) *~
	-$(RM) \#*
	-$(RM) core

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

install:	install-exe install-html install-man

install-exe:
	-$(RM) $(BINDIR)/bibjoin
	$(CP) bibjoin.sh $(BINDIR)/bibjoin
	$(CHMOD) 755 $(BINDIR)/bibjoin
	$(LN) $(BINDIR)/bibjoin $(BINDIR)/bibjoin-$(VERSION)
	$(LS) -l $(BINDIR)/bibjoin $(BINDIR)/bibjoin-$(VERSION)

install-ftp:	bibjoin.tar bibjoin.zip bibjoin.zoo
	$(TAR) tvf bibjoin-$(VERSION).tar >$(FTPDIR)/bibjoin-$(VERSION).tar-lst
	$(COMPRESS) <bibjoin-$(VERSION).tar \
		>$(FTPDIR)/bibjoin-$(VERSION).tar.gz
	$(CP) bibjoin-$(VERSION).zip $(FTPDIR)
	$(CP) bibjoin-$(VERSION).zip-lst $(FTPDIR)
	$(CP) bibjoin-$(VERSION).zoo $(FTPDIR)
	$(CP) bibjoin-$(VERSION).zoo-lst $(FTPDIR)
	$(LS) -l $(FTPDIR)/bibjoin-$(VERSION).*

install-html:	bibjoin.html
	-if [ -d $(HTMLMANDIR) ] ; \
	then \
		true ; \
	else \
		$(MKDIR) $(HTMLMANDIR) 2>/dev/null ; \
	fi
	-if [ -d $(HTMLMANDIR) ] ; \
	then \
		$(RM) $(HTMLMANDIR)/bibjoin.html ; \
		$(CP) bibjoin.html $(HTMLMANDIR)/bibjoin.html ; \
		$(CHMOD) 664 $(HTMLMANDIR)/bibjoin.html ; \
		$(LS) -l $(HTMLMANDIR)/bibjoin.html ; \
	else \
		echo "Skipping HTML file installation: $(HTMLMANDIR) does not exist" ; \
	fi

install-man:
	$(CP) bibjoin.man $(MANDIR)/bibjoin.$(MANEXT)
	-$(RM) $(CATDIR)/bibjoin.$(MANEXT)
	$(CHMOD) 664 $(MANDIR)/bibjoin.$(MANEXT)
	$(LS) -l $(MANDIR)/bibjoin.$(MANEXT)

maintainer-clean:	distclean
	@echo "This command is intended for maintainers to use;"
	@echo "it deletes files that may require special tools to rebuild."
	-$(RM) bibjoin.pdf bibjoin.html

# Check the spelling in the bibjoin script and manual pages, leaving
# exceptions in bibjoin.ser.  Both spell and ispell are used.
SPELL-FILTER = cat bibjoin.man bibjoin.sh | $(SED) -e "s/[^a-zA-Z'-]/ /g"
spell bibjoin.ser:
	-if [ ! -f bibjoin.sok ] ; then touch bibjoin.sok ; fi
	-$(SPELL-FILTER) | $(SPELL) +bibjoin.sok >/tmp/bibjoin.ser
	-$(SPELL-FILTER) | $(ISPELL) -l -p bibjoin.sok >>/tmp/bibjoin.ser
	-$(SORT) -u /tmp/bibjoin.ser >bibjoin.ser
#	-if [ ! -s bibjoin.ser ] ; then $(RM) bibjoin.ser ; fi
	-$(RM) /tmp/bibjoin.ser

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

uninstall:	uninstall-exe uninstall-html uninstall-man

uninstall-exe:
	-$(RM) $(BINDIR)/bibjoin $(BINDIR)/bibjoin-$(VERSION)

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

uninstall-html:
	-$(RM) $(HTMLMANDIR)/bibjoin.html

uninstall-man:
	-$(RM) $(MANDIR)/bibjoin.$(MANEXT)
