#!/usr/bin/make -f

export DH_VERBOSE=1
export PYBUILD_NAME=pyfai

# add some verbosity to pyopencl
export PYOPENCL_COMPILER_OUTPUT=1

# run OpenCL tests on a selected set of architecture
ifneq (,$(filter $(DEB_BUILD_ARCH),amd64 arm64 armhf))
export PYFAI_OPENCL=True
else
export PYFAI_OPENCL=False
endif

# get the default python3 interpreter version
PY3VER := $(shell py3versions -dv)

# temporary until the 1/1/1970 issue is solved
SOURCE_DATE_EPOCH ?= $(shell dpkg-parsechangelog -S Timestamp)

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_clean:
	rm -f $(patsubst %.pyx,%.c,$(wildcard pyFAI/ext/*.pyx))
	rm -rf doc/build/html
	rm -rf build/man
	rm -rf *.egg-info
	rm -f all_testimage.json fai_cfg.json lima_cfg.json profile.log

override_dh_auto_build-arch:
	PYBUILD_BUILD_ARGS="-Ccompile-args=--verbose" dh_auto_build

override_dh_auto_build-indep: override_dh_auto_build-arch
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	dh_auto_build -- -s custom -p $(PY3VER) --build-args="cd doc && env PYTHONPATH={build_dir} {interpreter} -m sphinx -N -bhtml source build/html"
endif

override_dh_auto_test:
	dh_auto_test -- -s custom --test-args="PYTHONPATH={build_dir} WITH_QT_TEST=False PYFAI_TESTIMAGES=testimages PYFAI_DATA=$(shell pwd) xvfb-run -a --server-args=\"-screen 0 1024x768x24\" {interpreter} ./run_tests.py -v --low-mem --installed"

execute_after_dh_auto_install:
	dh_install -p pyfai debian/python3-pyfai/usr/bin /usr
	rm -rf debian/python3-pyfai/usr/bin

execute_after_dh_installdocs:
	dh_installdocs -p python-pyfai-doc "doc/build/html"

execute_before_dh_fixperms:
	# hack until the 1/1/1970 issue is solved by upstream
	-find debian/pyfai -type f | xargs touch -d@${SOURCE_DATE_EPOCH}
	-find debian/python3-pyfai -type f | xargs touch -d@${SOURCE_DATE_EPOCH}
	-find debian/python-pyfai-doc -type f | xargs touch -d@${SOURCE_DATE_EPOCH}
