# ======================================================================
# Makefile for checksum program
#
# Targets:
#	all		make everything
#	checksum	checksum program
#	checksum.tar	checksum program distribution in UNIX tar format
#	checksum.zip	checksum program distribution in Info-Zip format
#	checksum.zoo	checksum program distribution in zoo archive format
#	clean		remove object files and other temporary files
#	clobber		remove all reconstructable files
#	crc.tab		output of polynom
#	distclean	same as clobber
#	install		install checksum in system directory
#	mostlyclean	same as clean
#	polynom		generator of polynomial coefficients used by
#			checksum
#	realclean	same as clobber
#	test.new	simple test of checksum and its status codes
#	uninstall	remove checksum from system directory
#
# [15-Jul-1994]
# ======================================================================

# These two values may need to be changed for your local conventions
BINDIR          = /usr/local/bin
CATDIR          = /usr/local/man/cat1
MANDIR          = /usr/local/man/man1

# You should be able to use any C (old K&R, or new ANSI/ISO Standard
# C) or C++ compiler.  Please select the maximal warning message level
# provided by your compiler, and report all warnings back to the
# maintainer, <beebe@math.utah.edu>.
CC              = cc
CC              = CC
CC              = gcc $(GCCFLAGS)
CC		= g++ $(G++FLAGS)

CHECKSUM        = ./checksum

COMPRESS        = compress

# Use -p to preserve time stamps where available;
# HPUX and AT&T System V lack it however
CP              = /bin/cp -p

CTANGLE         = ctangle

CTFLAGS		=

CWEAVE          = cweave

CWFLAGS		=

DETEX           = detex

FTPDIR          = /usr/spool/ftp/pub/misc
FTPDIR          = /u/ftp/pub/misc

GCCFLAGS	= -Wall -Wshadow -Wcast-qual -Wpointer-arith -Wwrite-strings \
		  -Wnested-externs

# Options for g++ 2.0
G++FLAGS	= -Wall -Wshadow -Wcast-qual -Wpointer-arith -Wwrite-strings \
		-Wid-clash-6 -Wpointer-arith -Wcast-qual \
		-Wconversion -Waggregate-return -Wstrict-prototypes \
		-Wmissing-prototypes -Wno-parentheses

INSTALL         = $(CP)

LN              = /bin/ln

RM              = /bin/rm -f

SEP             = "============================================================"

SHELL           = /bin/sh

SPELL           = spell

STRIP           = strip

TAR             = tar

TARFILES        = Makefile README SMakefile checksum.c checksum.ch \
		  checksum.eok checksum.idx checksum.man checksum.scn \
		  checksum.tex checksum.toc checksum.txt checksum.w \
		  index polynom.c polynom.ch polynom.eok polynom.idx \
		  polynom.scn polynom.tex polynom.toc polynom.toc \
		  polynom.w test.new test.org alphavms ibmpcdos vaxvms

TEX             = tex

TMP             = tmp

UNZIP           = unzip

VERSION         = `nawk '/^%%% *version * = / \
		{ gsub(/[^0-9.]/,"",$$4); gsub(/\./,"-",$$4); print $$4 }' \
		checksum.w`

ZIP             = zip

ZOO             = zoo

#=======================================================================
# the remaining lines should not need changes

.SUFFIXES: .exc .tex .dvi .w

.tex.exc:
	$(DETEX) $< | $(SPELL) +$*.eok >$@
	@if [ -s $*.exc ] ; \
	then \
		echo "Spelling exceptions in $<:" ; \
		cat $*.exc ; \
	else \
		$(RM) $*.exc ; \
	fi

.tex.dvi:
	tex $<

.w.dvi:
	$(CWEAVE) $< $*.ch
	$(TEX) $<

.w.c:
	$(CTANGLE) $(CTFLAGS) $< $*.ch

.w.o:
	$(MAKE) "CC=$(CC)" "CFLAGS=$(CFLAGS)" $*.c
	$(MAKE) "CC=$(CC)" "CFLAGS=$(CFLAGS)" $*.o

.w.tex:
	$(CWEAVE) $(CWFLAGS) $< $*.ch

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

all:	checksum polynom crc.tab test.new checksum.dvi polynom.dvi

check:	test.new

checksum:	checksum.o
	$(CC) $(CFLAGS) -o checksum checksum.o

checksum.c checksum.tex:	checksum.w checksum.ch

checksum.txt:	checksum.man
	nroff -man checksum.man | col -b >$@

checksum.tar:	$(TMP)/LASTUPDATE
	cd $(TMP) ; $(TAR) cf ../$@ checksum
	-$(RM) checksum-$(VERSION).tar
	$(LN) $@ checksum-$(VERSION).tar

