#! /usr/bin/make -f
#

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# liblockfile
MVER	= 1
SOVER	= 1.0

tmp	= debian/tmp

do_md5sums = cd $(tmp) && find -type f \! -regex '.*/DEBIAN/.*' -printf '%P\0' | xargs -r0 md5sum >DEBIAN/md5sums

INSTALL=install
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL+=-s
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	export CC = $(DEB_HOST_GNU_TYPE)-gcc
	INSTALL += --strip-program=$(DEB_HOST_GNU_TYPE)-strip
endif

build-arch:	config.status
	$(checkdir)
	make
	touch build

build-indep:

config.status:
	./configure --enable-shared --prefix=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--with-mailgroup --mandir=/usr/share/man
	
clean:
	rm -rf build $(tmp)* debian/files debian/substvars
	[ ! -f Makefile ] || make distclean

binary-indep: checkroot
	$(checkdir)

binary-arch: checkroot
	$(checkdir)
	#
	#	First, liblockfile.so.1
	#
	rm -rf $(tmp)*
	install -d -o root -m 755 $(tmp)
	install -d -o root -m 755 $(tmp)/DEBIAN
	install -d -o root -m 755 $(tmp)/usr/lib
	install -d -o root -m 755 $(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)
	install -d -o root -m 755 $(tmp)/usr/share/doc/liblockfile1
	#
	install -o root -m 755 debian/postinst $(tmp)/DEBIAN
	install -o root -m 755 debian/postrm $(tmp)/DEBIAN
	install -o root -m 644 debian/shlibs $(tmp)/DEBIAN
	$(INSTALL) -o root -m 644 liblockfile.so \
		$(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)/liblockfile.so.$(SOVER)
	ln -s liblockfile.so.$(SOVER) \
		$(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)/liblockfile.so.$(MVER)
	install -o root -m 644 debian/changelog \
		$(tmp)/usr/share/doc/liblockfile1/changelog.Debian
	install -o root -m 644 Changelog \
		$(tmp)/usr/share/doc/liblockfile1/changelog
	gzip -n -9f $(tmp)/usr/share/doc/liblockfile1/*
	install -o root -m 644 COPYRIGHT \
		$(tmp)/usr/share/doc/liblockfile1/copyright
	$(do_md5sums)
	dpkg-shlibdeps liblockfile.so
	dpkg-gencontrol -pliblockfile1 -P$(tmp) -isp
	dpkg --build $(tmp) ..
	#
	#	Build liblockfile-bin
	#
	rm -rf $(tmp)*
	rm -f debian/substvars
	install -d -o root -m 755 $(tmp)
	install -d -o root -m 755 $(tmp)/DEBIAN
	install -d -o root -m 755 $(tmp)/usr/bin
	install -d -o root -m 755 $(tmp)/usr/share/man/man1
	install -d -o root -m 755 $(tmp)/usr/share/doc/liblockfile-bin
	#
	install -o root -m 644 dotlockfile.1 $(tmp)/usr/share/man/man1
	gzip -9fn $(tmp)/usr/share/man/*/*
	$(INSTALL) -g mail -m 2755 dotlockfile $(tmp)/usr/bin/dotlockfile
	install -o root -m 644 debian/changelog \
		$(tmp)/usr/share/doc/liblockfile-bin/changelog.Debian
	install -o root -m 644 Changelog \
		$(tmp)/usr/share/doc/liblockfile-bin/changelog
	gzip -9fn $(tmp)/usr/share/doc/liblockfile-bin/*
	install -o root -m 644 COPYRIGHT \
		$(tmp)/usr/share/doc/liblockfile-bin/copyright
	install -d -o root -m 755 $(tmp)/usr/share/lintian
	install -d -o root -m 755 $(tmp)/usr/share/lintian/overrides
	install -o root -m 644 debian/liblockfile-bin.overrides \
		$(tmp)/usr/share/lintian/overrides/liblockfile-bin
	$(do_md5sums)
	dpkg-shlibdeps dotlockfile
	dpkg-gencontrol -pliblockfile-bin -P$(tmp) -isp
	dpkg --build $(tmp) ..
	#
	#	Build liblockfile-dev
	#
	rm -rf $(tmp)*
	rm -f debian/substvars
	install -d -o root -m 755 $(tmp)
	install -d -o root -m 755 $(tmp)/DEBIAN
	install -d -o root -m 755 $(tmp)/usr/lib
	install -d -o root -m 755 $(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)
	install -d -o root -m 755 $(tmp)/usr/include
	install -d -o root -m 755 $(tmp)/usr/share/man/man3
	install -d -o root -m 755 $(tmp)/usr/share/doc/liblockfile-dev
	#
	install -o root -m 644 lockfile.h maillock.h $(tmp)/usr/include
	ln -s liblockfile.so.$(SOVER) \
		$(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)/liblockfile.so
	install -o root -m 644 liblockfile.a $(tmp)/usr/lib/$(DEB_HOST_MULTIARCH)
	install -o root -m 644 maillock.3 $(tmp)/usr/share/man/man3
	install -o root -m 644 lockfile_create.3 $(tmp)/usr/share/man/man3
	gzip -9f $(tmp)/usr/share/man/*/*
	ln -sf lockfile_create.3.gz $(tmp)/usr/share/man/man3/lockfile_check.3.gz
	ln -sf lockfile_create.3.gz $(tmp)/usr/share/man/man3/lockfile_touch.3.gz
	ln -sf lockfile_create.3.gz $(tmp)/usr/share/man/man3/lockfile_remove.3.gz
	ln -sf maillock.3.gz $(tmp)/usr/share/man/man3/mailunlock.3.gz
	ln -sf maillock.3.gz $(tmp)/usr/share/man/man3/touchlock.3.gz
	install -o root -m 644 debian/changelog \
		$(tmp)/usr/share/doc/liblockfile-dev/changelog.Debian
	install -o root -m 644 Changelog \
		$(tmp)/usr/share/doc/liblockfile-dev/changelog
	gzip -9f $(tmp)/usr/share/doc/liblockfile-dev/*
	install -o root -m 644 COPYRIGHT \
		$(tmp)/usr/share/doc/liblockfile-dev/copyright
	$(do_md5sums)
	dpkg-gencontrol -pliblockfile-dev -P$(tmp) -isp
	dpkg --build $(tmp) ..


define checkdir
	test -f lockfile.c -a -f debian/rules
endef

binary: binary-indep binary-arch

build:	build-indep build-arch

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

.PHONY: config clean build build-arch build-indep binary binary-arch binary-indep

