#!/usr/bin/make -f

#export DH_VERBOSE = 1

export PYBUILD_NAME=sasmodels

# enable additional hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

# opencl needs somewhere to cache compiled objects
export POCL_CACHE_DIR=$(CURDIR)/.cache/
export XDG_CACHE_HOME=$(CURDIR)/.cache/
# and also tell pyopencl not to cache
export PYOPENCL_NO_CACHE=1
# and give somewhere for sasmodels to look for models
export SAS_MODELPATH=$(CURDIR)/.sasmodels/
export SAS_MODELPATH_COMPILED=./sasmodels/compiled_models/

# ensure test logs don't get accidentally installed
export PYBUILD_AFTER_INSTALL=rm -rf {build_dir}/logs

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

override_dh_clean:
	dh_clean
	make -C doc clean
	rm -rf $(POCL_CACHE_DIR) $(SAS_MODELPATH) $(SAS_MODELPATH_COMPILED)
	rm -rf doc/guide/models/
	rm -rf sasmodels.egg-info
	find -name logs -type d -exec rm -rf {} +

override_dh_auto_build:
	mkdir -p $(POCL_CACHE_DIR) $(SAS_MODELPATH) $(SAS_MODELPATH_COMPILED)
	PYTHONPATH=. ./debian/compile_models
	dh_auto_build
	MPLBACKEND=Agg SAS_OPENCL=none make -C doc html
	ln -sf /usr/share/javascript/mathjax doc/_build/html/_static/

override_dh_auto_install:
	dh_auto_install
	for d in debian/*/usr/lib/python*/*packages/sasmodels/compiled_models; do \
	  rm -rf $$d; \
	  ln -s /usr/lib/sasmodels/compiled_models $$d ; \
	done

override_dh_auto_test:
	# don't run the tests if suppressed with DEB_BUILD_OPTIONS=nocheck
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p $(POCL_CACHE_DIR)
	PYBUILD_SYSTEM=custom \
	  PYBUILD_TEST_ARGS='cd {build_dir} && \
	    {interpreter} -m sasmodels.model_test dll all && \
	    {interpreter} -m unittest -v sasmodels.resolution; \
	    ' \
          dh_auto_test --buildsystem=pybuild
endif