checksum.tar.z:	checksum.tar
	$(COMPRESS) <$? >$@
	-$(RM) checksum-$(VERSION).tar.z
	$(LN) $@ checksum-$(VERSION).tar.z

checksum.tar-lst:	checksum.tar
	$(TAR) tvf checksum.tar >$@
	-$(RM) checksum-$(VERSION).tar-lst
	$(LN) checksum.tar-lst checksum-$(VERSION).tar-lst

checksum.zip:	$(TMP)/LASTUPDATE
	-$(RM) $@
	cd $(TMP) ; $(ZIP) -r ../$@ checksum
	-$(RM) checksum-$(VERSION).zip
	$(LN) $@ checksum-$(VERSION).zip

checksum.zip-lst:	checksum.zip
	$(UNZIP) -v checksum.zip >$@
	-$(RM) checksum-$(VERSION).zip-lst
	$(LN) $@ checksum-$(VERSION).zip-lst

checksum.zoo:	$(TMP)/LASTUPDATE
	-$(RM) $@
	cd $(TMP) ; \
		$(ZOO) a ../$@ checksum checksum/* checksum/*/* checksum/*/*/*
	-$(RM) checksum-$(VERSION).zoo
	$(LN) $@ checksum-$(VERSION).zoo

checksum.zoo-lst:	checksum.zoo
	$(ZOO) v checksum.zoo >$@
	-$(RM) checksum-$(VERSION).zoo-lst
	$(LN) $@ checksum-$(VERSION).zoo-lst

clean mostlyclean:
	-$(RM) *.dvi
	-$(RM) *.log
	-$(RM) *.dvi-*
	-$(RM) *.exc
	-$(RM) *.o
	-$(RM) *~
	-$(RM) -r $(TMP)
	-$(RM) \#*
	-$(RM) checksum*.tar*
	-$(RM) checksum*.zip*
	-$(RM) checksum*.zoo*
	-$(RM) core

clobber distclean realclean:	clean
	-$(RM) checksum polynom crc.tab

crc.tab:	polynom
	./polynom

install:	checksum
	$(INSTALL) checksum $(BINDIR)/checksum
	$(STRIP) $(BINDIR)/checksum
	chmod 755 $(BINDIR)/checksum
	$(INSTALL) checksum.man $(MANDIR)/checksum.1
	chmod 744 $(MANDIR)/checksum.1
	-$(RM) $(CATDIR)/checksum.1

install-ftp:	checksum.tar-lst checksum.tar.z checksum.zip-lst checksum.zoo-lst
	$(CP) checksum-$(VERSION).tar.z $(FTPDIR)
	$(CP) checksum-$(VERSION).tar-lst $(FTPDIR)
	$(CP) checksum-$(VERSION).zip $(FTPDIR)
	$(CP) checksum-$(VERSION).zip-lst $(FTPDIR)
	$(CP) checksum-$(VERSION).zoo $(FTPDIR)
	$(CP) checksum-$(VERSION).zoo-lst $(FTPDIR)

polynom:	polynom.o
	$(CC) $(CFLAGS) -o polynom polynom.o

polynom.c polynom.tex:	polynom.w polynom.ch

test.new:	checksum
	$(CHECKSUM) test.org test.new
	@echo $(SEP)
	@echo "The following lines should show the old and new checksums:"
	- diff test.org test.new
	@echo $(SEP)
	@echo "The following test should return a NON-ZERO status code:"
	@if $(CHECKSUM) -v test.org  >/dev/null ;\
	then	echo "	checksum INCORRECTLY returns a zero status code" ;\
	else	echo "	checksum correctly returns a NON-ZERO status code" ;\
	fi
	@echo $(SEP)
	@echo "The following test should return a ZERO status code:"
	@if $(CHECKSUM) -v test.new >/dev/null ;\
	then	echo "	checksum correctly returns a ZERO status code" ;\
	else	echo "	checksum INCORRECTLY returns a non-zero status code" ;\
	fi
	@echo $(SEP)
	@echo "This test of the -c option should produce no output"
	@$(CHECKSUM) -c test.org >/tmp/checksum.new
	@$(CHECKSUM) <test.org | grep 'checksum *=' >/tmp/checksum.old
	@diff /tmp/checksum.old /tmp/checksum.new
	@echo $(SEP)

$(TMP)/LASTUPDATE:	$(TARFILES)
	-$(RM) -r $(TMP)
	-mkdir $(TMP) $(TMP)/checksum $(TMP)/checksum/v$(VERSION)
	$(TAR) cf - $(TARFILES) | (cd $(TMP)/checksum/v$(VERSION); $(TAR) xf -)
	date > $@

uninstall:
	-$(RM) $(BINDIR)/checksum
	-$(RM) $(MANDIR)/checksum.1
	-$(RM) $(CATDIR)/checksum.1
