#
# Makefile for Domtools
#
# by Paul Balyoz
#
# Useful targets:   all  install  clean
# See README for usage.
#
# NOTES
#	Domtools consist of many programs and lib files with simple
#	names (which may conflict with other stuff on your system).
#	That's why I usually put them in their own sub-directory
#	named "domtools" within bin/ and lib/ dirs.  That way
#	anyone who wants to use these tools can add the new bin
#	directory to their path.
#

# Physical Installation Hierarchy - root of tree where Makefile should
# write files to.  For AFS use your "dot" path (writable branch).
PDEST=/usr/local
#PDEST=/afs/.site.dom.ain/sun4/usr/local
#PDEST=/home/pbalyoz
#PDEST=/oper/oper2/pbalyoz

# Logical Installation Hierarchy - paths that shell scripts should use
# when referring to lib files & programs from this package.
# Usually this is the same as PDEST.
LDEST=$(PDEST)
#LDEST=/afs/site.dom.ain/sun4/usr/local

# Binary sub-directory under $PDEST where executables should go.
# This directory is created below if it doesn't exist.
#	"bin" will put it in $LDEST/bin.
#	"bin/domtools" (my favorite) puts it in $LDEST/bin/domtools.
#DOMBIN=bin
DOMBIN=bin/domtools
#DOMBIN=bin_hp9000/domtools
#DOMBIN=bin_ibm6000/domtools
#DOMBIN=bin_sun4/domtools

# Library sub-directory under $PDEST where other domain files such as awk scripts should go.
# This directory is created below if it doesn't exist.
DOMLIB=lib/domtools

# Do you want a world-writable Zone Cache directory, or not?
# Good points: speeds up the axfr tool substantially.
# Bad points: anyone can store files in there to get around their disk quota;
#             this directory always grows (not automatically cleaned out).
# Uncomment exactly 1 of these lines:
ZONECACHEWANTED=1		# yes we want a zone cache
#ZONECACHEWANTED=0		# no we don't want a zone cache

# Path to the zone cache directory (ignored if $ZONECACHEWANTED is 0).
ZONEDIR=$(DOMLIB)/zonecache

# The subdomain at your site/domain which contains nothing but TXT records
# for every network name at your site/domain.  These TXT records can be
# created to describe each network or keep track of important location
# information: building, room, department, whatever you want.
# They are used by networktbl and netmasktbl when -x specified and are written
# as comment lines above each entry in their output files.  For example,
# the resource record:
#
#    csenet.networks.nau.edu. IN TXT "Computer Science Engineering, bldg 69, floor 1"
#
# would produce a comment line above the entry:
#
#	  # Computer Science Engineering, bldg 69, floor 1
#	  134.114.64 csenet
#
# The comment line, above, might have come from a TXT record in domain.nau.edu.
# It makes the /etc/network and /etc/netmask files they generate much clearer.
# The domain below _must_ have an ending period on it.
# This is not an Internet standard of any sort; it's just a useful trick
# used in Northern Arizona University's Computer Science department.
#
# This feature is optional; set NETWORKSDOM to nothing to disable it.
#
#NETWORKSDOM=networks.nau.edu.		# just an example!  change before using!
NETWORKSDOM=

# Number of levels of host aliases that should be generated by hosttbl when
# outputting /etc/host table data, by default (can be overridden on command line).
# Example values:       0   =>   1.2.3.4 host.cse.nau.edu
#                       1   =>   1.2.3.4 host.cse.nau.edu  host
#                       2   =>   1.2.3.4 host.cse.nau.edu  host host.cse
# You can use any number in the range 0..N where N is the minimu
#
ALIASLEVELS=2

# If you're installing this systemwide as the root user, specify "chown".
# If you're installing this as a normal user, specify the "true" program instead.
# Uncomment exactly 1 of the following lines:
#CHOWN=chown		# yes we are root and can run "chown".
CHOWN=true		# no we aren't root and can't run "chown".

# Owner for installing Domtools directories and files.
# If CHOWN set to true, above, then OWNER is ignored.
#OWNER= root
OWNER= pbalyoz

# Group for installing Domtools directories and files.
#GROUP= staff
#GROUP= oper
GROUP= user

# Chown option to "install" specification.
# If you're root, use the first line, otherwise use the second.
# Uncomment exactly 1 of the following lines:
#CHOWNOPT = -o $(OWNER)		# If we're installing as root
CHOWNOPT =			# If we're installing as normal user

# BSD-style installation command.
# First line is for BSD or SunOS; second is for SysV like Solaris 2.
# You can use ginstall (gnu) if you have it.
INSTALL=install
#INSTALL=/usr/ucb/install
#INSTALL=/usr/local/gnu/bin/install
#INSTALL=/stor/gnu/aix/bin/install

