#! /usr/bin/make -f
#
#	debian/rules file for nis
#

# Package name.
p = nis

DEBDIR=$(shell pwd)/debian/tmp

YPSERV=ypserv-1.1.7
YPCLIENTS=yp-clients-2.2
YPPASSWD=yppasswd-0.9
YPBIND=ypbind-bsd-1.15
## YPBIND=ypbind-3.0

define checkdir
        test -f yp-clients-2.2/README
endef

all: build

build:
# Builds the binary package.
	(cd $(YPCLIENTS) && make)
	(cd $(YPBIND) && make -f make.debian)
	## (cd $(YPBIND) && make )
	(cd $(YPPASSWD) && make)
	-(cd $(YPSERV) && [ ! -f config.status ] && \
	AWK=/usr/bin/awk ./configure --sysconfdir=/etc --libexecdir=/usr/lib )
	(cd $(YPSERV) && make)
	touch build

# Undoes the effect of `make -f debian/rules build'.
clean:
	(cd $(YPCLIENTS) && make clean)
	(cd $(YPBIND) && make -f make.debian clean)
	## (cd $(YPBIND) && make clean)
	-(cd $(YPSERV) && [ -f Makefile ] && make distclean)
	(cd $(YPPASSWD) && make distclean)

	-rm -rf $(DEBDIR)
	rm -f build
	find . -name '*.bak' -o -name '*~' | xargs -r rm --
	rm -f $(YPSERV)/config.cache

# Architecture independant files.
binary-indep:	build
	$(checkdir)

# Make a binary package.
binary-arch:	build checkroot
	-rm -rf $(DEBDIR)
	install -d -g root -m 755 $(DEBDIR)
	install -d -g root -m 755 $(DEBDIR)/DEBIAN
	install -d -g root -m 755 $(DEBDIR)/bin
	install -d -g root -m 755 $(DEBDIR)/etc/init.d
	install -d -g root -m 755 $(DEBDIR)/usr/{bin,sbin,man}
	install -d -g root -m 755 $(DEBDIR)/usr/lib/yp
	install -d -g root -m 755 $(DEBDIR)/var/yp
	install -d -g root -m 755 -o root $(DEBDIR)/usr/doc/nis
	install -d -g root -m 755 -o root $(DEBDIR)/usr/man/man{1,5,8}
	chown -R root.root $(DEBDIR)/var/yp
	(cd $(YPSERV) && make install ROOT=$(DEBDIR))
	chown -R root.root $(DEBDIR)
	(cd $(YPPASSWD) && make install DEBDIR=$(DEBDIR))
	(cd $(YPBIND) && make -f make.debian install DEBDIR=$(DEBDIR))
	## (cd $(YPBIND) && make install DEBDIR=$(DEBDIR))
	(cd $(YPCLIENTS) && make install DEBDIR=$(DEBDIR))
	install -g root -m 755 debian/ypxfr_1perhour $(DEBDIR)/usr/lib/yp
	install -g root -m 755 debian/ypxfr_1perday $(DEBDIR)/usr/lib/yp
	install -g root -m 755 debian/ypxfr_2perday $(DEBDIR)/usr/lib/yp
	install -g root -m 755 debian/rc.nis $(DEBDIR)/etc/init.d/nis
	install -g root -m 644 debian/netgroup.sample $(DEBDIR)/etc/netgroup
	install -g root -m 644 debian/ypserv.securenets $(DEBDIR)/etc
	install -g root -m 644 debian/ypserv.conf.debian $(DEBDIR)/etc/ypserv.conf
	# Documentation.
	install -g root -m 644 debian/*.1 $(DEBDIR)/usr/man/man1
	install -g root -m 644 debian/*.8 $(DEBDIR)/usr/man/man8
	gzip -9f $(DEBDIR)/usr/man/man*/*
	install -g root -m 644 debian/COPYRIGHT \
	  $(DEBDIR)/usr/doc/$(p)/copyright
	install -g root -m 644 debian/nis.debian.howto $(DEBDIR)/usr/doc/nis
	install -g root -m 644 $(YPPASSWD)/README \
		$(DEBDIR)/usr/doc/nis/yppasswd.readme
	install -g root -m 644 debian/changelog \
		$(DEBDIR)/usr/doc/nis/changelog.Debian
	install -g root -m 644 $(YPSERV)/ChangeLog \
		$(DEBDIR)/usr/doc/nis/changelog
	gzip -9f $(DEBDIR)/usr/doc/nis/*
	#
	install -g root -m 644 debian/conffiles \
	  $(DEBDIR)/DEBIAN/conffiles
	install -g root -m 755 debian/postinst \
	  $(DEBDIR)/DEBIAN/postinst
	install -g root -m 755 debian/prerm \
	  $(DEBDIR)/DEBIAN/prerm
	install -g root -m 755 debian/postrm \
	  $(DEBDIR)/DEBIAN/postrm
	# Use some binaries to generate the dependencies.
	dpkg-shlibdeps $(YPSERV)/ypserv $(YPPASSWD)/yppasswdd
	dpkg-gencontrol
	dpkg --build $(DEBDIR) ..
	rm -rf $(DEBDIR)

binary:	binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

dist:
	dpkg-source -b
