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

PACKAGE_NAME = python-pandas
PACKAGE_ROOT_DIR = debian/${PACKAGE_NAME}

PYVERS = $(shell pyversions -vr)
PYVER = $(shell pyversions -vd)

UVER := $(shell LC_ALL=C dpkg-parsechangelog | awk '/^Version:/{print $$2;}' | sed -e 's,-[^-]*$$,,g')
CYTHONVER := $(shell dpkg -l cython | awk '/^ii/{print $$3;}' || echo 0)
MPLVER := $(shell dpkg -l python-matplotlib | awk '/^ii/{print $$3;}' || echo 0)

MIN_CYTHONVER = 0.15.1
EXCLUDE_MORETESTS := 
# $(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

cythonize:
	find pandas/src -iname *.c -delete
	python setup.py cython
	cp pandas/src/*.c debian/cythonized-files
	echo "$(UVER)" >| debian/cythonized-files/VERSION

override_dh_auto_configure:
	dpkg --compare-versions $(CYTHONVER) lt $(MIN_CYTHONVER) && { \
		echo "I: Using pre-Cython-ed files"; \
		cp -rp debian/cythonized-files/*.c pandas/src/; } || :
	dh_auto_configure

override_dh_clean:
	: # 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
	grep -l -e 'Generated by Cython' pandas/src/*.c | xargs -r rm -f
	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%} ${PYVERS:%=python-test%}
# Per Python version logic -- install, test, move .so into -lib
python-install%:
	python$* setup.py install --install-layout=deb --root=$(PACKAGE_ROOT_DIR)

python-test%: python-install%
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "backend : Agg" >| $(CURDIR)/build/matplotlibrc
	: # Run unittests here against installed pandas
	export PYTHONPATH=`/bin/ls -d $$PWD/$(PACKAGE_ROOT_DIR)/usr/lib/python$*/*/`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	cd build/; python$* /usr/bin/nosetests -s -v pandas;
	: # Upstream was informed about testFamaMacBethRolling failures on 32bit
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/$(PACKAGE_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 RELEASE.rst TODO.rst README*.rst

#override_dh_installchangelogs:
#	dh_installchangelogs doc/whats_new.rst

## move binary libraries into -lib
override_dh_pysupport:
	: # Move platform-specific libraries into -lib
	for lib in $$(find $(PACKAGE_ROOT_DIR)/usr -name '*.so'); do \
	   sdir=$$(dirname $$lib) ; \
	   tdir=$(PACKAGE_ROOT_DIR)-lib/$${sdir#*$(PACKAGE_NAME)/} ; \
	   mkdir -p $$tdir ; \
	   echo "Moving '$$lib' into '$$tdir'." ; \
	   mv $$lib $$tdir ; \
	done
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy -ppython-pandas-lib; fi
	dh_pysupport

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