#!/usr/bin/make -f

# To build on an i386 machine with amd64 kernel, use 
#   linux32 dpkg-buildpackage ...

# Debian package information
package		:= modutils
arch		:= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
version		:= $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)/\1/p')

docdir		:= /usr/share/doc/$(package)
build_dir	:= debian/build
udeb_build_dir	:= debian/build-udeb
tmpdir		:= $(shell pwd)/debian/tmp/
udeb_tmpdir	:= $(shell pwd)/debian/tmp-udeb/

stamp-config:
	mkdir -p $(build_dir)
	cd $(build_dir) ; ../../configure --sysconfdir=/etc \
		--infodir='$${prefix}'/share/info \
		--mandir='$${prefix}'/share/man
	touch stamp-config

stamp-build: stamp-config
	cd $(build_dir) ; $(MAKE)
	touch stamp-build

stamp-udeb-config:
	mkdir -p $(udeb_build_dir)
	cd $(udeb_build_dir) ; \
		CFLAGS="-Os -fomit-frame-pointer" ../../configure \
		--sysconfdir=/etc \
		--infodir='$${prefix}'/share/info \
		--mandir='$${prefix}'/share/man
	touch stamp-udeb-config

stamp-udeb-build: stamp-udeb-config
	cd $(udeb_build_dir) ; $(MAKE)
	touch stamp-udeb-build

build: stamp-build stamp-udeb-build

clean:
	test -d debian
	rm -rf $(build_dir) $(udeb_build_dir)
	rm -rf $(tmpdir) $(udeb_tmpdir)
	rm -rf debian/files* debian/substvars
	rm -f stamp-config stamp-build
	rm -f stamp-udeb-config stamp-udeb-build

binary-indep:
	@echo Nothing to do

