#!/usr/bin/make -f

export PYBUILD_NAME=MACS3
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

include /usr/share/dpkg/default.mk

subcommands=callpeak bdgpeakcall bdgbroadcall bdgcmp bdgopt cmbreps bdgdiff \
	      filterdup predictd pileup randsample refinepeak
tagline=\- Model\-based Analysis for ChIP\-Sequencing
# DH_VERBOSE := 1

%:
	dh $@ --buildsystem=pybuild

execute_before_dh_python3:
	dh_numpy3

override_dh_auto_build:
	for pyx in $$(find MACS3/ -name "*.pyx"); do \
		rm -f $${pyx%%.pyx}.{h,c}; touch $${pyx}; done
	dh_auto_build
	PYTHONPATH=./.pybuild/cpython3_$$(py3versions -v -d)_MACS3/build/ help2man \
		--no-discard-stderr --no-info \
		--name "macs3 ${tagline}" "python3 bin/macs3" > debian/macs3.1
	for subcommand in ${subcommands}; do printf \
		"[NAME]\nmac3_$${subcommand} ${tagline}\n" \
		> debian/macs3_$${subcommand}-man-include; done
	for subcommand in ${subcommands}; do \
		PYTHONPATH=./.pybuild/cpython3_$$(py3versions -v -d)_MACS3/build/ help2man \
		--no-discard-stderr --no-info --version-string="$(DEB_VERSION_UPSTREAM)" \
		--include debian/macs3_$${subcommand}-man-include \
		"python3 bin/macs3 $${subcommand}" > \
		debian/macs3_$${subcommand}.1; done

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
ifeq (,$(filter $(DEB_HOST_ARCH),arm64 ppc64el s390x))
	cd test && for PY3VER in $(shell py3versions -vs); do \
	export PATH=$(CURDIR)/.pybuild/cpython3_$${PY3VER}_MACS3/scripts:$$PATH \
		&& export PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$${PY3VER}_MACS3/build \
		&& ./cmdlinetest macs$(DEB_VERSION)-$${PY3VER} ; done
endif
endif

override_dh_auto_clean:
	rm -Rf test/macs$(DEB_VERSION)* test/speedtest*
	rm -f debian/*.1 debian/*-man-include
