#!/usr/bin/make -f

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE) ac_cv_func_malloc_0_nonnull=yes
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir
	./autogen.sh
	./configure --prefix=/usr --with-plugindir=/usr/lib/$(DEB_HOST_MULTIARCH)/ts0 \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--sysconfdir=/etc $(CROSS)
	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	./autogen-clean.sh
	dh_clean depcomp tslib-0.0.pc aclocal.m4 INSTALL m4/internal/*
	rm -f build-stamp

binary-indep: build

binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean
	$(MAKE) DESTDIR=$(shell pwd)/debian/tmp install
	dh_installdirs
	dh_install --sourcedir=debian/tmp --list-missing
ifeq (,$(findstring nodocs,$(DEB_BUILD_OPTIONS)))
	dh_installdocs AUTHORS README
	dh_installchangelogs ChangeLog
	dh_installman -plibts-bin debian/*.1
	dh_installman -ptsconf debian/*.5
endif
	dh_compress
	dh_fixperms
	dh_strip --dbg-package=libts-0.0-0-dbg
	dh_makeshlibs
	dh_shlibdeps
	dh_gencontrol
	dh_installdeb
	dh_md5sums
	dh_builddeb

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