#!/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')
# Python doesn't use ~ for rc
UVER_PY := $(shell echo $(UVER) | sed -e 's,[~],,g')
UVER_PYSHORT := $(shell echo $(UVER_PY) | sed -e 's,+git.*,,g')

MIN_CYTHONVER = 0.23

ifneq ($(DEB_HOST_ARCH),amd64)
	# obtained by   grep -e 'ERROR:' -e 'FAIL:' pandas-sid.log |awk '{print $2;}' | sed -e 's,^test,,g' | tr '\n' '|'
	# on log of failed tests on mips build box on pandas 0.19.2-1
	# Majority of them is probably due to a bug in NumPy https://github.com/numpy/numpy/issues/8325
	# of incorrectly treating NaT on non-amd64 platforms
	# So for stretch release for now disabling those tests on non-amd64
    # plot ones are excluded due to seems to be a bug in matplotlib which shows up
    # on s390
	EXCLUDE_TESTS_ARCH := --exclude 'test(_frame_from_json_to_json|_misc_example|ArrayNumpyLabelled|DataFrameNumpyLabelled|_resample_timedelta_values|_timestamp_compare|_where_timedelta|ArrayNumpyExcept|_resample_datetime_values|_NaT_cast|_where_datetime|_where_datetime|_datetimelikes_nan|_value_counts_normalized|_agg_dict_parameter_cast_result_dtypes|_boxplot|_boxplot_vertical|_errorbar_plot|_hist_df|_line_area_stacked|_plot|_round_trip_valid_encodings)'
else
	EXCLUDE_TESTS_ARCH :=
endif

# Some unittests might need to be excluded for different Python versions
EXCLUDE_TESTS := --exclude 'test_(bar_log|set_locale|constructor_compound_dtypes|invalid_index_types|searchsorted_sorter|sparse_max_row|msgpack|read_dta18|integer_panel|mixed_arithmetic|binary_ops|import_google_api_python_client|to_hdf_with_object_column_names)'
EXCLUDE_TESTS2 :=
EXCLUDE_TESTS3.2 :=
#--exclude "test_bootstrap_plot"
EXCLUDE_TESTS3.3 := --exclude 'test_(to_latex_filename|expressions)'

#--exclude "test_(bootstrap_plot|quoting|cant_compare_tz_naive_w_aware|more_flexible_frame_multi_function|yahoo)"
EXCLUDE_TESTS3.4 := $(EXCLUDE_TESTS3.3)
EXCLUDE_TESTS3.5 := $(EXCLUDE_TESTS3.3)

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

export SHELL=/bin/bash

# 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

version_py:
	[ -e pandas/__version.py ] || \
      echo -e "version = '$(UVER_PY)'\nshort_version = '$(UVER_PYSHORT)'" > pandas/__version.py

override_dh_auto_build: version_py
	# Override default build operation which --force's re-cythonization
	# on elderly ubuntus
	# Just build the version.py file
	:

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/; LC_ALL=C.UTF-8 xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" \
      python$* /usr/bin/nosetests -s -v -A 'not network and not disabled' $(EXCLUDE_TESTS) $(EXCLUDE_TESTS$*) $(EXCLUDE_TESTS_ARCH) 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%:
	[ -e /usr/bin/dh_numpy$(*:2=) ] && dh_numpy$(*:2=) -p$(PACKAGE$*_NAME)-lib || :
	dh_python$*
	-find debian/python*-pandas -name "*.so" -delete

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