binary-udeb-arch: stamp-udeb-build
	test -d debian
	-rm -rf $(udeb_tmpdir) $(tmpdir)
	install -d -m 755 -o root -g root $(tmpdir)
	install -d -m 755 -o root -g root $(udeb_tmpdir)

	# Do the basic install
	cd $(udeb_build_dir) ; $(MAKE) prefix=$(tmpdir)/usr \
		exec_prefix=$(tmpdir) \
		sysconfdir=$(tmpdir)/etc install

	# Trick trick: copy only the things we really need. Note we use
	# tar in order to preserve (hard)links
	set -e ; \
		( cd $(tmpdir) ; \
		  tar cf - sbin/modprobe sbin/insmod sbin/depmod ) | \
		( cd $(udeb_tmpdir) ; tar xvpf - ) 

	# Add some Debian flavour
	strip --strip-unneeded -R .comment $(udeb_tmpdir)/sbin/*
	install -d -m 755 -o root -g root $(udeb_tmpdir)/DEBIAN

	# Lets build the full udeb now
	dpkg-shlibdeps $(udeb_tmpdir)/sbin/*
	dpkg-gencontrol -pmodutils-full -P$(udeb_tmpdir) \
		-nmodutils-full_$(version)_$(arch).udeb
	dpkg --build $(udeb_tmpdir) ../modutils-full_$(version)_$(arch).udeb

	# Now remove a few more things and do the basic udeb
	rm -f $(udeb_tmpdir)/sbin/depmod
	dpkg-shlibdeps $(udeb_tmpdir)/sbin/*
	dpkg-gencontrol -pmodutils-basic -P$(udeb_tmpdir) \
		-nmodutils-basic_$(version)_$(arch).udeb
	dpkg --build $(udeb_tmpdir) ../modutils-basic_$(version)_$(arch).udeb


binary-arch: checkroot stamp-build binary-udeb-arch
	test -d debian
	-rm -rf $(tmpdir)
	install -d -m 755 -o root -g root $(tmpdir)

# Setup directories
	install -d -m 755 -o root -g root $(tmpdir)/sbin
	install -d -m 755 -o root -g root $(tmpdir)/bin
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/man1
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/man2
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/man5
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/man8
	install -d -m 755 -o root -g root $(tmpdir)/lib/modules
# For translations
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/es/man5
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/es/man8
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/fr/man5
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/fr/man8
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/pt_BR/man5
	install -d -m 755 -o root -g root $(tmpdir)/usr/share/man/pt_BR/man8

	cd $(build_dir) ; $(MAKE) prefix=$(tmpdir)/usr exec_prefix=$(tmpdir) \
		sysconfdir=$(tmpdir)/etc install

	rm -f $(tmpdir)/usr/share/man/man8/kdstat.8

# Do some changes for Debian
	-strip $(tmpdir)/sbin/*

# add links in /bin for FHS.
	ln -s ../sbin/kernelversion $(tmpdir)/bin

	chmod -R u=rwX,go=rX $(tmpdir)/usr/share/man/
	find $(tmpdir)/usr/share/man/ -xdev -noleaf \! -type d \! -type l \
	   -perm +0111 -exec /bin/chmod 0644 \{\} \;
	find $(tmpdir)/usr/share/man/ -type f -exec gzip -9 {} \;
	install -d -m 755 -o root -g root $(tmpdir)/etc/cron.daily
	install -p -m 755 -o root -g root debian/cron.daily \
		$(tmpdir)/etc/cron.daily/modutils

# Install documentation
	install -d -m 755 -o root -g root $(tmpdir)$(docdir)
	set -e ; for i in CREDITS NEWS README TODO ; do \
		install -p -m 644 -g root -o root $$i $(tmpdir)/$(docdir)/ ; \
	done
	install -p -m 644 -g root -o root ChangeLog \
		$(tmpdir)/$(docdir)/changelog
	gzip -9f $(tmpdir)/$(docdir)/*

# Install examples
	install -d -m 755 -o root -g root $(tmpdir)$(docdir)/examples
	install -p -g root -o root debian/examples/* \
			$(tmpdir)/$(docdir)/examples

# Install Debian-only documentation
	install -p -g root -m 644 -o root debian/modules.5 \
			$(tmpdir)/usr/share/man/man5
	gzip -9q $(tmpdir)/usr/share/man/man5/modules.5
	install -p -g root -m 644 -o root debian/module-policy \
			$(tmpdir)/$(docdir)/module-policy.Debian
	install -p -g root -m 644 -o root debian/changelog \
			$(tmpdir)/$(docdir)/changelog.Debian
	install -p -g root -m 644 -o root debian/README \
			$(tmpdir)/$(docdir)/README.Debian
	gzip -9f $(tmpdir)/$(docdir)/*Debian
	install -p -g root -m 644 -o root debian/copyright $(tmpdir)/$(docdir)

# Install Debian utils
	install -p -m 755 -o root -g root debian/update-modules $(tmpdir)/sbin/
	install -p -m 644 -o root -g root debian/update-modules.8 \
			$(tmpdir)/usr/share/man/man8/
	gzip -9q $(tmpdir)/usr/share/man/man8/*8

# Install translations
	install -p -m 644 -o root -g root debian/update-modules.es.8 \
			$(tmpdir)/usr/share/man/es/man8/update-modules.8
	install -p -m 644 -o root -g root debian/modules.es.5 \
			$(tmpdir)/usr/share/man/es/man5/modules.5
	install -p -m 644 -o root -g root debian/update-modules.fr.8 \
			$(tmpdir)/usr/share/man/fr/man8/update-modules.8
	install -p -m 644 -o root -g root debian/modules.fr.5 \
			$(tmpdir)/usr/share/man/fr/man5/modules.5
	install -p -m 644 -o root -g root debian/update-modules.pt_BR.8 \
			$(tmpdir)/usr/share/man/pt_BR/man8/update-modules.8
	install -p -m 644 -o root -g root debian/modules.pt_BR.5 \
			$(tmpdir)/usr/share/man/pt_BR/man5/modules.5

# Install configuration files
	install -d -g root -m 755 -o root $(tmpdir)/etc/modutils
	cp -a debian/conf/* $(tmpdir)/etc/modutils/
	chown -R root.root $(tmpdir)/etc/modutils

	install -d -g root -m 755 -o root $(tmpdir)/etc/init.d
	install -p -g root -m 755 -o root debian/rc.modutils \
		$(tmpdir)/etc/init.d/modutils
	
# Install Debian system files
	install -d -m 755 -o root -g root $(tmpdir)/DEBIAN
	install -p -m 644 -o root -g root debian/conffiles $(tmpdir)/DEBIAN
	install -p -m 755 -o root -g root debian/preinst $(tmpdir)/DEBIAN
	install -p -m 755 -o root -g root debian/postinst $(tmpdir)/DEBIAN
	install -p -m 755 -o root -g root debian/prerm $(tmpdir)/DEBIAN
	install -p -m 755 -o root -g root debian/postrm $(tmpdir)/DEBIAN

# Some left-over stuff
	install -d -m 755 -o root -g root $(tmpdir)/usr/bin
	ln -s /sbin/genksyms $(tmpdir)/usr/bin/genksyms

	dpkg-shlibdeps $(tmpdir)/sbin/*
	dpkg-gencontrol -isp -pmodutils
	chown -R root.root $(tmpdir)
	chmod -R go=rX $(tmpdir)
	dpkg --build $(tmpdir) ..

# Below here is fairly generic really

binary:		binary-indep binary-arch

checkroot:
	test 0 = "`id -u`"

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

# vi: ts=8 sw=8 ai nowrap
