#!/usr/bin/make -f

INITRAMFS=$(CURDIR)/debian/multipath-tools-boot/usr/share/initramfs-tools/

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
        INSTALL_PROGRAM = install -s
else
        INSTALL_PROGRAM = install
endif

# Work around misp(el) linker bug:
# http://sources.redhat.com/bugzilla/show_bug.cgi?id=10144
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
ifneq (,$(findstring mips,$(DEB_HOST_ARCH)))
  OPTFLAGS = "OPTFLAGS=-pipe -Wall -Wunused -Wstrict-prototypes"
endif

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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DISABLE_SYSTEMD = 1

build: build-arch build-indep

build-arch: build-multipath-udeb-stamp build-stamp
build-indep: build-stamp

build-stamp:
	dh_testdir
	
	[ ! -f kpartx/kpartx.rules ] || cp kpartx/kpartx.rules debian/kpartx.udev

	$(MAKE) $(OPTFLAGS) LIB=lib

	touch $@

# multipath-udeb: build separately; don't reference dynamic libgcc at runtime (#779579)
build-multipath-udeb-stamp:
	dh_testdir

	DEB_CFLAGS_MAINT_APPEND="-static-libgcc" CFLAGS_DISABLE_FEXCEPTIONS=1 $(MAKE) $(OPTFLAGS) LIB=lib
	
	# store files for install target
	mkdir -p $(CURDIR)/debian/tmp-multipath-udeb/sbin
	$(MAKE) install INSTALL_PROGRAM=$(INSTALL_PROGRAM) DESTDIR=$(CURDIR)/debian/tmp-multipath-udeb LIB=lib

	$(MAKE) clean

	touch $@

clean-tree:
	$(MAKE) clean

clean: clean-tree
	dh_testdir
	rm -f build-stamp build-multipath-udeb-stamp
	dh_prep
	debconf-updatepo
	rm -rf debian/*.debhelper.log
	rm -rf debian/tmp-multipath-udeb

install:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	mkdir -p $(CURDIR)/debian/tmp/sbin
	$(MAKE) install INSTALL_PROGRAM=$(INSTALL_PROGRAM) DESTDIR=$(CURDIR)/debian/tmp LIB=lib
	install -m 755 debian/dmsetup_env $(CURDIR)/debian/tmp/lib/udev/
	dh_install -X.rules --fail-missing -Nmultipath-udeb --sourcedir=$(CURDIR)/debian/tmp
	dh_install -X.rules --fail-missing -pmultipath-udeb --sourcedir=$(CURDIR)/debian/tmp-multipath-udeb
	# initramfs stuff:
	install -D -m 755 debian/initramfs/hooks $(INITRAMFS)/hooks/multipath
	install -D -m 755 debian/initramfs/kpartx.hook $(CURDIR)/debian/kpartx-boot/usr/share/initramfs-tools/hooks/kpartx
	install -D -m 755 debian/initramfs/local-top \
		$(INITRAMFS)/scripts/local-top/multipath
	install -D -m 755 debian/initramfs/local-premount \
		$(INITRAMFS)/scripts/local-premount/multipath
	install -D -m 755 debian/initramfs/init-top \
		$(INITRAMFS)/scripts/init-top/multipath
	# reportbug:
	for pkg in "multipath-tools" "multipath-tools-boot"; do \
	    install -D -m 755 debian/reportbug/script debian/$${pkg}/usr/share/bug/$${pkg}/script; \
	done

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_lintian
	dh_installinit -pmultipath-tools
	dh_installinit -pmultipath-tools --name=multipath-tools-boot --no-start -- start 21 S .
	dh_installudev -pkpartx --priority=95
	dh_installudev -pmultipath-tools --name=multipath --priority=95
	dh_installdebconf -pmultipath-tools-boot
	dh_installman
	dh_makeshlibs --add-udeb=multipath-udeb
	dh_link
	dh_strip --dbg-package=multipath-tools-dbg
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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