# Where is your Perl executable located?  /bin/perl is _supposed_ to work,
# but some sites still don't have it there.
PERLPROG=/bin/perl
#PERLPROG=/usr/local/bin/perl

# C compiler you prefer: (gcc is OK too)
CC=cc

# Which Bourne-shell should Bourne-shell shell scripts use?
# You'd think /bin/sh works everywhere, wouldn't you.
# BSDI (at least thru version 2.1) have a bug in /bin/sh.  Test it out:
#		true; `false`; echo $?
# The correct answer is any non-zero number (false), but BSDI returns 0 (true)!
# BSDI's ksh and bash do the right thing though, as does /bin/sh, ksh, and bash on
# every other platform I've tried (AIX 3.2.x, AIX 4.1.x, HP/UX 9.x, HP/UX 10.x,
# SunOS 4.1.x, Solaris 2.x, Linux, NCR SVR4).
# Use /bin/sh here, unless your /bin/sh returned true to the commands above.
BOURNESHELL=/bin/sh
#BOURNESHELL=/bin/bash		# BSDI only!

# A few scripts need the gnu version of awk, sometimes called gawk, because they
# pass arguments into the awk scripts using the -v option.
# Some newer operating systems like BSDI have gawk installed as "awk".
# Regardless of the name, it _must_ be a gnu-based awk.  The man-page will tell.
# If you don't have gawk, get it and install it, otherwise hosttbl,networktbl,netmasktbl
# tools won't work right.  Gawk is much better than awk or nawk anyway.
GAWKPROG=gawk			# many other systems
#GAWKPROG=awk			# BSDI

#---------------------------------------------------------------------------------------
#   END OF CONFIGURABLE PARAMETERS!
#---------------------------------------------------------------------------------------

# All files in distribution:
FILES=	README INSTALL MANUAL HOWTOUSE COPYRIGHT TODO BUGS WHY CHANGES \
	Makefile address addr2mask addr2net.c axfr axfroutany.awk basedomain \
	basedomain.awk check1101 domsort domsort.awk f2iaddr \
	f2iaddr.awk hinfo hosts hosttbl hosttbl.awk \
	hosttbl.footer hosttbl.header i2faddr i2faddr.awk ipsort ipsort.awk \
	isequal issubdom makestatic netkillzeros \
	netkillzeros.awk netmask network netname netwithzeros netwithzeros.awk\
	ns nsroot ptr root soa subdom subdom.awk subdom2 subzone subzone.awk \
	txt type type.awk zone any digoutany.awk wks mx rndarg nsap \
	gensubnetlist.c networktbl networktbl.footer networktbl.header \
	networktbl.awk netmasktbl netmasktbl.awk netmasktbl.header \
	netmasktbl.footer uinfo cachebuild localad localdom subnetmask \
	siteinfo cname hosttbl.perl soalist

# Shell script commands:
SHS=	address addr2mask axfr basedomain check1101 domsort f2iaddr hinfo \
	hosts hosttbl i2faddr ipsort isequal issubdom makestatic \
	netkillzeros netmask network netname netwithzeros nsap \
	ns nsroot ptr root soa subdom subdom2 subzone txt type zone any wks \
	mx networktbl netmasktbl uinfo cachebuild localad localdom subnetmask \
	siteinfo cname rndarg soalist

# Items for lib/ directory:
LIBS=	basedomain.awk domsort.awk f2iaddr.awk hosttbl.awk \
	i2faddr.awk ipsort.awk netkillzeros.awk \
	netwithzeros.awk subdom.awk subzone.awk type.awk digoutany.awk \
	hosttbl.footer hosttbl.header networktbl.footer networktbl.header \
	networktbl.awk netmasktbl.footer netmasktbl.header netmasktbl.awk \
	axfroutany.awk hosttbl.perl

# Document files for lib/ directory:
DOCS=	README MANUAL HOWTOUSE COPYRIGHT

# Executables that aren't shell scripts (these get deleted by "make clean")
EXECS= addr2net gensubnetlist

#-------

# Sed input file for replacing macro strings with full pathnames
# in the various programs during installation.
# See below for the set of macro replacements.
MACRO=macros.sed

#-------

all:		$(EXECS)

addr2net:	addr2net.c
		$(CC) -O addr2net.c -o addr2net

gensubnetlist:	gensubnetlist.c
		$(CC) -O gensubnetlist.c -o gensubnetlist

#-------

