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

export DEB_BUILD_HARDENING=1

# Pass hardening flags into distutils
CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
export CFLAGS

PACKAGE2_NAME = python-pandas
PACKAGE3_NAME = python3-pandas
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')

MIN_CYTHONVER = 0.17.3

# Some unittests might need to be excluded for different Python versions
EXCLUDE_TESTS := --exclude test_bar_log
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 '')

# try to prevent unsunctioned downloads
export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

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

clean_generated:
	find pandas/ -regex '.*\.c\(\|pp\)' | xargs grep -l -e 'Generated by Cython'  | xargs -r rm -f

_cythonize%:
	debian/rules clean_generated # force removal of previous copies
	python$(*:2=) setup.py cython
	D=debian/cythonized-files$(*:2=) && \
		git rm -rf $$D; \
		find pandas/ -regex '.*\.c\(\|pp\)' | while read f; do \
			grep -q 'Generated by Cython' "$$f" || continue; \
			mkdir -p "$$D/$$(dirname $$f)"; \
			cp "$$f" "$$D/$$(dirname $$f)"; \
			git add -f "$$D/$$f"; \
		done; \
		echo "$(UVER)" >| $$D/VERSION; git add $$D/VERSION

_uncythonize%:
	echo "$*" | grep -q '^3' && PY=3 || PY= ; \
	CYTHONVER=$$(dpkg -l cython$$PY 2>/dev/null | awk '/^ii/{print $$3;}' || echo 0); \
	dpkg --compare-versions "$$CYTHONVER" lt "$(MIN_CYTHONVER)" && { \
		echo "I: Using pre-Cython-ed files for Python $*"; \
		cd debian/cythonized-files$$PY/ ; \
		find . -regex '.*\.c\(\|pp\)' | while read f; do cp $$f ../../$$f; done; } || :

cythonize: _cythonize2 _cythonize3

override_dh_clean: clean_generated
	: # Make sure that cythonized sources are up-to-date
	[ "$(UVER)" = "`cat debian/cythonized-files/VERSION`" ]
	rm -rf build doc/_build *-stamp # pandas.egg-info pandas/datasets/__config__.py
	dh_clean
	: # prune auto-generated version.py -- will be generated during build again
	rm -f pandas/version.py

override_dh_auto_build:
	# Override default build operation which --force's re-cythonization
	# on elderly ubuntus
	:

override_dh_auto_install: ${PYVERS:%=python-install%} ${PY3VERS:%=python-install%} ${PYVERS:%=python-test%} ${PY3VERS:%=python-test%}
# Per Python version logic -- install, test, remomove .so (installed into -lib)
python-install%: _uncythonize%
	python$* setup.py install --install-layout=deb --root=$(CURDIR)/debian/tmp

python-test%: python-install%
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "backend : Agg" >| $(CURDIR)/build/matplotlibrc
	: # Run unittests here against installed pandas
	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; \
	python$* ci/print_versions.py; \
    cd build/; xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" \
      python$* /usr/bin/nosetests -s -v -a '!network' $(EXCLUDE_TESTS) $(EXCLUDE_TESTS$*) pandas;
else
	: # Skip unittests due to nocheck
endif

override_dh_installdocs:
	: # Build Documentation using installed pandas
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; \
# 	 cd doc; python make.py html
endif
endif
	: # Use jquery from Debian package, so prune shipped one
	#TODO -rm doc/_build/html/_static/jquery.js
	dh_installdocs -A *.md

#override_dh_installchangelogs:
#	dh_installchangelogs doc/whats_new.rst

## remove .so libraries from main package, and call dh_numpy*
## while removing 2 if not present
_dh_python%:
	if [ -d debian/$(PACKAGE$*_NAME)/usr/lib ]; then \
	   find debian/$(PACKAGE$*_NAME)/usr/lib -name "*.so" -delete; \
	fi
	[ -e /usr/bin/dh_numpy$(*:2=) ] && dh_numpy$(*:2=) -p$(PACKAGE$*_NAME)-lib || :
	dh_python$*

## "Instantiate" both rules so dh sees them
override_dh_python2: _dh_python2
override_dh_python3: _dh_python3

## 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
