#!/usr/bin/make -f
# There are some problems with sanitize=+all and getgroups(2), as checked in
# runit-2.1.2/src/chkshgrp.c.
# -- Dmitry Bogatov <KAction@gnu.org>  Sun, 24 Dec 2017 00:13:54 +0300
export DEB_BUILD_MAINT_OPTIONS = hardening=+all qa=+canary
include /usr/share/dpkg/default.mk

%:
	dh $@ --sourcedirectory=runit-$(DEB_VERSION_UPSTREAM)/src \
	      --with bash_completion,runit,sysuser

# Do these as part of dh_build so we don't proliferate different canaries etc.
.PHONY: write_runit_build_flags restore_runit_build_flags
write_runit_build_flags:
	cp runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc \
	      runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc.bk
	cp runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld \
	      runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld.bk
	echo $(COMPILE.c) > runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc
	echo $(LINK.o) > runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld

restore_runit_build_flags:
ifneq ("$(wildcard runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc.bk)","")
	mv -f runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc.bk \
	    runit-$(DEB_VERSION_UPSTREAM)/src/conf-cc
endif
ifneq ("$(wildcard runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld.bk)","")
	mv -f runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld.bk \
	    runit-$(DEB_VERSION_UPSTREAM)/src/conf-ld
endif

override_dh_auto_build: write_runit_build_flags
	$(MAKE) -C debian/contrib all
	patch   -o ./debian/contrib/i-s-h/invoke-rc.d ./debian/contrib/i-s-h/invoke-rc.d-*.in \
		./debian/contrib/i-s-h/invoke-rc.d-runit.patch
	patch   -o ./debian/contrib/i-s-h/service ./debian/contrib/i-s-h/service-*.in \
		./debian/contrib/i-s-h/service-runit.patch
	dh_auto_build -- -Oline

override_dh_installsystemd:
	dh_installsystemd --name runit --no-stop-on-upgrade

override_dh_installman-arch:
	dh_installman
	mv debian/runit/usr/share/man/man8/shutdown.8 \
	   debian/runit/usr/share/man/man8/runit-shutdown.8
	mv debian/runit/usr/share/man/man8/runlevel.8 \
	   debian/runit/usr/share/man/man8/runit-runlevel.8

override_dh_runit:
	./debian/sv/getty.in/install
	dh_runit

override_dh_installchangelogs:
	dh_installchangelogs
	dh_installchangelogs -p runit -p runit-init \
	    runit-$(DEB_VERSION_UPSTREAM)/package/CHANGES

override_dh_clean: restore_runit_build_flags
	dh_clean
	$(MAKE) -C debian/contrib clean

autopkgtest-image:
	autopkgtest-build-qemu unstable ../autopkgtest.img

autopkgtest:
#	$(info Running autopkgtest with options to avoid entropy starvation)
#	$(info For more information see #931658)
ifeq ($(wildcard ../autopkgtest.img),)
	$(error Qemu image ../autopkgtest.img not found. Run autopkgtest-image rule)
endif
ifeq ($(wildcard ../runit_$(DEB_VERSION)_$(DEB_BUILD_ARCH).deb),)
	$(error No binary packages in parent directory. Build package)
endif
	autopkgtest                                            \
	    ../runit-init_$(DEB_VERSION)_$(DEB_BUILD_ARCH).deb               \
	    ../runit_$(DEB_VERSION)_$(DEB_BUILD_ARCH).deb      \
	    ../getty-run_$(DEB_VERSION)_all.deb                               \
	    .                                                  \
	    -- qemu                                            \
	    ../autopkgtest.img
#	    --show-boot   --timeout-reboot=180
#	    --qemu-options='-object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-pci,rng=rng0' \
	    ../autopkgtest.img

.PHONY: autopkgtest autopkgtest-image