#=======================================================================
# Makefile for bibsort, a BibTeX bibliography file sort utility.
#
# Current target list:
#	all			make documentation files
#	bibsort.hlp		VAX VMS HELP file of manual pages
#	bibsort.shar		UNIX shar bundle for e-mail distribution
#	bibsort.ps		PostScript file of typeset manual pages
#	bibsort.tar		UNIX tar archive bundle
#	bibsort.txt		ASCII text file of manual pages
#	bibsort.zip		Info-ZIP archive bundle
#	bibsort.zoo		Zoo archive bundle
#	clean			remove temporary files
#	clobber			remove all automatically recreatable files
#	distclean		same as clobber
#	install			install bibsort on system
#	install-ftp		install bibsort 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
#
# [16-Sep-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		= /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 bibsort.man bibsort.hlp \
		  bibsort.ps bibsort.sh bibsort.sok bibsort.txt man2ps \
		  rofvms.awk

ISPELL		= ispell

LN		= ln -s

MAN2PS		= ./man2ps

NROFF		= nroff

RM		= /bin/rm -f

SED		= /bin/sed

SHELL		= /bin/sh

SORT		= sort

SPELL		= spell

UNZIP		= unzip

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

ZIP		= zip

ZOO		= zoo

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

all:	bibsort.txt bibsort.hlp

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

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

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

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

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

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

bibsort.zoo:	$(DIST-FILES)
	-$(RM) bibsort*.zoo
	-$(RM) bibsort*.zoo-lst
	$(ZOO) a bibsort-$(VERSION).zoo $(DIST-FILES)
	$(ZOO) v bibsort-$(VERSION).zoo >bibsort-$(VERSION).zoo-lst
	$(LN) bibsort-$(VERSION).zoo bibsort.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) core

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

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

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

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

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

uninstall:
	-$(RM) $(BINDIR)/bibsort $(MANDIR)/bibsort.$(MANEXT)

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