#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_BFH_SSP := $(DEB_VERSION_UPSTREAM)

# Force use of pyproject.toml
export PYBUILD_SYSTEM=pyproject

PYTHON3_DEFAULT := $(shell py3versions -vd)

# Create vendor files symlink for tests.
export PYBUILD_BEFORE_TEST = \
	rm -rf {build_dir}/bfh_science_ssp/components/vendor/clipboard && \
	ln -sf /usr/share/nodejs/clipboard/dist {build_dir}/bfh_science_ssp/components/vendor/clipboard

# Remove vendor files from install.
export PYBUILD_AFTER_INSTALL = \
	rm -rf {destdir}/{install_dir}/bfh_science_ssp/components/vendor/clipboard

%:
	dh ${@} --buildsystem=pybuild --test-pytest --with bash-completion

execute_after_dh_auto_clean:
	rm -rf *.egg-info
	rm -f debian/bfh-ssp.bash-completion

override_dh_auto_test:
	# For testing, we need a timezone and a display for Javascript tests.
	TZ=UTC xvfb-run dh_auto_test

execute_after_dh_auto_install:
	# Compile locales
	for pofile in debian/bfh-ssp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages/bfh_science_ssp/locales/*/LC_MESSAGES/*.po; \
	do \
		mofile=$$(echo $$pofile | sed -e "s|.po|.mo|g"); \
		msgfmt $$pofile -o $$mofile; \
	done

	# Remove tests files
	find debian/bfh-ssp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages/bfh_science_ssp -type d -name "tests" -exec rm -rf {} +

execute_after_dh_bash-completion:
	# Generate bash-completions with shtab
	PYTHONPATH=debian/bfh-ssp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages:$(PYTHONPATH) \
	shtab --shell=bash -u bfh_science_ssp.config.get_parser --error-unimportable --prog bfh-ssp \
		> debian/bfh-ssp.bash-completion

execute_after_dh_installman:
	# Generate man page from argparse
	mkdir -p debian/bfh-ssp/usr/share/man/man1

	PYTHONPATH=debian/bfh-ssp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages:$(PYTHONPATH) \
	argparse-manpage --module bfh_science_ssp.config --function get_parser \
		--version "$(DEB_VERSION_UPSTREAM)" \
		--output debian/bfh-ssp/usr/share/man/man1/bfh-ssp.1

gentarball:
	# Generate orig.tar.gz
	git archive --format=tar HEAD --prefix=$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/ | gzip -9 > ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz
	mk-origtargz --compression gzip ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz
