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

BUILD_UDEB := 1
USE_SELINUX := 1



DPKG_EXPORT_BUILDFLAGS = 1
-include /usr/share/dpkg/buildflags.mk

D  := $(CURDIR)/debian/udev
DU := $(CURDIR)/debian/udev-udeb
DGU:= $(CURDIR)/debian/udev-gtk-udeb

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CONFFLAGS := --prefix=/usr --sysconfdir=/etc --sbindir=/sbin \
  --libexecdir=/lib/udev \
  --enable-static --disable-dependency-tracking
CONFFLAGS_deb  := $(CONFFLAGS) \
  --with-rootlibdir=/lib/$(DEB_HOST_MULTIARCH) \
  --enable-gtk-doc --enable-introspection \
  --enable-rule_generator --enable-udev_acl \
  --enable-edd --with-systemdsystemunitdir=/lib/systemd/system
CONFFLAGS_udeb := $(CONFFLAGS) \
  --with-rootlibdir=/lib

##############################################################################
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifdef USE_SELINUX
  CONFFLAGS_deb += --with-selinux
endif

ifdef BUILD_UDEB
BUILD_UDEB_TARGET := .stamp-build-udeb
else
NO_PACKAGE := --no-package=udev-udeb --no-package=udev-gtk-udeb
endif

