
#   Development tree name for this version
PRODUCT = @PRODUCT@
VERSION = @VERSION@
TREE = $(PRODUCT)-$(VERSION)

#   Utilities used in building the program
SHELL = /bin/sh
CC = @CC@
CTANGLE = ctangle
CWEAVE = cweave
DVIPS = dvips
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
PDFTEX = pdftex
TEX = tex
XDVI = xdvi

#   Utility options
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@

#   Targets
PROGS = $(PRODUCT)
DOCUMENTS = $(PRODUCT).tex

#   Installation destinations

## The 'DESTDIR' variable allows you to install the program into a different
## location, e.g., for packaging, without affecting the compiled-in pathnames.
DESTDIR =

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
mandir = @mandir@
man1dir = $(mandir)/man1
man1ext = .1
BINDIR = $(DESTDIR)$(bindir)
MAN1DIR = $(DESTDIR)$(man1dir)

prog:	$(PROGS)

doc:	$(DOCUMENTS)

all:	$(PROGS) $(DOCUMENTS)

#   Build executable program

$(PRODUCT): $(PRODUCT).o getopt.o
	$(CC) $(PRODUCT).o getopt.o -o $(PRODUCT) $(CFLAGS) $(LDFLAGS)

#   Create C source from web

.w.c:
	$(CTANGLE) $*.w

#   Build TeX documentation file from web

.w.tex:
	$(CWEAVE) $*.w

#   Create TeX .dvi file of documentation

.tex.dvi:
	$(TEX) $*.tex

#   View TeX documentation

viewdoc: $(PRODUCT).dvi
	$(XDVI) $(PRODUCT)

#   Build PostScript file to print program

.dvi.ps:
	$(DVIPS) -f @*.dvi >$*.ps

#   Build PDF file of program and documentation
$(PRODUCT).pdf: $(PRODUCT).tex
	$(PDFTEX) $(PRODUCT).tex

#   Test programs and support for same

wretch: wretch.o
	$(CC) wretch.o -o wretch $(CFLAGS) $(LDFLAGS)

wretch.bin:  wretch
	./wretch

#   Build auto-configuration scripts

configurator:
	rm -f config.cache
	autoconf

#   Installation

install-bin: $(BINDIR) $(PRODUCT)
	$(INSTALL_PROGRAM) $(PRODUCT) $(BINDIR)/$(PRODUCT)

install-man: $(PRODUCT).1
	$(INSTALL_DATA) $(PRODUCT).1 $(MAN1DIR)/$(PRODUCT)$(man1ext)

install: install-bin install-man

#   Uninstallation

uninstall:
	rm -f $(BINDIR)/$(PRODUCT) $(MAN1DIR)/$(PRODUCT)$(man1ext)

#   There are a variety of levels of cleanliness
#
#	clean:	Remove object code, but leave C and TeX extracts from web
#		and generated documentation.  The current build configuration
#		is preserved.
clean:
	rm -f $(PROGS) *.o *.bak *.shar *.zip *.tar.gz *.dvi core *.out \
	      *.log
#
#	distclean:  Remove everything not present in the distribution.
#		    This wipes out the build configuration; you must
#		    re-run ./configure before rebuilding.
distclean:  clean
	rm -f config.cache config.h Makefile config.log config.status
	rm -f $(PRODUCT).tex $(PRODUCT).idx $(PRODUCT).scn $(PRODUCT).toc
#
#	maintainer-clean:  Remove everything derivative from the
#			   master web.	We don't delete $(PRODUCT).c
#			   because that causes configure to fail.
maintainer-clean: distclean
	@echo 'This command is intended for maintainers to use; it'
	@echo 'deletes files that may need special tools to rebuild.'
	rm -f *.ps *.pdf
	rm -f wretch.c wretch.bin

#   Create distribution archives

ARCH_SOURCE = Makefile.in $(PRODUCT).c $(PRODUCT).w config.h.in getopt.c getopt.h
ARCH_DOC = $(PRODUCT).1 index.html logo.gif \
	   rfc1521.html rfc1521.txt $(PRODUCT).pdf \
	   COPYING INSTALL README
ARCH_BIN = $(PRODUCT).exe
ARCH_CONF = configure configure.in install-sh
ARCH_WRETCH = wretch.w wretch.c wretch.bin

ARCH_FILES = $(ARCH_SOURCE) $(ARCH_DOC) $(ARCH_BIN) $(ARCH_CONF) $(ARCH_WRETCH)

dist:
	#   Make zipped archive of Win32 executable
	rm -f $(PRODUCT).zip
	zip $(PRODUCT).zip $(ARCH_BIN)
	#   Make gzipped tar archive of complete distribution
	rm -f $(TREE).tar.gz
	rm -rf $(TREE)
	mkdir $(TREE)
	chmod 777 $(TREE)
	@echo "Copying distribution files"
	@for file in $(ARCH_FILES); do \
	    cp -p $$file $(TREE); \
	done
	tar cfv $(TREE).tar $(TREE)
	gzip $(TREE).tar
	rm -rf $(TREE)

#   Check for lint

lint:	$(PRODUCT).c
	lint $(PRODUCT).c

