#!/usr/bin/make -f
SHELL+= -e

USE_SELINUX := false

QUILT_STAMPFN=debian/.stamp-patched
include /usr/share/quilt/quilt.make

D  := $(CURDIR)/debian/udev
DL := $(CURDIR)/debian/libvolume-id0
DD := $(CURDIR)/debian/libvolume-id-dev

MAKE_EXTRAS := extras/ata_id/ extras/cdrom_id/ extras/edd_id extras/path_id \
	extras/scsi_id/ extras/usb_id/ extras/volume_id/

DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

MAKE_VARIABLES := V=true EXTRAS="$(MAKE_EXTRAS)"

all: build

clean: unpatch
	$(MAKE) clean $(MAKE_VARIABLES) USE_SELINUX=$(USE_SELINUX)
	dh_clean

build: patch
	dh_testdir
	$(MAKE) all $(MAKE_VARIABLES) USE_SELINUX=$(USE_SELINUX)

binary-arch: checkroot
	dh_testdir
	dh_clean -k
	dh_installdirs

	$(MAKE) install $(MAKE_VARIABLES) DESTDIR=$D

	dh_movefiles --sourcedir=debian/udev
	rmdir $D/dev/ $D/usr/lib/pkgconfig/ $D/usr/include/
	rm -f $D/etc/udev/rules.d/*

	install --mode=755 extra/*-devfs.sh \
		hotplug/*.agent hotplug/scsi-re-add \
		extras/rule_generator/write_*_rules $D/lib/udev/

	cp etc/udev/debian/* extra/links.conf $D/etc/udev/
	cp grml/*.rules $D/etc/udev
	printf "# maximum size of the /dev tmpfs\ntmpfs_size=\"10M\"\n\n" \
		>> $D/etc/udev/udev.conf
	sed -e 's/^#\([^ ]\)/\1/' < $D/etc/udev/compat.rules > \
		$D/etc/udev/compat-full.rules

	install --mode=644 hotplug/hotplug.functions $D/lib/udev/
	install --mode=644 hotplug/modprobe.d/* $D/etc/modprobe.d/

	cp extra/reportbug.presubj $D/usr/share/bug/udev/presubj
	install --mode=755 extra/reportbug.script $D/usr/share/bug/udev/script

	install --mode=755 extra/initramfs.hook \
		$D/usr/share/initramfs-tools/hooks/udev
	install --mode=755 extra/initramfs.premount \
		$D/usr/share/initramfs-tools/scripts/init-premount/udev
	install --mode=755 extra/initramfs.bottom \
		$D/usr/share/initramfs-tools/scripts/init-bottom/udev

	dh_installchangelogs ChangeLog
	dh_installdocs FAQ README RELEASE-NOTES TODO docs/writing_udev_rules/ grml/README.grml
	cp extras/volume_id/README $D/usr/share/doc/udev/README.vol_id
	dh_installexamples \
		etc/udev/suse/ etc/udev/redhat/ etc/udev/gentoo/ \
		extra/udev.vim
	dh_installinit --no-start --update-rcd-params='start 03 S .'
	dh_installinit --no-start --update-rcd-params='start 36 S .' \
		--name=udev-mtab

	# remove duplicate changelogs
	rm -rf $(DD)/usr/share/doc/libvolume-id-dev/
	cd $(DD)/usr/share/doc/ && ln -s libvolume-id0 libvolume-id-dev

	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs --version-info='libvolume-id0 (>= 0.113)'
	dh_shlibdeps --libpackage=libvolume-id0 -l $(DL)/lib/
	dh_installdeb
	dh_gencontrol $(NO_PACKAGE)
	dh_builddeb $(NO_PACKAGE)


binary:	binary-arch

checkroot:
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep unpack configure build clean checkroot
