#! /usr/bin/make -f
#
# Last updated: Thu Apr 26 22:00:00 CDT 1995 by david.
#
# To make the binary distribution package, the ``Debianized'' source package
# and the context diff to the original package, type `./debian.rules dist'.
# Make sure that `debian.rules' is executable before the final distribution
# is made.
#
# Invoke each target with `./debian.rules <target>'.  All targets should be
# invoked with the package root as the current directory.
#
# The `binary' target must be run as root, as it needs to install files with
# specific ownerships.  The `diff' target assumes that you have the original
# source package available, unpacked, in ../$(p)-$(v).orig, or that you have
# the previous revision of the ``Debianized'' source package and context diff
# in the parent directory.

include Version.mk
v = $(VERSION)
vm = $(VMAJOR)
ARCH = $(shell dpkg --print-architecture)
ifeq ($(ARCH),i386)
archx = i486
else
archx = $(ARCH)
endif

include Config.mk

build:
# Builds the binary package.
	make ARCH=$(ARCH) clean
	make ARCH=$(ARCH)
	touch build

clean:
# Undoes the effect of `make -f debian.rules build'.
	make ARCH=$(ARCH) clean
	rm -f build
	rm -rf debian/tmp debian/files

binary-indep:   checkroot build
	$(checkdir)

binary-arch:    checkroot build
	$(checkdir)
	#
	##############################
	# build the ldso package
	rm -rf debian/tmp
	install -d -m 755 debian/tmp
	chmod g-s debian/tmp
	#
	install -d -m 755 debian/tmp/sbin
	install -s -m 755 util/ldconfig debian/tmp/sbin/ldconfig.new
ifeq ($(LIBC5_SUPPORT),true)
	install -d -m 755 debian/tmp/usr/bin
	install -s -m 755 util/ldd debian/tmp/usr/bin/ldd
	install -d -m 755 debian/tmp/usr/lib
	install -s -m 755 util/lddstub debian/tmp/usr/lib/lddstub
endif
ifeq ($(AOUT_SUPPORT),true)
	install -d -m 755 debian/tmp/lib
	install -s -m 755 ld-so/ld.so debian/tmp/lib/ld.so.$(v)
endif
ifeq ($(LIBC5_SUPPORT),true)
	install -d -m 755 debian/tmp/lib
	install -m 755 d-link/ld-linux.so debian/tmp/lib/ld-linux.so.$(v)
	strip -g -K _dl_debug_state debian/tmp/lib/ld-linux.so.$(v)
	ln -sf ld-linux.so.$(v) debian/tmp/lib/ld-linux.so.$(vm)
ifneq ($(ARCH),sparc)
	install -s -m 755 d-link/libdl/libdl.so debian/tmp/lib/libdl.so.$(v)
	ln -sf libdl.so.$(v) debian/tmp/lib/libdl.so.$(vm)
endif
endif
	#
	install -d -m 755 debian/tmp/usr/share/man/man8
	install -m 644 man/ldconfig.8 debian/tmp/usr/share/man/man8/ldconfig.8
ifeq ($(LIBC5_SUPPORT),true)
	install -d -m 755 debian/tmp/usr/share/man/man1
	install -m 644 man/ldd.1 debian/tmp/usr/share/man/man1/ldd.1
	install -d -m 755 debian/tmp/usr/share/man/man8
	install -m 644 man/ld.so.8 debian/tmp/usr/share/man/man8/ld.so.8
	gzip -9 debian/tmp/usr/share/man/man*/*
	ln -sf ld.so.8.gz debian/tmp/usr/share/man/man8/ld-linux.so.8.gz
endif
	#
	install -d -m 755 debian/tmp/usr/share/doc/ldso
	install -m 644 README debian/tmp/usr/share/doc/ldso/README
	install -m 644 debian/changelog debian/tmp/usr/share/doc/ldso/changelog
	gzip -9f debian/tmp/usr/share/doc/ldso/*
	install -m 644 debian/copyright debian/tmp/usr/share/doc/ldso/copyright
	#
	install -d -m 755 debian/tmp/DEBIAN
ifeq ($(LIBC5_SUPPORT),true)
	uuencode util/elf-ok elf-ok > /tmp/elf-ok.uue
	sed -e '/^__END__/r /tmp/elf-ok.uue' debian/preinst \
		> debian/tmp/DEBIAN/preinst
	rm -f /tmp/elf-ok.uue
else
	sed -e '/^#MARKER1/,/^#MARKER2/d' debian/preinst \
		> debian/tmp/DEBIAN/preinst
endif
	chmod 755 debian/tmp/DEBIAN/preinst
	sed -e 's/VERSION/$(VERSION)/' debian/postinst \
		> debian/tmp/DEBIAN/postinst
	chmod 755 debian/tmp/DEBIAN/postinst
	install -m 755 debian/prerm debian/tmp/DEBIAN/prerm
ifeq ($(LIBC5_SUPPORT),true)
	install -m 644 debian/shlibs debian/tmp/DEBIAN/shlibs
endif
	#
	dh_md5sums -pldso -Pdebian/tmp
	dpkg-gencontrol -pldso
	chmod -R g-w debian/tmp
	chown -R root.root debian/tmp
	dpkg --build debian/tmp ..
	#
	##############################
	# build the libdl1-altdev package
ifeq ($(LIBC5_SUPPORT),true)
ifneq ($(ARCH),sparc)
	rm -rf debian/tmp
	install -d -m 755 debian/tmp
	chmod g-s debian/tmp
	#
	install -d -m 755 debian/tmp/usr/$(archx)-linuxlibc1/lib
	ln -sf /lib/libdl.so.$(v) \
		debian/tmp/usr/$(archx)-linuxlibc1/lib/libdl.so
	install -d -m 755 debian/tmp/usr/$(archx)-linuxlibc1/include
	install -m 644 d-link/libdl/dlfcn.h \
		debian/tmp/usr/$(archx)-linuxlibc1/include/dlfcn.h
	#
	install -d -m 755 debian/tmp/usr/share/doc
	ln -sf ldso debian/tmp/usr/share/doc/libdl1-altdev
	#
	install -d -m 755 debian/tmp/DEBIAN
	install -m 755 debian/dev.postinst debian/tmp/DEBIAN/postinst
	install -m 755 debian/dev.prerm debian/tmp/DEBIAN/prerm
	dh_md5sums -plibdl1-altdev -Pdebian/tmp
	dpkg-gencontrol -plibdl1-altdev
	chmod -R g-w debian/tmp
	chown -R root.root debian/tmp
	dpkg --build debian/tmp ..
endif
endif

binary: binary-indep binary-arch

checkroot:
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot
