#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export PYBUILD_NAME=datasette
export PYBUILD_DESTDIR_python3=debian/datasette
export PYBUILD_BEFORE_TEST=cp -r {dir}/docs {build_dir}
export PYBUILD_AFTER_TEST=rm -rf {build_dir}/docs

# Test exclusions:
# - test_black.py, test_docs.py: require black/sphinx modules not needed at runtime
# - test_publish_*.py: require network access
# - test_cli_serve_server.py: requires installed datasette binary
# - Individual tests using result.stderr: Click CliRunner compatibility issue
# - test_database_page: Unicode handling difference in build environment
export PYBUILD_TEST_ARGS=--ignore=tests/test_black.py \
	--ignore=tests/test_docs.py \
	--ignore=tests/test_publish_cloudrun.py \
	--ignore=tests/test_publish_heroku.py \
	--ignore=tests/test_cli_serve_server.py \
	-k 'not test_serve_invalid_ports \
	and not test_setting_type_validation \
	and not test_sql_errors_logged_to_stderr \
	and not test_error_on_config_json \
	and not test_crossdb_warning_if_too_many_databases \
	and not test_database_page'

%:
	dh $@ --buildsystem=pybuild

ifeq ($(DEB_HOST_ARCH_BITS),32)
override_dh_auto_test:
	# Reduce stack on 32bit archs to 1MB to avoid address space exhaustion
	ulimit -s 1024 && dh_auto_test
endif

execute_after_dh_auto_build:
	PYTHONPATH={build_dir} \
		help2man --no-info --no-discard-stderr \
		--name="multi-tool for exploring and publishing data" \
		--output=debian/datasette.1 \
		"python3 -m datasette"

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=$$(dirname $$(find .pybuild/ -type d -name "datasette*dist-info" | head -n1)) \
		python3 -m sphinx -b html docs/ -E -N \
		debian/datasette-doc/usr/share/doc/datasette-doc/html/
	dh_sphinxdoc
endif