#   View manual page

manpage:
	nroff -man $(PRODUCT).1 | more

#   Print manual page

printman:
	ptroff -man $(PRODUCT).1

# Test it

check:	$(PRODUCT) wretch.bin
	./$(PRODUCT) --encode --binary <wretch.bin >/tmp/$(PRODUCT)1.bak
	./$(PRODUCT) --decode </tmp/$(PRODUCT)1.bak >/tmp/$(PRODUCT)2.bak
	-cmp -s wretch.bin /tmp/$(PRODUCT)2.bak ; if test $$? -ne 0  ; then \
	    echo '** $(PRODUCT):  Redirection test failed. **' ; else \
	./$(PRODUCT) -eb wretch.bin /tmp/$(PRODUCT)1.bak ;\
	./$(PRODUCT) -d /tmp/$(PRODUCT)1.bak /tmp/$(PRODUCT)2.bak ;\
	cmp -s wretch.bin /tmp/$(PRODUCT)2.bak ; if test $$? -ne 0  ; then \
	    echo '** $(PRODUCT):  Command line file argument test failed. **' ; else \
	    echo 'All tests passed.' ; fi ; fi
	@rm /tmp/$(PRODUCT)1.bak /tmp/$(PRODUCT)2.bak

# Beat it over the head with an aluminium baseball bat

wringer: $(PRODUCT) wretch.bin
	@echo 'This test performs 13 nested encodes and decodes using'
	@echo 'various options and compares the result to the'
	@echo 'original file.'
	./$(PRODUCT) -e --binary wretch.bin /tmp/$(PRODUCT)1.bak    # E  1
	./$(PRODUCT) -e /tmp/$(PRODUCT)1.bak /tmp/$(PRODUCT)2.bak   # E  2
	./$(PRODUCT) -e /tmp/$(PRODUCT)2.bak /tmp/$(PRODUCT)3.bak   # E  3
	./$(PRODUCT) -e /tmp/$(PRODUCT)3.bak /tmp/$(PRODUCT)4.bak   # E  4
	./$(PRODUCT) -e /tmp/$(PRODUCT)4.bak /tmp/$(PRODUCT)5.bak   # E  5
	./$(PRODUCT) -e --paranoid /tmp/$(PRODUCT)5.bak /tmp/$(PRODUCT)6.bak   # E  6
	./$(PRODUCT) -e </tmp/$(PRODUCT)6.bak | ./$(PRODUCT) -e >/tmp/$(PRODUCT)8.bak	# E  7, 8
	./$(PRODUCT) -e /tmp/$(PRODUCT)8.bak /tmp/$(PRODUCT)9.bak   # E  9
	./$(PRODUCT) -e --paranoid /tmp/$(PRODUCT)9.bak /tmp/$(PRODUCT)a.bak   # E 10
	./$(PRODUCT) -e /tmp/$(PRODUCT)a.bak /tmp/$(PRODUCT)b.bak   # E 11
	./$(PRODUCT) -e /tmp/$(PRODUCT)b.bak /tmp/$(PRODUCT)c.bak   # E 12
	./$(PRODUCT) -e /tmp/$(PRODUCT)c.bak /tmp/$(PRODUCT)d.bak   # E 13
	#
	./$(PRODUCT) -d /tmp/$(PRODUCT)d.bak /tmp/qpdc.bak   # D  1
	./$(PRODUCT) -d /tmp/qpdc.bak /tmp/qpdb.bak   # D  2
	./$(PRODUCT) -d /tmp/qpdb.bak /tmp/qpda.bak   # D  3
	./$(PRODUCT) -d /tmp/qpda.bak /tmp/qpd9.bak   # D  4
	./$(PRODUCT) -d /tmp/qpd9.bak /tmp/qpd8.bak   # D  5
	./$(PRODUCT) -d /tmp/qpd8.bak /tmp/qpd7.bak   # D  6
	./$(PRODUCT) -d /tmp/qpd7.bak /tmp/qpd6.bak   # D  7
	./$(PRODUCT) -d </tmp/qpd6.bak | ./$(PRODUCT) -d >/tmp/qpd4.bak   # D  8, 9
	./$(PRODUCT) -d /tmp/qpd4.bak /tmp/qpd3.bak   # D 10
	./$(PRODUCT) -d /tmp/qpd3.bak /tmp/qpd2.bak   # D 11
	./$(PRODUCT) -d /tmp/qpd2.bak /tmp/qpd1.bak   # D 13
	./$(PRODUCT) -d /tmp/qpd1.bak /tmp/qpd0.bak   # D 13
	#
	-cmp -s /tmp/qpd0.bak wretch.bin ; if test $$? -ne 0  ; then \
	    echo '** $(PRODUCT) Wringer test failed. **' ; else \
	    echo 'Wringer test passed.' ; fi
	@rm /tmp/$(PRODUCT)?.bak /tmp/qpd?.bak

#   Default rules

.c.o:
	$(CC) -c $*.c $(CFLAGS)

.SUFFIXES:
.SUFFIXES: .c .o .ch .dvi .tex .w .1 .bin
