#!/usr/bin/make -f

DOPACKAGES := $(shell dh_listpackages)
BUILD_TARGETS :=

ifneq ($(filter libcrypt1,$(DOPACKAGES)),)
BUILD_DEB1 := 1
BUILD_TARGETS += build-deb1/.stamp
endif
ifneq ($(filter libcrypt2,$(DOPACKAGES)),)
BUILD_DEB2 := 1
BUILD_TARGETS += build-deb2/.stamp
endif
ifneq ($(filter libcrypt1-udeb,$(DOPACKAGES)),)
BUILD_UDEB := 1
BUILD_TARGETS += build-udeb/.stamp
endif

ifeq ($(filter noudeb, $(DEB_BUILD_PROFILES)), noudeb)
BUILD_UDEB :=
NO_PACKAGE := --no-package=libcrypt1-udeb
endif

D   := $(CURDIR)/debian/libcrypt1
DD  := $(CURDIR)/debian/libcrypt1-dev
D2  := $(CURDIR)/debian/libcrypt2
DD2 := $(CURDIR)/debian/libcrypt2-dev
DU  := $(CURDIR)/debian/libcrypt1-udeb

CONFFLAGS = --disable-werror --prefix=/usr \
  --disable-xcrypt-compat-files --enable-obsolete-api=glibc
CONFFLAGS_deb  = $(CONFFLAGS) \
  $(shell DEB_BUILD_MAINT_OPTIONS="hardening=+bindnow" \
    dpkg-buildflags --export=configure || true) \
  --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)
CONFFLAGS_udeb = $(CONFFLAGS) \
  $(subst -O2,-Os -fomit-frame-pointer,$(shell DEB_BUILD_MAINT_OPTIONS="hardening=-all" \
    dpkg-buildflags --export=configure || true)) \
  --libdir=/usr/lib

##############################################################################
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

##############################################################################
%:
	dh $@ --reload-all-buildenv-variables $(NO_PACKAGE)

override_dh_clean:
	rm -rf build-*
	dh_clean debian/copyright

build-deb1/config.status:
	dh_auto_configure --builddir=build-deb1 -- $(CONFFLAGS_deb)

build-deb2/config.status:
	dh_auto_configure --builddir=build-deb2 -- $(subst api=glibc,api=no,$(CONFFLAGS_deb))

build-udeb/config.status:
	dh_auto_configure --builddir=build-udeb -- $(CONFFLAGS_udeb)

override_dh_auto_configure:

build-deb1/.stamp: build-deb1/config.status
	dh_auto_build --builddir=build-deb1
	touch $@

build-deb2/.stamp: build-deb2/config.status
	dh_auto_build --builddir=build-deb2
	touch $@

build-udeb/.stamp: build-udeb/config.status
	dh_auto_build --builddir=build-udeb
	touch $@

override_dh_auto_build: $(BUILD_TARGETS)

override_dh_auto_test:
ifdef BUILD_DEB1
	dh_auto_test --builddir=build-deb1
else
ifdef BUILD_DEB2
	dh_auto_test --builddir=build-deb2
endif
endif

override_dh_auto_install:
ifdef BUILD_DEB1
	cd build-deb1/ && \
	$(MAKE) install DESTDIR=$D

	dh_movefiles -plibcrypt1-dev --sourcedir=debian/libcrypt1/
	rm $D/usr/lib/*/*.la
	rmdir --parents --ignore-fail-on-non-empty \
		$$(find $D -type d -empty) $$(find $(DD) -type d -empty)
endif

ifdef BUILD_DEB2
	cd build-deb2/ && \
	$(MAKE) install DESTDIR=$(D2)

	dh_movefiles -plibcrypt2-dev --sourcedir=debian/libcrypt2/
	rm $(D2)/usr/lib/*/*.la
	rmdir --parents --ignore-fail-on-non-empty \
		$$(find $(D2) -type d -empty) $$(find $(DD2) -type d -empty)
endif

ifdef BUILD_UDEB
	cd build-udeb/ && \
	$(MAKE) install-exec DESTDIR=$(DU)
	rm $(DU)/usr/lib/*.a $(DU)/usr/lib/*.la $(DU)/usr/lib/*.so
endif

override_dh_installdocs:
	cat debian/copyright.in LICENSING > debian/copyright
ifdef BUILD_DEB1
	dh_installdocs -plibcrypt1-dev --link-doc=libcrypt1
	dh_installdocs -plibcrypt1
endif
ifdef BUILD_DEB2
	dh_installdocs -plibcrypt2-dev --link-doc=libcrypt2
	dh_installdocs -plibcrypt2
endif

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog NEWS

override_dh_makeshlibs:
ifdef BUILD_DEB1
	dh_makeshlibs -plibcrypt1 -- -c4
endif
ifdef BUILD_DEB2
	dh_makeshlibs -plibcrypt2 -- -c4
endif

override_dh_shlibdeps:
	dh_shlibdeps -- --warnings=7

