#!/usr/bin/make -f
#export DH_VERBOSE=1

DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

DEB_BUILD_MAINT_OPTIONS=hardening=-pie reproducible=-fixfilepath,+fixdebugpath

# Test failures occured with LTO on these architectures
ifneq ($(findstring $(DEB_HOST_ARCH), arm64 ppc64el s390x),)
	DEB_BUILD_MAINT_OPTIONS += optimize=-lto
endif
export DEB_BUILD_MAINT_OPTIONS

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

ifeq ($(DEB_HOST_ARCH),amd64)
	DEBIAN_OPTIMIZE := amd64
endif

ifeq ($(DEB_HOST_ARCH),i386)
	DEBIAN_OPTIMIZE := x86
endif

config.status: configure
	dh_testdir

	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .

	./configure \
	  --host=$(DEB_HOST_GNU_TYPE) \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr \
	  --mandir=\$${prefix}/share/man \
	  --infodir=\$${prefix}/share/info \
	  --bindir=\$${prefix}/bin

build: build-arch
build-arch: build-stamp
build-indep:

build-stamp: config.status
	dh_testdir
	$(MAKE) $(DEBIAN_OPTIMIZE)

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) tests
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) clean
	rm -f config.sub config.guess
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) install prefix=$(CURDIR)/debian/aespipe/usr

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples bz2aespipe
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
