#!/usr/bin/make -f

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

# Strip everything after + (plus). e.g. +dfsg.2-1
PROJECT_UPSTREAM_VERSION := $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))
ifeq ($(DEB_DISTRIBUTION),UNRELEASED)
	# If UNRELEASED, append .dev
	export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CHERRYPY_FOUNDATION := $(PROJECT_UPSTREAM_VERSION).dev
else
	export SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CHERRYPY_FOUNDATION := $(PROJECT_UPSTREAM_VERSION)
endif

# Check if +dfsg build
DFSG_BUILD := $(findstring dfsg,$(DEB_VERSION_UPSTREAM))

# Default to running selenium tests
export SELENIUM_TEST ?= 1

# Disable or enable datatable test if "+dfsg" or not.
export DATATABLE_TEST := $(if $(DFSG_BUILD),0,1)

# Exclude or include vendor files if "+dfsg" or not.
ifneq (,$(DFSG_BUILD))
export VENDOR_EXCLUDE = ":!src/cherrypy_foundation/components/vendor"
endif

# 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 && \
	mkdir -p {build_dir}/cherrypy_foundation/components/vendor && \
	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
ifneq (,$(DFSG_BUILD))
	# Clean lintian overrides for dfgs.
	rm -rf debian/source/lintian-overrides
endif

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
ifeq ($(SELENIUM_TEST),1)
	TZ=UTC xvfb-run dh_auto_test
else
	TZ=UTC dh_auto_test
endif

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 original tarball
	git archive --format=tar HEAD --prefix=$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/ ":!debian" $(VENDOR_EXCLUDE) | xz > ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz
