#! /usr/bin/make -f

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

PROJECT_UPSTREAM_VERSION := $(firstword $(subst +, ,$(subst ~,+,$(DEB_VERSION_UPSTREAM))))
export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_RDIFFMOUNT := $(PROJECT_UPSTREAM_VERSION)

export PYBUILD_NAME=rdiffmount
export PYTHONDONTWRITEBYTECODE=1
export PYBUILD_TEST_PYTEST=1
export PYBUILD_TEST_ARGS=-p no:cacheprovider tests

PYTHON3_DEFAULT := $(shell py3versions -vd)

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

execute_after_dh_auto_clean:
	rm -rf src/*.egg-info
	rm -f debian/rdiffmount-gdrive.bash-completion

override_dh_auto_install:
	dh_auto_install
	mkdir -p debian/tmp
	mv debian/python3-rdiffmount/usr debian/tmp/

execute_after_dh_installman:
	mkdir -p debian/rdiffmount-gdrive/usr/share/man/man1

	PYTHONPATH=debian/tmp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages:$(PYTHONPATH) \
	argparse-manpage \
		--module rdiffmount.gdrive \
		--function _get_parser \
		--version "$(DEB_VERSION_UPSTREAM)" \
		--output debian/rdiffmount-gdrive/usr/share/man/man1/rdiffmount-gdrive.1

execute_before_dh_bash-completion:
	PYTHONPATH=debian/tmp/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages:$(PYTHONPATH) \
	shtab \
		--shell=bash \
		--error-unimportable rdiffmount.gdrive._get_parser \
		--prog rdiffmount \
	> debian/rdiffmount-gdrive.bash-completion

gentarball:
	# Generate orig.tar.xz
	git archive --format=tar HEAD --prefix=$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/ ":!debian" ":!.gitlab-ci.yml" | xz > ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz
