#!/usr/bin/make -f
# Copyright © 2001-2005 Mario Jou/3en <joussen@debian.org>
# Copyright © 2005-2008 Martin F. Krafft <madduck@debian.org>
# Distributable under the terms of the GNU GPL version 2.
#

#export DH_VERBOSE=1

CXFLAGS = -ggdb
CXFLAGS_UDEB := $(CXFLAGS) -fomit-frame-pointer
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CXFLAGS += -O0
    CXFLAGS_UDEB += -O0
else
    CXFLAGS += -O2
    CXFLAGS_UDEB += -Os
endif

.PHONY: configure
configure:

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE) -f debian/rules mdadm.udeb DEBIAN=yes
	$(MAKE) clean
	$(MAKE) -f debian/rules mdadm DEBIAN=yes
	touch $@

mdadm.udeb: FLAGS = CXFLAGS="$(CXFLAGS_UDEB)" CONFFILE=/tmp/mdadm.conf
mdadm.udeb: configure
	dh_testdir
	$(MAKE) $(FLAGS) all
	mv mdadm mdadm.udeb
	mv mdmon mdmon.udeb
.PHONY: mdadm.udeb mdmon.udeb

mdadm: FLAGS = CXFLAGS="$(CXFLAGS)" CONFFILE=/etc/mdadm/mdadm.conf CONFFILE2=/etc/mdadm.conf
mdadm: configure
	dh_testdir
	$(MAKE) $(FLAGS) all
.PHONY: mdadm

INTERPOLATED_FILES = debian/bugscript debian/mkconf

clean:
	dh_testdir
	rm -f $(INTERPOLATED_FILES)
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) clean
	rm -f mdadm.udeb mdmon.udeb mdadm debian/mdadm-startall.8
	dh_clean
	debconf-updatepo

debian/%: VERSION=$(shell dpkg-parsechangelog | sed -ne 's,^Version: ,,p')
debian/%: debian/%.in
	sed -re 's,%VERSION%,$(VERSION),g' < $< > $@

install: DESTDIR=$(CURDIR)/debian/mdadm
install: DESTDIR_UDEB=$(DESTDIR)-udeb
install: build $(INTERPOLATED_FILES)
	dh_testdir
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(DESTDIR)

	install -Dm0755 debian/initramfs/hook \
		$(DESTDIR)/usr/share/initramfs-tools/hooks/mdadm

	install -Dm0755 debian/initramfs/script.local-block \
		$(DESTDIR)/usr/share/initramfs-tools/scripts/local-block/mdadm

	install -Dm0755 debian/initramfs/script.local-bottom \
		$(DESTDIR)/usr/share/initramfs-tools/scripts/local-bottom/mdadm

	install -Dm0755 debian/mdadm.shutdown \
		$(DESTDIR)/lib/systemd/system-shutdown/mdadm.shutdown

	install -m0755 debian/mkconf $(DESTDIR)/usr/share/mdadm
	install -m0755 debian/checkarray $(DESTDIR)/usr/share/mdadm
	install -m0755 debian/bugscript $(DESTDIR)/usr/share/bug/mdadm/script
	install -m0644 debian/source_mdadm.py $(DESTDIR)/usr/share/apport/package-hooks/
	install -m0755 mdadm.udeb $(DESTDIR_UDEB)/sbin/mdadm
	install -m0755 mdmon.udeb $(DESTDIR_UDEB)/sbin/mdmon
	install -m0644 udev-md-raid-arrays.rules $(DESTDIR_UDEB)/lib/udev/rules.d/63-md-raid-arrays.rules
	install -m0644 udev-md-raid-assembly.rules $(DESTDIR_UDEB)/lib/udev/rules.d/64-md-raid-assembly.rules
	install -Dm0644 debian/mdadm-shutdown.service $(DESTDIR)/lib/systemd/system/mdadm-shutdown.service

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_lintian
	dh_installdebconf
	dh_installdocs
	dh_installexamples debian/mdadd.sh
	dh_installinit -- defaults 25
	dh_installinit --init-script=mdadm-waitidle --no-start -- stop 98 0 6 .
	dh_installman
	dh_installcron
	dh_installchangelogs ChangeLog
	dh_installlogcheck
	dh_link
	dh_strip
	dh_compress -Xmdadd.sh
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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