#!/usr/bin/make -f

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_TARGET_MULTIARCH ?= $(shell dpkg-architecture -qDEB_TARGET_MULTIARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_MULTIARCH DEB_TARGET_MULTIARCH

export CPPFLAGS += -DHAVE_GETTIMEOFDAY

%:
	dh $@ --parallel --with autoreconf

override_dh_auto_clean:
	dh_auto_clean --builddirectory build-deb
	rm -fr $(CURDIR)/debian/tmp-deb
	dh_auto_clean --builddirectory build-udeb
	rm -fr $(CURDIR)/debian/tmp-udeb
	dh_auto_clean --builddirectory build-doc
	rm -fr $(CURDIR)/debian/tmp-doc


#
# Architecture targets
#
override_dh_auto_configure-arch:
	dh_auto_configure --builddirectory build-deb -- --enable-tests-build
#	CFLAGS="$(CFLAGS) -Os" dh_auto_configure --builddirectory build-udeb -- --libdir=\$${prefix}/lib/

override_dh_auto_build-arch:
	dh_auto_build --builddirectory build-deb
#	dh_auto_build --builddirectory build-udeb

override_dh_auto_install-arch:
	dh_auto_install --builddirectory build-deb --destdir=$(CURDIR)/debian/tmp-deb
	# Move the library to /lib
	mkdir -p $(CURDIR)/debian/tmp-deb/lib/$(DEB_HOST_MULTIARCH)/
	mv $(CURDIR)/debian/tmp-deb/usr/lib/$(DEB_HOST_MULTIARCH)/libusb-1.0.so.* \
	   $(CURDIR)/debian/tmp-deb/lib/$(DEB_HOST_MULTIARCH)/
	ln -sf /lib/$(DEB_HOST_MULTIARCH)/libusb-1.0.so.0.1.0 \
	       $(CURDIR)/debian/tmp-deb/usr/lib/$(DEB_HOST_MULTIARCH)/libusb-1.0.so
#	dh_auto_install --builddirectory build-udeb --destdir=$(CURDIR)/debian/tmp-udeb

override_dh_install-arch:
	dh_install -s -Nlibusb-1.0-0-udeb --list-missing --sourcedir=debian/tmp-deb
#	dh_install -plibusb-1.0-0-udeb --sourcedir=debian/tmp-udeb

override_dh_strip-arch:
	dh_strip -plibusb-1.0-0 --dbgsym-migration='libusb-1.0-0-dbg (<< 2:1.0.21~)'
	dh_strip -a --remaining-packages

override_dh_makeshlibs-arch:
	dh_makeshlibs -plibusb-1.0-0
#	dh_makeshlibs -plibusb-1.0-0 --add-udeb="libusb-1.0-0-udeb"


#
# Architecture independent targets
#
override_dh_auto_configure-indep:
	dh_auto_configure --builddirectory build-doc

override_dh_auto_build-indep:
	$(MAKE) -C $(CURDIR)/build-doc/doc docs

