#!/usr/bin/make -f

PKD   = $(word 1,$(abspath $(dir $(MAKEFILE_LIST))))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
UVER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
DTYPE = +dfsg1
VER  ?= $(subst $(DTYPE),,$(UVER))

DHFLAGS=--parallel

%:
	dh $@ --with python2 $(DHFLAGS)

override_dh_install:
	rm -f debian/tmp/usr/lib/*/*.la
	rm -f debian/tmp/usr/share/MateConf/gsettings/mate-panel.convert
	dh_install --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_auto_configure:
	# upstream tarball is without configure. autogen.sh will create it
	NOCONFIGURE=1 ./autogen.sh
	dh_auto_configure $(DHFLAGS) -- \
		--disable-silent-rules \
		--localstatedir=/var \
		--libexecdir=/usr/lib/mate-panel \
		--disable-scrollkeeper \
		--disable-static \
		--enable-introspection \
		--enable-gtk-doc

override_dh_strip:
	dh_strip -pmate-panel --dbg-package=mate-panel-dbg
	dh_strip -plibmate-panel-applet-4-1 --dbg-package=libmate-panel-applet-dbg


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 "# Cleaning-up..."
	cd $(PKG)-$(VER) \
	    && find . -depth -type d -name 'windows' -exec $(RM) -r {} \; -printf 'removed %p\n' \
	    && $(RM) -r -v \
	        help/ \
		COPYING-DOCS
	$(RM) -v $(PKG)_$(VER).orig.tar.*
	@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)"
