#!/usr/bin/make -f

GLIBC_VERSION = 2.15
GLIBC_NEXT = 2.16
export ACLOCAL=aclocal-1.15
export AUTOMAKE=automake-1.15

DOPACKAGES := $(shell dh_listpackages)
BUILD_TARGETS :=

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

BUILD_UDEB :=
NO_PACKAGE := --no-package=libcrypt1-udeb --no-package=libxcrypt-source

D   := $(CURDIR)/debian/libcrypt1
DD  := $(CURDIR)/debian/libcrypt-dev
D2  := $(CURDIR)/debian/libcrypt2
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)

UPSTREAM_VERSION := $(shell dpkg-parsechangelog | sed -e 's/^Version: *//p' | sed -e 's/.*://' -e 's/[+-].*//')

SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog | sed -e 's/^Timestamp: *//p')

##############################################################################
%:
	dh $@ --with autoreconf $(NO_PACKAGE)

override_dh_clean:
	rm -rf build-deb* build-udeb*
	rm -f debian/libcrypt1.preinst
	rm -f debian/libcrypt1.postrm
	dh_clean debian/copyright

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

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)
	sh debian/patch_libtool build-udeb

override_dh_auto_configure:
	set -e; for maintscript in preinst postrm; do \
		sed \
			-e 's,@MULTIARCH@,${DEB_HOST_MULTIARCH},g' \
			-e 's,@GLIBCVERSION@,${GLIBC_VERSION},g' \
			< debian/libcrypt1.$$maintscript.in \
			> debian/libcrypt1.$$maintscript; \
		chmod 0755 debian/libcrypt1.$$maintscript; \
	done

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

	# Move the shared library back to /lib/ because this is where the
	# libc6 package used to install it (see #953562 for details).
	mkdir -p $D/lib/$(DEB_HOST_MULTIARCH)
	mv $D/usr/lib/$(DEB_HOST_MULTIARCH)/libcrypt.so.1* $D/lib/$(DEB_HOST_MULTIARCH)/
	ln -sf /lib/$(DEB_HOST_MULTIARCH)/libcrypt.so.1 $D/usr/lib/$(DEB_HOST_MULTIARCH)/libcrypt.so
ifeq ($(BUILD_DEV_VER), 1)
	dh_movefiles -plibcrypt-dev --sourcedir=debian/libcrypt1/
else
	cd $D && echo $(shell sed -re 's#^/##' $(CURDIR)/debian/libcrypt-dev.files) | xargs echo rm -rf
endif
	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)

ifeq ($(BUILD_DEV_VER), 2)
	dh_movefiles -plibcrypt-dev --sourcedir=debian/libcrypt2/
else
	cd $(D2) && echo $(shell sed -re 's#^/##' $(CURDIR)/debian/libcrypt-dev.files) | xargs echo rm -rf
endif
	rm $(D2)/usr/lib/*/*.la
	rmdir --parents --ignore-fail-on-non-empty \
		$$(find $(D2) -type d -empty) $$(find $(DD) -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
	dh_installdocs -plibcrypt-dev --link-doc=libcrypt$(BUILD_DEV_VER)
ifdef BUILD_DEB1
	dh_installdocs -plibcrypt1
endif
ifdef BUILD_DEB2
	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

override_dh_gencontrol:
	dh_gencontrol $@ -- -VLIBPKG=libcrypt$(BUILD_DEV_VER) -VGLIBCVERSION=${GLIBC_VERSION} -VGLIBCNEXT=${GLIBC_NEXT}

