#=======================================================================
# 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
#
# [11-Nov-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
HTMLMANDIR	= /home/httpd/htdocs/computing/man
HTMLMANDIR	= /usr/local/share/html/man/man1


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

CHMOD		= chmod

COL		= col -b

COMPRESS	= compress

# 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 bibsort.hlp \
		  bibsort.html bibsort.man bibsort.pdf bibsort.ps \
		  bibsort.sh bibsort.sok bibsort.txt configure man2ps \
		  rofvms.awk

DISTILL		= distill

ISPELL		= ispell

LN		= /bin/ln

LS		= /bin/ls

MAN2HTML	= $(HOME)/public_html/support/myman2html/man2html.sh

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

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.html:	bibsort.man
	$(MAN2HTML) bibsort.man

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

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

check:	all
	@echo bibsort 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) bibsort.hlp
	-$(RM) bibsort.txt
	-$(RM) bibsort.ps
	-$(RM) bibsort.ser

install:	install-exe install-html install-man

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

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

install-man:
	$(CP) bibsort.man $(MANDIR)/bibsort.$(MANEXT)
	-$(RM) $(CATDIR)/bibsort.$(MANEXT)
	$(CHMOD) 664 $(MANDIR)/bibsort.$(MANEXT)
	$(LS) -l $(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-$(VERSION).*

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

# 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:	uninstall-exe uninstall-html uninstall-man

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

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

uninstall-man:
	-$(RM) $(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
