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

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

export SHELL = /bin/bash

DOCDIR:=$(CURDIR)/debian/python-gevent-doc/usr/share/doc/python-gevent-doc

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

override_dh_auto_build:
	dh_auto_build
	PYTHONPATH=../.pybuild/pythonX.Y_2.7/build/ make --directory doc/ html

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(CURDIR)/gevent/*.so
	cd doc && make clean
	rm -f $(CURDIR)/doc/gevent.*.rst $(CURDIR)/doc/changelog.rst
	rm -rf $(CURDIR)/gevent.egg-info/
	rm -f $(CURDIR)/greentest/testresults.sqlite3
	[ ! -f libev/Makefile ] || (cd $(CURDIR)/libev/ && $(MAKE) distclean)
	rm -f $(CURDIR)/c-ares/ares_config.h $(CURDIR)/c-ares/config.log \
		$(CURDIR)/c-ares/config.status \
		$(CURDIR)/c-ares/stamp-h1 $(CURDIR)/c-ares/stamp-h2

override_dh_install:
	dh_install -O--parallel -O--buildsystem=pybuild --list-missing
	: # remove the compat stuff for 2/3
	rm -v -f debian/python-gevent/usr/lib/python2.7/dist-packages/gevent/_{util_py,ssl,fileobject,socket}3*
	rm -v -f debian/python3-gevent/usr/lib/python3*/dist-packages/gevent/_{util_py,ssl,fileobject,socket}2*

override_dh_compress:
	dh_compress -X.js -X_static/* -X _sources/* -X_sources/*/* -X.inv

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -p python-gevent --dbg-package=python-gevent-dbg
	dh_strip -p python3-gevent --dbg-package=python3-gevent-dbg
endif

# TODO: check for failures, but disable for now
override_dh_auto_test:
#	chmod +x $(CURDIR)/src/greentest/testrunner.py
#	cd $(CURDIR)/src/greentest && PYTHONPATH=../.. ./testrunner.py

override_dh_installdocs-arch:
	dh_installdocs -Npython-gevent-dbg -Npython3-gevent-dbg
	dh_installdocs -ppython-gevent-dbg --link-doc=python-gevent
	dh_installdocs -ppython3-gevent-dbg --link-doc=python3-gevent

override_dh_installdocs-indep:
	dh_installdocs -i
	# remove privacy tracking links
	sed -i 's|http.*/btn_donateCC_LG.gif||' \
		$(DOCDIR)/html/sfc.html
	sed -i 's|http.*/pixel.gif||' \
		$(DOCDIR)/html/sfc.html
	sed -i 's|<a .\+twitter.com.\+>\(.\+\)</a>|\1|' \
		$(DOCDIR)/html/index.html
	sed -i 's|<script>.\+platform.twitter.com.\+</script>||' \
		$(DOCDIR)/html/index.html

override_dh_installexamples-indep:
	dh_installexamples -i
	# remove privacy tracking links
	sed -i 's|http.*/jquery.min.js|file://usr/share/javascript/jquery/jquery.min.js|' \
		debian/python-gevent-doc/usr/share/doc/python-gevent-doc/examples/webchat/templates/index.html


.PHONY: override_dh_auto_build override_dh_auto_clean override_dh_install \
	override_dh_compress override_dh_strip override_dh_auto_test \
	override_dh_installdocs-arch override_dh_installdocs-indep \
	override_dh_installexamples-indep
