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

# override HOME (for weave) and matplotlib config directory
# to allow building in chroots with read-only HOME
export HOME=$(CURDIR)/build
export MPLCONFIGDIR=$(HOME)

#block internet access
export http_proxy=127.0.0.1:9
export https_proxy=127.0.0.1:9

export PY3VER=$(shell py3versions -vd)
SOURCE_DATE:=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%a, %d %b %Y" || echo "xxx, xx xxx xxxx")
SOURCE_TIME:=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+%T" || echo "xx:xx:xx")

export PYBUILD_NAME=statsmodels
export PYBUILD_INSTALL_ARGS=--install-layout=deb

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --with python3,sphinxdoc --buildsystem=pybuild

# To guarantee HOME existence with mpl 1.3.0
# See https://github.com/matplotlib/matplotlib/issues/2300
#     https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=719384
$(HOME):
	mkdir "$@"
	: # Hardcode backend to Agg to avoid doc build and tests failures
	echo "backend : Agg" >| $(MPLCONFIGDIR)/matplotlibrc

override_dh_auto_build-arch: $(HOME)
	dh_auto_build

override_dh_auto_build-indep: $(HOME) override_dh_auto_build-arch
	: # above dependency is because documentation depends on built package
	: # I: Generate documentation
	: # Build Documentation -- assure existence of build/html for nodoc
	: # Reproducibility: stopping the clock (or slowing it down enough to get all 00:00:00) with faketime (near-)hangs in glm_weights.ipynb
	mkdir -p build/html
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	python3 debian/datasets/prepopulate_cache.py
	PYTHONPATH=$(shell pybuild --print build_dir --interpreter python3) \
	BUILDDIR=$(CURDIR)/build \
	STATSMODELS_DATA=$(CURDIR)/build/datacache/ \
	 LC_ALL=C.UTF-8 make -C docs html
	mv docs/build/* build/
	: # replace timestamps and build paths in examples output for reproducibility
	for html in `find build/html examples -name _modules -prune -o -name "*.html" -o -name "*.ipynb" -o -name "*.ipynb.txt"` ; do \
	    sed -i -e 's#$(CURDIR)/.pybuild/[^/]*/build/statsmodels/#/usr/lib/python3/dist-packages/statsmodels/#g' \
	    -e 's# at 0x[0-9a-f]\{8,16\}\(&gt;\|>\)# at 0xadde5de1e8ed\1#g' \
	    -e 's#\(Date:.*\)[A-Z][a-z]\+, \+[0-9]\+,\? \+[A-Z][a-z]\+,\? \+[0-9]\+#\1$(SOURCE_DATE)#g' \
	    -e 's#\(Time:.*\)[0-9][0-9]:[0-9][0-9]:[0-9][0-9]#\1$(SOURCE_TIME)#g' $${html} ; \
	done
endif

override_dh_installdocs:
	rm -rf docs/source/generated/
	dh_installdocs

override_dh_auto_clean:
	# this would try to run setup.py clean, which is an error: dh_auto_clean
	-rm -rf cythonize.dat \
	       *.egg-info \
	       *.png \
	       .pybuild \
	       .pytest_cache \
	       build \
	       docs/build/ \
	       docs/rehab.table \
	       docs/salary.table \
	       docs/source/datasets/generated \
	       docs/source/examples/notebooks \
	       docs/source/savefig \
	       docs/source/dev/generated \
	       docs/source/datasets/statsmodels.datasets.*.rst \
	       docs/source/examples/notebooks/generated \
	       docs/source/examples/index.rst \
	       examples/executed \
	       tools/hash_dict.pickle
	find . -name __pycache__ -print0 | xargs -0 rm -rf
	find . -name *.pyx -print0 | sed -e "s/\.pyx/.c/g" | xargs -0 rm -f
	find . -name *.pyx.in -print0 | sed -e "s/\.pyx\.in/.pyx/g" | xargs -0 rm -f
	: # Remove autogenerated version.py
	rm -f statsmodels/version.py statsmodels/_version.py docs/source/examples/index.rst

override_dh_install:
	dh_install
	rm -f debian/*/usr/setup.cfg
	: # Remove compiled due to testing files
	find debian -name *.pyc -delete
	rm -f debian/*/usr/lib/*/dist-packages/enet_poisson.csv debian/*/usr/lib/*/dist-packages/enet_binomial.csv
	: # strip docs/ since they aren't really a Python module, there is -doc for it
	: # TODO find debian -wholename \*scikits/statsmodels/docs | xargs rm -rf
	find debian -iname COPYING -o -iname LICENSE* | xargs -r rm -f

# statsmodels has a conftest.py that uses testroot-only features, so the tests must be run from the statsmodels directory, not its parent
# https://github.com/statsmodels/statsmodels/issues/5108
# the cd is to be "anywhere but pkgroot" to avoid subprocesses (test_lazy_imports) importing the source tree (current directory is tried before PYTHONPATH), which doesn't work because the built .so files aren't there
# the inner 'for' (expected to be a single item, cpython3_${testpy3ver}_statsmodels/build) is because globs aren't expanded when setting env vars
# TEST_SUCCESS is used because the return status of a for-loop is its last item, not the "fail if any of them do" wanted here
override_dh_auto_test:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	TEST_SUCCESS=true ; cd tools && for testpy3ver in `py3versions -vs` ; do \
#	for testpath in ../.pybuild/*$${testpy3ver}*/*/statsmodels ; do \
#	PYTHONPATH=$${testpath}/.. python$${testpy3ver} -m pytest -v $${testpath} || TEST_SUCCESS=false ; \
#	rm -rf $${testpath}/.pytest_cache ; \
#	done ; done ; $${TEST_SUCCESS}
#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 -X.inv -Xobjects.inv

override_dh_link:
	: # deduplicate images - the ||true is because we only build-depend on jdupes if we're building documentation
	jdupes -r -l debian/python-statsmodels-doc/usr/share/doc || true
	dh_link

## move binary libraries into -lib
override_dh_auto_install:
	dh_auto_install
	for PACKAGE_NAME in python3-statsmodels; do \
	  for lib in $$(find debian/$${PACKAGE_NAME}/usr -name '*.so'); do \
	     sdir=$$(dirname $$lib) ; \
	     tdir=debian/$${PACKAGE_NAME}-lib/$${sdir#*$${PACKAGE_NAME}/} ; \
	     mkdir -p $$tdir ; \
	     echo "I: Moving '$$lib' into '$$tdir'." ; \
	     mv $$lib $$tdir ; \
	  done ; \
	done
	dh_numpy3

override_dh_fixperms:
	dh_fixperms
	find debian -name "*.txt" -exec chmod -x \{\} \;
