#!/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
include /usr/share/dpkg/architecture.mk
export HOME=$(CURDIR)/build
export MPLCONFIGDIR=$(HOME)

export PY3VER=$(shell py3versions -vd)

export PYBUILD_NAME=statsmodels
export PYBUILD_INSTALL_ARGS=--install-layout=deb
export PYBUILD_TEST_ARGS = --exclude='(test_sarimax)|(test_structural)|(test_dynamic_factor)|(test_varmax)'

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

# To guarantee HOME existence with mpl 1.3.0
# See https://github.com/matplotlib/matplotlib/issues/2300
#     http://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)
ifeq ($(DEB_HOST_ARCH),armhf)
	patch -p1 -Z -F 0 -f -N < $(CURDIR)/debian/patches/disable_broken_KalmanFilter.patch || true
endif
	dh_auto_build

override_dh_auto_build-indep: $(HOME)
	: # Documentation depends on built package
	dh_auto_build
	: # I: Generate documentation
	: # Build Documentation -- assure existence of build/html for nodoc
	mkdir -p build/html

override_dh_installdocs:
	rm -rf docs/source/generated/
	dh_installdocs
	for html in `find debian -name "*.html"` ; do \
	    sed -i 's#https://cdnjs\..*/mathjax/.*/MathJax.js\(?config=TeX.*L">\)#file:///usr/share/javascript/mathjax/MathJax.js\1#' $${html} ; \
	    sed -i 's#https://cdnjs\..*/mathjax/.*/latest.js#file:///usr/share/javascript/mathjax/unpacked/latest.js#' $${html} ; \
	done

override_dh_auto_clean:
ifeq ($(DEB_HOST_ARCH),armhf)
	patch -p1 -Z -F 0 -f -R < $(CURDIR)/debian/patches/disable_broken_KalmanFilter.patch || true
endif
	dh_auto_clean
	-rm -rf cythonize.dat \
	       *.egg-info \
	       *.png \
	       docs/build/ \
	       docs/rehab.table \
	       docs/salary.table \
	       docs/source/datasets/generated \
	       docs/source/dev/generated \
	       docs/source/datasets/statsmodels.datasets.*.rst \
	       docs/source/examples/notebooks/generated \
	       examples/executed \
	       statsmodels/tsa/statespace/_statespace.pyx \
	       statsmodels/tsa/regime_switching/_hamilton_filter.pyx \
	       tools/hash_dict.pickle
	: # Remove autogenerated version.py
	rm -f statsmodels/version.py

override_dh_install:
	dh_install
	: # 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


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



## move binary libraries into -lib
override_dh_auto_install:
	dh_auto_install
	for PACKAGE_NAME in python-statsmodels 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
	: # Prune scikits/__init__.py to avoid conflicts
	find debian -wholename \*scikits/__init__.py -delete
	: # Move scikits. space into a compatibility package
	set -e; \
	find debian -wholename \*scikits -type d | while read skd; do \
		skbd=$$(dirname $$skd); \
		skbd_=$$(echo $$skbd | sed -e 's/python-statsmodels/python-scikits.statsmodels/g' \
		                     | sed -e 's/python3-statsmodels/python3-scikits.statsmodels/g'); \
		mkdir -p $$skbd_; \
		mv $$skd $$skbd_; \
	done
	dh_numpy
	dh_numpy3

get-orig-source:
	uscan --upstream-version 0 --rename

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

