#!/usr/bin/make -f
# -*- makefile -*-

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

UVERSION := $(shell dpkg-parsechangelog | sed -n -e's/^Version: \([0-9]:\)\(.*\)-[^-]\+/\2/p')
PACKAGE_NAME := $(shell dpkg-parsechangelog | sed -n -e's/^Source: \(.*\)\+/\1/p')
PYVERS = $(shell pyversions -r)
PY3VERS = $(shell py3versions -r)
export PYBUILD_NAME=$(PACKAGE_NAME)
export PYTHONPATH=$(CURDIR)/src

%:
	dh $@ --with python2,python3,sphinxdoc --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean
	rm -rf docs/_build
	rm -rf src/webassets.egg-info/
	rm -f debian/webassets.1
	rm -f debian/webassets3.1

override_dh_auto_install:
	dh_auto_install
	# Doc
	cd docs; \
		make html; \
		cd -
	mv debian/python3-webassets/usr/bin/webassets debian/python3-webassets/usr/bin/webassets3
	help2man --version-string=$(UVERSION) \
				--name="Asset management application for Python web development" \
				--no-info \
				debian/python-webassets/usr/bin/webassets > debian/webassets.1
	help2man --version-string=$(UVERSION) \
				--name="Asset management application for Python web development" \
				--no-info \
				debian/python3-webassets/usr/bin/webassets3 > debian/webassets3.1

override_dh_installexamples:
	dh_installexamples -X.gitignore

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -ex; \
		for py in $(PYVERS) $(PY3VERS); do \
			$$py run_tests.py; \
		done
endif

get-orig-source:
	@if [ ! -d "debian" ] ; then \
		echo 'Run this from the top directory of the Debian source' >&2; \
		exit 1; \
	fi
	mkdir -p ../tmp/webassets_$(UVERSION)
	wget http://pypi.python.org/packages/source/w/webassets/webassets-$(UVERSION).tar.gz \
		-O ../tmp/webassets_$(UVERSION).tar.gz;
	tar xf ../tmp/webassets_$(UVERSION).tar.gz \
		-C ../tmp/webassets_$(UVERSION) \
		--exclude=*egg-info*;
	tar -C ../tmp/webassets_$(UVERSION) webassets-$(UVERSION) -c --xz -f ../webassets_$(UVERSION).orig.tar.xz
