#!/usr/bin/make -f

PKGDIR=$(CURDIR)/debian/ibid

%:
	dh $@ --with python2,sphinxdoc

TRIAL=$(shell which trial)

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; \
	for ver in $(shell pyversions -vr); do \
		PYTHONPATH=. python$$ver $(TRIAL) --reporter=text ibid; \
	done
endif

override_dh_auto_build:
	dh_auto_build
	# Old sphinx-builds (lenny) are picky
	mkdir -p docs/html docs/_static
	sphinx-build -N -bhtml docs/ build/docs

override_dh_auto_install:
	dh_auto_install
	rm -rf $(PKGDIR)/usr/lib/python*/*-packages/ibid/test
	rm -rf $(PKGDIR)/usr/lib/python*/*-packages/*.egg-info/requires.txt

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build
	rm -rf *.egg-info
	rm -rf _trial_temp
	find . -name dropin.cache -delete

override_dh_installdocs:
	dh_installdocs
	cd $(PKGDIR)/usr/share/doc/ibid && mv docs html

override_dh_installexamples:
	dh_installexamples
	cd $(PKGDIR)/usr/share/doc/ibid/examples/factpacks && rm -f *.py *.sh

override_dh_compress:
	dh_compress -X.inv -X.txt

override_dh_python2:
	dh_python2 --no-guessing-deps

REV=$(shell dpkg-parsechangelog | sed -rne 's,^Version: .*bzr([0-9]+).*,\1,p')
VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: (.+)\+dfsg.*,\1,p')
BZR_REPO?=lp:ibid
BZR_DOCS_REPO?=lp:~ibid-dev/ibid/docs

get-orig-source:
ifeq ($(REV),)
	uscan --noconf --force-download --rename --download-version=$(VER) --destdir=.
	tar -xf ibid_$(VER).orig.tar.gz
	mv Ibid-$(VER) ibid-$(VER)
	rm -f ibid_$(VER).orig.tar.gz
else
	bzr export -r $(REV) ibid-$(VER) $(BZR_REPO)
	rm -rf ibid-$(VER)/docs
	bzr export ibid-$(VER)/docs $(BZR_DOCS_REPO)/docs
endif
	cd ibid-$(VER)/factpacks/ && rm -f handey.json overlord.json wrestling.json zippy.json
	rm -rf ibid-$(VER)/docs/html
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
	                -f ibid_$(VER)+dfsg.orig.tar.gz ibid-$(VER)
	rm -rf ibid-$(VER)
