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

export PY3VER=$(shell py3versions -vd)
SOURCE_DATE:=$(shell date -d "@$(SOURCE_DATE_EPOCH)" "+%a, %d %b %Y" || echo "xxx, xx xxx xxxx")
SOURCE_TIME:=$(shell date -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)
	: # Documentation depends on built package
	dh_auto_build
	: # 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
	http_proxy=127.0.0.1:9 \
	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/
	: # Fix up links to MathJax to be local
	grep -l '/mathjax/' -R build/html/examples/notebooks \
	| xargs -r sed -i -e 's,https://\S*.org/mathjax/latest/MathJax.js,file:///usr/share/javascript/mathjax/MathJax.js,g'
	: # Replace require
	grep -l '/require\.js/' -R build/html/examples/notebooks \
	| xargs -r sed -i -e 's,https://\S*.com/ajax/libs/require.js/.*/require.min.js,file:///usr/share/javascript/requirejs/require.min.js,g'
	grep -l '/jquery/' -R build/html/examples/notebooks \
	| xargs -r sed -i -e 's,https://\S*.com/ajax/libs/jquery/.*/jquery.min.js,file:///usr/share/javascript/jquery/jquery.min.js,g'
	: # 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"` ; do \
	    sed -i -e 's#$(CURDIR)/.pybuild/[^/]*/build/statsmodels/#/usr/lib/python3/dist-packages/statsmodels/#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_installexamples:
ifneq (,$(findstring python-statsmodels-doc, $(shell dh_listpackages)))
	dh_installexamples -ppython-statsmodels-doc --doc-main-package=python3-statsmodels
endif
	dh_installexamples -Npython-statsmodels-doc

override_dh_installdocs:
	rm -rf docs/source/generated/
ifneq (,$(findstring python-statsmodels-doc, $(shell dh_listpackages)))
	dh_installdocs -ppython-statsmodels-doc --doc-main-package=python3-statsmodels
endif
	dh_installdocs -Npython-statsmodels-doc
	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:
	dh_auto_clean
	-rm -rf cythonize.dat \
	       *.egg-info \
	       *.png \
	       build/Rtmp build/datacache \
	       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

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
	: # 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/python3-statsmodels/python3-scikits.statsmodels/g'); \
		mkdir -p $$skbd_; \
		mv $$skd $$skbd_; \
	done
	dh_numpy3

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