#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

export DH_VERBOSE=1
export http_proxy=http://127.0.0.1:9/

PACKAGE2_NAME = python-patsy
PACKAGE3_NAME = python3-patsy
PACKAGE2_ROOT_DIR = debian/${PACKAGE2_NAME}
PACKAGE3_ROOT_DIR = debian/${PACKAGE3_NAME}

PYVERS = $(shell pyversions -vr)
PYVER = $(shell pyversions -vd)
PY3VERS = $(shell py3versions -vr)
PY3VER = $(shell py3versions -vd)

UVER := $(shell LC_ALL=C dpkg-parsechangelog | awk '/^Version:/{print $$2;}' | sed -e 's,-[^-]*$$,,g')

# Some unittests might need to be excluded for different Python versions
EXCLUDE_TESTS2 := 
EXCLUDE_TESTS3.2 := 
#--exclude "test_bootstrap_plot"
EXCLUDE_TESTS3.3 := 
#--exclude "test_(bootstrap_plot|quoting|cant_compare_tz_naive_w_aware|more_flexible_frame_multi_function|yahoo)"

# MPLVER := $(shell dpkg -l python-matplotlib | awk '/^ii/{print $$3;}' || echo 0)
# $(shell dpkg --compare-versions $(MPLVER) lt 1.0 && echo '|test_hist|test_plot|test_boxplot|test_corr_rank' || echo '')

# Mega rule
%:
	: # Explicit build system to avoid use of all-in-1 Makefile
	dh $@ --buildsystem=python_distutils --with python2,python3

python-build%:
	python$* setup.py build

override_dh_auto_build: $(PY3VERS:%=python-build%)
	dh_auto_build

python-install%:
	python$* setup.py install --root=$(CURDIR)/debian/tmp \
		--force --install-layout=deb

override_dh_auto_install: $(PYVERS:%=python-install%) $(PY3VERS:%=python-install%) \
                          ${PYVERS:%=python-test%}    ${PY3VERS:%=python-test%}
	: # nothing more to do

override_dh_clean:
	rm -rf build *.egg-info
	dh_clean

python-test%: python-install%
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "backend : Agg" >| $(CURDIR)/build/matplotlibrc
	: # Run unittests here against installed patsy
	echo "$*" | grep -q '^3' && PY=3 || PY=$*; \
	export PYTHONPATH=`/bin/ls -d $$PWD/debian/tmp/usr/lib/python$$PY/*/`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	mkdir -p build/tmp; cd build/tmp; \
	python$* /usr/bin/nosetests -s -v $(EXCLUDE_TESTS$*) patsy;
else
	: # Skip unittests due to nocheck
endif

override_dh_installdocs:
	: # Build Documentation using installed patsy
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
ifneq (,$(findstring -a,$(DH_INTERNAL_OPTIONS)))
	: # not building documentation in -a
else
	: # not building documentation ATM since requires ipython 0.11
	export PYTHONPATH=`/bin/ls -d $$PWD/$(PACKAGE2_ROOT_DIR)/usr/lib/python$(PYVER)/*`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	 make -C doc html
	: # Use jquery from Debian package, so prune shipped one
	-rm doc/_build/html/_static/jquery.js
	-rm doc/_build/html/_static/underscore.js
	dh_installdocs
endif
endif

## immediately useable documentation and exemplar scripts/data
override_dh_compress:
	dh_compress -X.py -X.html -X.pdf -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -Xobjects.inv