##############################################################################
clean:
	rm -rf .stamp-* build-* debian/*.substvars
	rm -f aclocal.m4 autom4te.cache/* config.h.in config.h.in~ configure \
		ltmain.sh install-sh missing depcomp config.sub config.guess \
		gtk-doc.make m4/* $$(find . -name Makefile.in)
	dh_clean

autoreconf: .stamp-autoreconf
.stamp-autoreconf:
	gtkdocize
	# improve support for out of tree builds, and it's still not enough
	sed 's/srcdir/builddir/g' < gtk-doc.make > gtk-doc.make.tmp && \
		mv gtk-doc.make.tmp gtk-doc.make
	autoreconf --force --install --symlink
	touch $@

build-deb/config.status: .stamp-autoreconf
	[ -d build-deb ] || mkdir build-deb
	cd build-deb/ && \
	../configure $(CONFFLAGS_deb)

build-udeb/config.status: CFLAGS += -Os -fomit-frame-pointer
build-udeb/config.status: .stamp-autoreconf
	[ -d build-udeb ] || mkdir build-udeb
	cd build-udeb/ && \
	../configure $(CONFFLAGS_udeb)

build: build-arch build-indep
build-arch: .stamp-build $(BUILD_UDEB_TARGET)
build-indep: .stamp-build $(BUILD_UDEB_TARGET)
.stamp-build: build-deb/config.status
	dh_testdir
	cd build-deb/ && \
	$(MAKE) all USB_DATABASE=/usr/share/misc/usb.ids
	touch $@

build-udeb: .stamp-build-udeb
.stamp-build-udeb: build-udeb/config.status
	dh_testdir
	cd build-udeb/ && \
	$(MAKE) all
	touch $@

binary-arch: build checkroot
	dh_testdir
	dh_prep
	dh_installdirs

	cd build-deb/ && \
	$(MAKE) install DESTDIR=$D

	dh_movefiles --sourcedir=debian/udev
	cd $D && rm -r usr/lib/pkgconfig/ usr/include/ usr/share/gtk-doc/ \
		usr/share/gir-1.0/ usr/lib/girepository-1.0/ \
		usr/lib/*.la
	cd $D && rmdir lib/$(DEB_HOST_MULTIARCH) usr/lib/$(DEB_HOST_MULTIARCH)

	install --mode=755 extra/*.agent extra/create_static_nodes \
		extra/write_dev_root_rule \
		$D/lib/udev/
	cp extra/hotplug.functions $D/lib/udev/

	mv $D/lib/udev/udevd $D/sbin/
	cd $D/lib/udev/rules.d/ && rm 61-persistent-storage-edd.rules
	cp rules/debian/*.rules $D/lib/udev/rules.d/
	cp extra/links.conf $D/etc/udev/

	install --mode=644 extra/fbdev-blacklist.conf \
		$D/etc/modprobe.d/

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

ifdef BUILD_UDEB
	grep '^[[:alnum:]]' extra/links.conf > $(DU)/etc/udev/links.conf
	grep '^[[:alnum:]]' udev.conf > $(DU)/etc/udev/udev.conf

	cp rules/debian/*-generator.rules rules/debian/80-drivers.rules \
		rules/debian/50-udev-default.rules \
		rules/rules.d/60-persistent-input.rules \
		extras/cdrom_id/60-cdrom_id.rules $(DU)/lib/udev/rules.d/
	sed -r -e '/Enhanced Disk Device/,/^$$/d' \
		< rules/debian/60-persistent-storage.rules \
		> $(DU)/lib/udev/rules.d/60-persistent-storage.rules
	echo 'MODE="0666"' > $(DU)/lib/udev/rules.d/91-permissions.rules

	install --mode=755 $(addprefix build-udeb/udev/, udevd udevadm) \
		$(DU)/sbin/
	install --mode=755 $(addprefix build-udeb/extras/, \
		ata_id/ata_id cdrom_id/cdrom_id scsi_id/scsi_id ) \
		extras/rule_generator/write_*_rules extra/firmware.agent \
		$(DU)/lib/udev/
	install --mode=644 extras/rule_generator/rule_generator.functions \
		extra/hotplug.functions \
		$(DU)/lib/udev/
	install --mode=755 extra/udev.startup \
		$(DU)/lib/debian-installer/start-udev
	install --mode=755 extra/udev.base-installer \
		$(DU)/usr/lib/base-installer.d/05udev

	cp -a build-udeb/libudev/.libs/libudev.so.0* $(DGU)/lib/
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),sparc sparc64))
	install --mode=755 build-udeb/extras/vio_type/vio_type $(DGU)/lib/udev/
endif
endif

ifneq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),sparc sparc64))
	rm $D/lib/udev/vio_type $D/usr/share/man/man8/vio_type.8
endif

	# workaround for #555111
	cp -a libudev/docs/html/* \
		$(CURDIR)/debian/libudev-dev/usr/share/gtk-doc/html/libudev/
	cp -a extras/gudev/docs/html/* \
		$(CURDIR)/debian/libgudev-1.0-dev/usr/share/gtk-doc/html/gudev/

	dh_link
	mv $D/usr/share/doc/udev $D/usr/share/doc/libudev0
	dh_installdocs -pudev -plibudev-dev --link-doc=libudev0
	dh_installdocs -pgir1.2-gudev-1.0 -plibgudev-1.0-dev --link-doc=libgudev-1.0-0
	dh_installdocs README NEWS TODO extra/udev.vim
	rm $D/usr/share/doc/libudev0/copyright
	dh_installchangelogs ChangeLog
	dh_installexamples
	dh_installdebconf
	dh_installinit --no-start --update-rcd-params='start 03 S .'
	dh_installinit --no-start --update-rcd-params='start 36 S .' \
		--name=udev-mtab
	dh_installinit --name=udev-finish --no-start -- start 37 S .
	dh_installinit --name=udevtrigger --no-start
	dh_installinit --name=udevmonitor --no-start
	dh_installinit --name=udev-fallback-graphics --no-start

	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs --add-udeb=udev-gtk-udeb -- -c4
	dh_shlibdeps --exclude=/lib/udev/udev-acl -- --warnings=7
	dh_girepository
	dh_installdeb
	dh_gencontrol $(NO_PACKAGE)
	dh_md5sums
	dh_builddeb $(NO_PACKAGE)


binary:	binary-arch

checkroot:
	test root = "`whoami`"

get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST)))../ && \
	uscan --rename --force-download --watchfile debian/watch --destdir $(CURDIR)

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