#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DESTDIR=$(shell pwd)/debian/check

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

export AUTOMAKE = automake-1.11
export ACLOCAL = aclocal-1.11

configure-pre-stamp:
	dh_testdir
	dh_autoreconf
	touch $@

configure-stamp: configure-pre-stamp
	dh_testdir
	rm -rf build
	mkdir -p build
	cd build && ../configure --prefix=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--infodir=/usr/share/info --enable-plain-docdir \
		$(shell dpkg-buildflags --export=cmdline)
	touch $@
	touch configure-stamp

configure-stamp-pic: configure-pre-stamp
	dh_testdir
	rm -rf build-pic
	mkdir -p build-pic
	cd build-pic && ../configure --prefix=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--infodir=/usr/share/info \
		--enable-plain-docdir \
		$(shell DEB_CFLAGS_MAINT_APPEND='-fPIC' dpkg-buildflags --export=cmdline)
	touch $@

build: build-pic configure-stamp build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C build
	touch build-stamp

build-pic: build-stamp-pic
build-stamp-pic: configure-stamp-pic
	#remove check.info so it will be regenerated
	rm -f build-pic/doc/check.info
	dh_testdir
	$(MAKE) -C build-pic
	cp build-pic/src/.libs/libcheck.a build-pic/src/libcheck_pic.a
	#run the testsuite
	dh_auto_test
	#remove dynamic content for Multi-Arch:same (see #718200)
	sed -i -e "1d" build-pic/doc/check.info
	touch build-stamp-pic

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp build-stamp-pic configure-stamp-pic configure-stamp-pic
	rm -rf build build-pic
	dh_autoreconf_clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs

	$(MAKE) -C build DESTDIR=$(DESTDIR) install
	# fix the installation
	rm -f debian/check/usr/share/doc/check/COPYING*

install-pic: build-pic
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	mkdir -p debian/check/usr/lib/$(DEB_HOST_MULTIARCH)
	cp build-pic/src/libcheck_pic.a debian/check/usr/lib/$(DEB_HOST_MULTIARCH)/

binary-arch: install-pic install
	dh_testdir
	dh_testroot
	dh_installdocs -XTODO
	dh_installinfo build-pic/doc/check.info
	dh_installexamples
	dh_installchangelogs ChangeLog
	dh_compress -XMakefile
	# fix the setup
	sed -i '/\/\* generated using gnu compiler gcc.*/d' debian/check/usr/include/check_stdint.h
	rm -f debian/check/usr/share/doc/check/ChangeLog.gz
	rm -f debian/check/usr/lib/$(DEB_HOST_MULTIARCH)/libcheck.so.*
	rm -f debian/check/usr/lib/$(DEB_HOST_MULTIARCH)/libcheck.so
	rm -f debian/check/usr/lib/$(DEB_HOST_MULTIARCH)/libcheck.la
	rm -f debian/check/usr/share/info/dir.gz
	rm -rf debian/check/usr/share/doc/check/example
	dh_link
	dh_strip
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:
	# nothing to do

build-arch: build
build-indep: build

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