#!/usr/bin/make -f

PKGDIR=$(CURDIR)/debian/ibid

%:
	dh --with python-central $@

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/ docs/html/

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

override_dh_installdocs:
	dh_installdocs -Xdocs/html/_sources -Xdocs/_build -Xdocs/html/.doctrees \
	               -Xdocs/html/_static/jquery.js

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

override_dh_pycentral:
	rm -rf $(wildcard $(PKGDIR)/usr/lib/python*/*-packages/ibid/templates)
	rm -rf $(wildcard $(PKGDIR)/usr/lib/python*/*-packages/ibid/test)
	rm -rf $(wildcard $(PKGDIR)/usr/lib/python*/*-packages/*.egg-info/requires.txt)
	dh_pycentral
	set -e; \
	for DATA in data configspec.ini logging.ini config.ini static templates; do \
		cp -a ibid/$$DATA $(PKGDIR)/usr/share/ibid/; \
	done

override_dh_compress:
	dh_compress -X.js

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
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
	                -f ibid_$(VER)+dfsg.orig.tar.gz ibid-$(VER)
	rm -rf ibid-$(VER)
