#!/usr/bin/make -f

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

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

SOURCE_DATE ?= $(shell dpkg-parsechangelog --show-field Date)
SOURCE_DATE_UTC ?= $(shell LC_ALL=C date -u -d "$(SOURCE_DATE)")
CHANGELOG_DATE ?= $(shell LC_ALL=C date -u -d "$(SOURCE_DATE)" +"%d %B %Y")

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

UPSTREAM_PACKAGE_VERSION ?= $(shell dpkg-parsechangelog --show-field Version | sed 's/-[^-]*$$//')

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=host
	dh_auto_clean --sourcedirectory=firmware
	$(RM) debian/man/*.1
	$(RM) -r ubertooth-firmware-source
	$(RM) ubertooth-firmware-source.tar.gz
	$(RM) debian/ubertooth.pc

override_dh_auto_configure:
	# Creating ubertooth-firmare-source.tar.gz:
	cp -rf firmware ubertooth-firmware-source
	cd ubertooth-firmware-source ; patch -R -Np2 < ../debian/patches/02_fix_missing_dfu.patch ; \
	  patch -Np1 < ../debian/patches/patch_include_source.patch
	GZIP=-9n tar --mtime='$(SOURCE_DATE_UTC)' --sort=name --owner=root --group=root \
	  --mode=go=rX,u+rw,a-s -czvf ubertooth-firmware-source.tar.gz \
	  ubertooth-firmware-source/
	#
	
	dh_auto_configure --sourcedirectory=host -- -DLIB_SUFFIX=/$(DEB_HOST_MULTIARCH) \
	  -DUBERTOOTH_GROUP=plugdev -DUDEV_RULES_PATH=/lib/udev/rules.d
	dh_auto_configure --sourcedirectory=firmware

override_dh_auto_build:
	dh_auto_build --sourcedirectory=host
	dh_auto_build --sourcedirectory=firmware -- TIMESTAMP="-DTIMESTAMP=''" \
	  COMPILE_BY="-D'COMPILE_BY=\"user\"'" COMPILE_HOST="-D'COMPILE_HOST=\"localhost\"'"
	cat debian/ubertooth.pc.in | sed 's/DEB_HOST_MULTIARCH/$(DEB_HOST_MULTIARCH)/' | \
	  sed 's/UPSTREAM_PACKAGE_VERSION/$(UPSTREAM_PACKAGE_VERSION)/' > debian/ubertooth.pc

override_dh_auto_install:
	chmod a-x firmware/*/*.bin
	dh_auto_install --sourcedirectory=host
	dh_auto_install --sourcedirectory=firmware
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	cp debian/ubertooth.pc debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/
	rm debian/tmp/usr/bin/ubertooth-tx # The tool does nothing - not fully implemented. Skip installing
	                                   # TODO: remove line when tool has been implemented

override_dh_installman:
	cd debian/man ; CHANGELOG_DATE="$(CHANGELOG_DATE)" ./genmanpages.sh
	dh_installman
