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

PACKAGE_NAME = python-statsmodels
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)
IPYTHONVER := $(shell ipython -V || echo 0) # 0.12 is needed to build docs, otherwise use IPython01x
IPYTHONPATH := $(shell dpkg --compare-versions $(IPYTHONVER) lt 0.13 \
				 && echo ":/usr/lib/python$(PYVER)/dist-packages/IPython01X" || echo '')

MIN_CYTHONVER = 0.19

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export OPT+=-g -O0
endif

%:
	dh  $@ --with python2


cythonize:
	find statsmodels -iname *.pyx | sed -e 's,\.pyx,\.c,g' | xargs -r rm -f
	: # since there is no cython command and I have no time...
	python setup.py build_ext --inplace -f
	mkdir -p debian/cythonized-files
	cp $$(find statsmodels -iname *.pyx | sed -e 's,\.pyx,\.c,g') 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"; \
		find -iname *pyx | while read p; do \
		  f=$$(basename $$p); f=$${f%%.pyx}; d=$$(dirname $$p); \
		  cp -rp debian/cythonized-files/$$f.c $$d/; done; } || :
	dh_auto_configure


override_dh_auto_test:
	: # overriden since testing happens against installed version across
	: # all supported python versions

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

# Build docs during build
override_dh_auto_build-arch: $(HOME)
	dh_auto_build
	: # Hardcode backend to Agg to avoid doc build and tests failures
	echo "backend : Agg" >| $(MPLCONFIGDIR)/matplotlibrc

override_dh_auto_build-indep: python-install$(PYVER)
	: # I: Generate documentation
	: # Build Documentation -- assure existence of build/html for nodoc
	mkdir -p build/html
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=`/bin/ls -d $(CURDIR)/$(PACKAGE_ROOT_DIR)/usr/lib/python$(PYVER)/*/`:$(CURDIR)$(IPYTHONPATH) \
	 $(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*.com/mathjax/latest/MathJax.js,file:///usr/share/javascript/mathjax/MathJax.js,g'
	rm -rf docs/source/generated/
endif

override_dh_auto_clean: $(HOME)
	dh_auto_clean
	-rm -f cythonize.dat

override_dh_clean:
	dh_clean
	rm -rf build *-stamp *.egg-info docs/source/generated
	: # Remove autogenerated version.py
	rm -f statsmodels/version.py

override_dh_auto_install-arch: ${PYVERS:%=python-install%} ${PYVERS:%=python-test%}

override_dh_auto_install-indep:

# 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)))
	: # Run unittests here against installed statsmodels
	cd build/ && \
	 PYTHONPATH=`/bin/ls -d $(CURDIR)/$(PACKAGE_ROOT_DIR)/usr/lib/python$*/*/` \
	 MPLCONFIGDIR=$(CURDIR)/build \
	 python$* /usr/bin/nosetests -s -v --exclude=sandbox --exclude=test_plot_month statsmodels
else
	: # Skip unittests due to nocheck
endif


override_dh_install:
	dh_install
	: # Remove compiled due to testing files
	find debian -name *.pyc -delete
	: # 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_installdocs:
	: # Use jquery from Debian package, so prune shipped one
	-rm ./build/html/_static/jquery.js
	dh_installdocs


## move binary libraries into -lib
override_dh_python2:
	: # 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 "I: Moving '$$lib' into '$$tdir'." ; \
	   mv $$lib $$tdir ; \
	done
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy -ppython-statsmodels-lib; fi
	: # 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'); \
		mkdir -p $$skbd_; \
		mv $$skd $$skbd_; \
	done
	dh_python2


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