$(MACRO):	Makefile
		-rm -f $(MACRO)
		echo "s,DOMBIN,$(LDEST)/$(DOMBIN),g" >> $(MACRO)
		echo "s,DOMLIB,$(LDEST)/$(DOMLIB),g" >> $(MACRO)
		echo "s,ZONEDIR,$(LDEST)/$(ZONEDIR),g" >> $(MACRO)
		echo "s,ZONECACHEWANTED,$(ZONECACHEWANTED),g" >> $(MACRO)
		echo "s,PERLPROG,$(PERLPROG),g" >> $(MACRO)
		echo "s,NETWORKSDOM,$(NETWORKSDOM),g" >> $(MACRO)
		echo "s,ALIASLEVELS,$(ALIASLEVELS),g" >> $(MACRO)
		echo "s,BOURNESHELL,$(BOURNESHELL),g" >> $(MACRO)
		echo "s,GAWKPROG,$(GAWKPROG),g" >> $(MACRO)

#-------

# Install all tools and related files systemwide.

install:	install-bins install-libs

install-bins:	$(SHS) $(EXECS) $(MACRO) delete-old-bins
		if test ! -d $(PDEST)/$(DOMBIN); then \
			mkdir $(PDEST)/$(DOMBIN); \
			chmod 755 $(PDEST)/$(DOMBIN); \
			chgrp $(GROUP) $(PDEST)/$(DOMBIN); \
			$(CHOWN) $(OWNER) $(PDEST)/$(DOMBIN); \
		fi
		for i in $(SHS); do \
			sed -f $(MACRO) < $$i > xxxtool; \
			$(INSTALL) -c -m 755 $(CHOWNOPT) -g $(GROUP) xxxtool $(PDEST)/$(DOMBIN)/$$i; \
		done
		-rm -f xxxtool
		for i in $(EXECS); do \
			$(INSTALL) -c -s -m 755 $(CHOWNOPT) -g $(GROUP) $$i $(PDEST)/$(DOMBIN); \
		done
		-rm -f $(PDEST)/$(DOMBIN)/gw > /dev/null 2>&1
		-ln -s netname $(PDEST)/${DOMBIN}/gw

delete-old-bins:
		-rm -f $(PDEST)/$(DOMBIN)/ad

install-libs:	$(LIBS) $(DOCS) $(MACRO) delete-old-libs
		if test ! -d $(PDEST)/$(DOMLIB); then \
			mkdir $(PDEST)/$(DOMLIB); \
			chmod 755 $(PDEST)/$(DOMLIB); \
			chgrp $(GROUP) $(PDEST)/$(DOMLIB); \
			$(CHOWN) $(OWNER) $(PDEST)/$(DOMLIB); \
		fi
		# Don't panic; ZONEDIR (zonecache directory) must be writable by everyone.
		# (If you don't want this, set ZONECACHEWANTED to 0, above).
		if test ! -d $(PDEST)/$(ZONEDIR) -a $(ZONECACHEWANTED) -eq 1; then \
			mkdir $(PDEST)/$(ZONEDIR); \
			chmod 777 $(PDEST)/$(ZONEDIR); \
			chgrp $(GROUP) $(PDEST)/$(ZONEDIR); \
			$(CHOWN) $(OWNER) $(PDEST)/$(ZONEDIR); \
		fi
		for i in $(LIBS); do \
			sed -f $(MACRO) < $$i > xxxtool; \
			$(INSTALL) -c -m 644 $(CHOWNOPT) -g $(GROUP) xxxtool $(PDEST)/$(DOMLIB)/$$i; \
		done
		-rm -f xxxtool
		for i in $(DOCS); do \
			$(INSTALL) -c -m 644 $(CHOWNOPT) -g $(GROUP) $$i $(PDEST)/$(DOMLIB)/$$i; \
		done

delete-old-libs:
	-rm -f $(PDEST)/$(DOMLIB)/digout.awk

#-------

#
# Pack up all the files, ready for distribution.
#

dist:			/tmp/domtools.tar.gz
		@echo "distribution is /tmp/domtools.tar.gz"

/tmp/domtools.tar.gz:	/tmp/domtools.tar
		gzip -f /tmp/domtools.tar

/tmp/domtools.tar:
		tar cf /tmp/domtools.tar $(FILES)

#-------

#
# Clean up all extraneous files.  This does NOT damage the software package.
#

clean:
		-rm -f $(EXECS) $(MACRO) xxxtool *.o a.out core

#-------

#
# Spell check all english documents.
#

spell:
		{ for i in README MANUAL HOWTOUSE WHY COPYRIGHT TODO BUGS CHANGES INSTALL TESTS; do \
			ispell -p WORDS -l < $$i; \
		done } | sort -u

# End of Makefile.
