#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
BUILT_USING := $(shell dpkg-query -f'$${source:Package} (= $${source:Version})' -W dietlibc-dev)
DIET_LIBDIR := $(shell diet -L gcc)

%:
	dh $@

HAVE_DIETLIBC=no
ifeq ($(shell dpkg -s dietlibc-dev | grep -o installed), installed)
	HAVE_DIETLIBC=yes
endif

override_dh_auto_configure:
ifeq ($(HAVE_DIETLIBC),yes)
	dh_auto_configure -B diet-build --  \
	    --libdir $(DIET_LIBDIR) \
	    --disable-shared \
	    --enable-static \
	    CC='diet gcc'
endif
	dh_auto_configure -B glibc-build -- --enable-libgdbm-compat

override_dh_auto_build:
ifeq ($(HAVE_DIETLIBC),yes)
	dh_auto_build -B diet-build
endif
	dh_auto_build -B glibc-build

override_dh_auto_install:
ifeq ($(HAVE_DIETLIBC),yes)
	dh_auto_install -B diet-build
endif
	dh_auto_install -B glibc-build

ifeq ($(HAVE_DIETLIBC),yes)
override_dh_gencontrol:
	dh_gencontrol -- -VBuilt-Using="$(BUILT_USING)" -Vlibgdbm-dietlibc:Provides=libgdbm-dietlibc-dev
endif

override_dh_install:
	dh_install
ifeq ($(HAVE_DIETLIBC),yes)
	dh_install -plibgdbm-dev /usr/lib/*/diet/*/libgdbm.a
endif
