#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/vendor.mk

export CROSS_COMPILE=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)-
export LDFLAGS = $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get LDFLAGS)
export CXFLAGS = $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get CFLAGS) \
	  $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --get CPPFLAGS)

export DEBIAN="yes"
export EXTRAVERSION=$(DEB_VENDOR) $(DEB_VERSION)

# these are reversed in the Makefile
export CONFFILE="/etc/mdadm/mdadm.conf"
export CONFFILE2="/etc/mdadm.conf"

# No udev.pc / systemd.pc with libudev-compat-only builds: Makefile :=
# assignments ignore a plain export, so pass these on the make command line.
UDEVDIR = /usr/lib/udev
SYSTEMD_DIR = /usr/lib/systemd/system

%:
	dh $@ --with runit,openrc

execute_after_dh_auto_clean:
	# remove generated files
	for file in $$(find . -type f -name "*.in"); \
	do \
		rm -f "$$(dirname "$${file}")"/"$$(basename "$${file}" .in)"; \
	done

override_dh_auto_install:
	dh_auto_install -- UDEVDIR=$(UDEVDIR)

execute_before_dh_install:
	$(MAKE) install-systemd DESTDIR=$(CURDIR)/debian/tmp SYSTEMD_DIR=$(SYSTEMD_DIR)

execute_after_dh_install:
	mkdir -p $(CURDIR)/debian/mdadm/etc/mdadm
	chmod +x $(CURDIR)/debian/mdadm/usr/share/mdadm/mdcheck

override_dh_installsystemd:
	dh_installsystemd -pmdadm mdcheck_continue.timer mdcheck_start.timer mdmonitor-oneshot.timer

override_dh_installinit:
	dh_installinit --no-scripts --init-script=mdadm-waitidle --no-start -- stop 98 0 6 .
	dh_installinit --no-scripts -- defaults 25
