#!/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=+bindnow
%:
	dh $@ --with python2

override_dh_auto_configure:
	dh_auto_configure -- --with-debug

# backportability: patch upstream source for compatibility with older libav if necessary.
NEW_AV=$(shell dpkg --compare-versions $$(dpkg --status libavcodec-dev | grep Version | cut -d' ' -f2) ge 6:9.1 && echo 1)
override_dh_auto_build:
	# Make sure all patches applied
	dh_quilt_patch
	# Unapply top patch if necessary (i.e. if new libav detected)
	[ -z "$(NEW_AV)" ] || QUILT_PATCHES=debian/patches quilt pop
	# Build:
	dh_auto_build

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/xpra
	# Post-install clean-up
	$(RM) -v debian/xpra/usr/share/xpra/COPYING debian/xpra/usr/bin/xpra_Xdummy

## gracefully handle stripping if -dbg packages are (un-)commented in debian/control
override_dh_strip:
	[ -d "$(CURDIR)/debian/xpra-dbg" ] \
        && dh_strip -pxpra --dbg-package=xpra-dbg \
        || dh_strip -pxpra

override_dh_builddeb:
	dh_builddeb -- -Zxz

## http://wiki.debian.org/onlyjob/get-orig-source
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER  ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.]+)}')
DTYPE = +dfsg
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz $(info I: $(PKG)_$(VER)$(DTYPE))
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
        && tar -xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
        || $(RM) -r $(PKG)-$(VER)
	@echo "# Clean-up..."
	cd $(PKG)-$(VER) \
        && $(RM) -r -v \
             */*.bmp \
             */.ico \
             */*.ico \
             xpra/codecs/webm \
             html5
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
        | XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "$(PKG)-$(VER)"
