#!/usr/bin/make -f
# -*- makefile -*-

# The structure follows debian/rules of the python-opentelemetry and
# python-opentelemetry-contrib source packages: every upstream sub-project is
# built and installed in its own loop iteration. Refer to README.Debian!

#export DH_VERBOSE = 1

export TMP_DIST-PACKAGES_FOLDER=$(CURDIR)/.pybuild/dist-packages/build

# Sub-projects in the root of the upstream source.
PYTHON_LOGFIRE_ROOT_PACKAGES=\
logfire \
$(NULL)

# Sub-projects in a subdirectory with the same name.
PYTHON_LOGFIRE_PACKAGES=\
logfire-api \
$(NULL)

# Integration tests from tests/otel_integrations/ that can run, because the
# instrumentations and SDKs they need are in Debian (or the tests skip
# themselves when an optional SDK is missing). The other integrations need
# instrumentations or third-party SDKs that are not packaged yet, so their
# tests are ignored.
# Keep in sync with debian/tests/upstream-testsuite.
LOGFIRE_INTEGRATION_TESTS=\
aiohttp_client \
aiohttp_server \
anthropic \
anthropic_bedrock \
asgi \
aws_lambda \
claude_sdk \
django \
dspy \
fastapi \
flask \
httpx \
openai \
pydantic_ai_mcp \
pytest_plugin \
requests \
system_metrics \
wsgi \
$(NULL)

LOGFIRE_IGNORED_INTEGRATION_TESTS = $(filter-out \
	$(patsubst %,tests/otel_integrations/test_%.py,$(LOGFIRE_INTEGRATION_TESTS)), \
	$(wildcard tests/otel_integrations/test_*.py))

LOGFIRE_TEST_ARGS = tests \
	$(addprefix --ignore=,$(LOGFIRE_IGNORED_INTEGRATION_TESTS)) \
	--ignore=tests/test_variable_composition.py

export DJANGO_SETTINGS_MODULE = tests.otel_integrations.django_test_project.django_test_site.settings

%:
	dh $@

override_dh_clean:
	dh_clean
	rm -rf .pybuild
	find -type d -name ".pytest_cache" -exec rm -rf {} \; -prune
	find -type d -name "__pycache__" -exec rm -rf {} \; -prune

override_dh_auto_build:
	for PYTHON_LOGFIRE_ROOT_PACKAGE in $(PYTHON_LOGFIRE_ROOT_PACKAGES); do \
	    echo "\n########\n# Building package $${PYTHON_LOGFIRE_ROOT_PACKAGE}\n########" ;\
	    dh_auto_build --buildsystem=pybuild --sourcedirectory . -- --name=$${PYTHON_LOGFIRE_ROOT_PACKAGE} ;\
	done
	for PYTHON_LOGFIRE_PACKAGE in $(PYTHON_LOGFIRE_PACKAGES); do \
	    echo "\n########\n# Building package $${PYTHON_LOGFIRE_PACKAGE}\n########" ;\
	    dh_auto_build --buildsystem=pybuild --sourcedirectory $${PYTHON_LOGFIRE_PACKAGE} -- --name=$${PYTHON_LOGFIRE_PACKAGE} ;\
	done
	# The tests import both logfire and logfire_api, so we copy the builds
	# into one folder we can use as PYTHONPATH.
	mkdir .pybuild/dist-packages && \
	for FOLDER in `find .pybuild/cpython3_3.*_logfire*/ -maxdepth 1 -type d -name build`; do \
	    echo "Copy $${FOLDER} -> .pybuild/dist-packages" ;\
	    cp -a $${FOLDER} .pybuild/dist-packages ;\
	done

override_dh_auto_install:
	for PYTHON_LOGFIRE_ROOT_PACKAGE in $(PYTHON_LOGFIRE_ROOT_PACKAGES); do \
	    echo "\n########\n# Installing package $${PYTHON_LOGFIRE_ROOT_PACKAGE}\n########" ;\
	    dh_auto_install --buildsystem=pybuild --sourcedirectory . -- --name=$${PYTHON_LOGFIRE_ROOT_PACKAGE} ;\
	done
	for PYTHON_LOGFIRE_PACKAGE in $(PYTHON_LOGFIRE_PACKAGES); do \
	    echo "\n########\n# Installing package $${PYTHON_LOGFIRE_PACKAGE}\n########" ;\
	    dh_auto_install --buildsystem=pybuild --sourcedirectory $${PYTHON_LOGFIRE_PACKAGE} -- --name=$${PYTHON_LOGFIRE_PACKAGE} ;\
	done
	# Upstream ships instruction files for AI coding agents in logfire/.agents/.
	# Nothing in logfire reads them, so install them as documentation instead.
	# pybuild installs per Python version here (dh_python3 merges them later),
	# so move the first copy and drop the others.
	mkdir -p debian/python3-logfire/usr/share/doc/python3-logfire
	for AGENTS in debian/python3-logfire/usr/lib/python3*/dist-packages/logfire/.agents; do \
	    if [ ! -d debian/python3-logfire/usr/share/doc/python3-logfire/agent-skills ]; then \
	        mv $${AGENTS}/skills debian/python3-logfire/usr/share/doc/python3-logfire/agent-skills ;\
	    fi ;\
	    rm -rf $${AGENTS} ;\
	done
	test -d debian/python3-logfire/usr/share/doc/python3-logfire/agent-skills

# Keep the agent skill files plain Markdown: agents read them as they are, and
# the autopkgtest reads them by name.
override_dh_compress:
	dh_compress -X.md

# Only logfire itself has a test suite. It runs from the source root, because
# the tests also read docs/, scripts/, README.md, pyproject.toml and
# logfire-api/ relative to it.
#
# The CLI tests write to ~/.logfire, so HOME has to be writable, like pybuild
# does it for its own test step.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	for py in $(shell py3versions -s) ; do \
		mkdir -p $(CURDIR)/.pybuild/home; \
		set -e ;\
		for PYTHON_LOGFIRE_ROOT_PACKAGE in $(PYTHON_LOGFIRE_ROOT_PACKAGES); do \
		    echo "\n#######################\n# Run tests for $${PYTHON_LOGFIRE_ROOT_PACKAGE} with $$py\n#######################\n" ;\
		    cd $(CURDIR) ;\
		    HOME=$(CURDIR)/.pybuild/home PYTHONPATH=$(TMP_DIST-PACKAGES_FOLDER) $$py -m pytest $(LOGFIRE_TEST_ARGS) ;\
		done; \
		rm -rf $(CURDIR)/.pybuild/home; \
	done
endif

# Docs can't build for now due to missing pydantic_docs package.
# override_dh_auto_build:
# 	dh_auto_build
# ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
# 	mkdocs build -d $(CURDIR)/build/html -f $(CURDIR)/mkdocs.yml
# endif
#
# ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
# override_dh_installdocs:
# 	dh_installdocs
# 	dh_mkdocs -T mkdocs -T libjs-underscore -T mkdocs-material
# 	find -type f -name "sitemap.xml.gz" -delete
# endif
