#!/usr/bin/make -f

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

%:
	dh $@ --with bash-completion

# On Debian we can use either busybox or busybox-static, but on Ubuntu
# and derivatives only busybox-initramfs will work.
BUSYBOX_PACKAGES := $(shell if dpkg-vendor --derives-from ubuntu; then echo busybox-initramfs; else echo busybox busybox-static; fi)

override_dh_gencontrol:
	echo >> debian/initramfs-tools-core.substvars "busybox:Depends=$(wordlist 2,100,$(BUSYBOX_PACKAGES:%=| %))"
	dh_gencontrol

override_dh_install:
	install -d debian/initramfs-tools-core/usr/sbin
	sed -e 's,@BUSYBOX_PACKAGES@,$(wordlist 2,100,$(BUSYBOX_PACKAGES:%=or %)),;s,@VERSION@,$(DEB_VERSION),' \
		mkinitramfs > debian/initramfs-tools-core/usr/sbin/mkinitramfs
	chmod 755 debian/initramfs-tools-core/usr/sbin/mkinitramfs
	dh_install

override_dh_installinitramfs:
	@:

DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CROSS :=
else
CROSS := CC=$(DEB_HOST_GNU_TYPE)-gcc PKG_CONFIG=$(DEB_HOST_GNU_TYPE)-pkg-config
endif

override_dh_auto_build:
	make -C src $(CROSS)

override_dh_auto_clean:
	make -C src clean

DEB_HOST_ARCH     := $(shell dpkg-architecture -qDEB_HOST_ARCH)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    ifeq (,$(filter i386,$(DEB_HOST_ARCH)))
	shellcheck hook-functions $$(find hooks scripts -type f) $$({ find . -maxdepth 1 -type f -executable; find debian -maxdepth 1 -type f; find debian/tests docs kernel tests -type f; } | xargs grep -l '^#!/bin/sh')
    endif
	tests/functions_test
	./tests/run-tests
endif
