#!/usr/bin/make -f

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

export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CHERRYPY_FOUNDATION := $(DEB_VERSION_UPSTREAM)

# Force use of pyproject.toml
export PYBUILD_SYSTEM=pyproject

export PYBUILD_NAME=cherrypy-foundation

PYTHON3_DEFAULT := $(shell py3versions -vd)

# Create vendor files symlink for tests.
export PYBUILD_BEFORE_TEST = \
	rm -rf {build_dir}/cherrypy_foundation/components/vendor/bootstrap5 && \
	rm -rf {build_dir}/cherrypy_foundation/components/vendor/jquery && \
	rm -rf {build_dir}/cherrypy_foundation/components/vendor/jquery-typeahead && \
	rm -rf {build_dir}/cherrypy_foundation/components/vendor/popper && \
	ln -sf /usr/share/javascript/bootstrap5 {build_dir}/cherrypy_foundation/components/vendor/bootstrap5 && \
	ln -sf /usr/share/javascript/jquery {build_dir}/cherrypy_foundation/components/vendor/jquery && \
	ln -sf /usr/share/javascript/jquery-typeahead {build_dir}/cherrypy_foundation/components/vendor/typeahead && \
	ln -sf /usr/share/javascript/popperjs2 {build_dir}/cherrypy_foundation/components/vendor/popper

# Remove vendor files from install.
export PYBUILD_AFTER_INSTALL = \
	rm -rf {destdir}/{install_dir}/cherrypy_foundation/components/vendor/bootstrap5 && \
	rm -rf {destdir}/{install_dir}/cherrypy_foundation/components/vendor/jquery && \
	rm -rf {destdir}/{install_dir}/cherrypy_foundation/components/vendor/typeahead && \
	rm -rf {destdir}/{install_dir}/cherrypy_foundation/components/vendor/popper

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

execute_after_dh_auto_clean:
	rm -rf *.egg-info
	# Clean Sphinx build artifacts
	rm -rf build/html docs/_build

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. python3 -m sphinx $(SPHINXOPTS) -N -bhtml doc/ \
		debian/python3-cherrypy-foundation-doc/usr/share/doc/python3-cherrypy-foundation-doc/html/
	dh_sphinxdoc
endif

override_dh_auto_test:
	# For testing, we need a timezone
	TZ=UTC xvfb-run dh_auto_test

execute_after_dh_auto_install:
	# Remove tests files
	find debian/python3-cherrypy-foundation/usr/lib/python$(PYTHON3_DEFAULT)/dist-packages/cherrypy_foundation -type d -name "tests" -exec rm -rf {} +

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
