#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

# verbose build output (antimake.mk)
export V=1

PG_CONFIG=pg_config
PGBINDIR=$(shell $(PG_CONFIG) --bindir)

ifeq ($(DEB_HOST_ARCH_OS),linux)
	SYSTEMD = --with-systemd
endif

%:
	dh $@

override_dh_auto_clean:
	[ ! -f config.mak ] || $(MAKE) distclean

override_dh_autoreconf:
	dh_autoreconf ./autogen.sh

override_dh_auto_configure:
	dh_auto_configure -- \
		--bindir=/usr/sbin \
		--with-cares=yes \
		--with-pam \
		--with-ldap \
		$(SYSTEMD)

# On buildd we run a small smoke test subset instead of the full upstream
# suite.  The full suite has proven to be far too flaky on the slower buildd
# architectures (s390x, sparc64, ppc64, loong64, alpha, riscv64, ...): with
# each release a different timing- or load-sensitive test failed, and the
# failures moved around between builds of the same package.
#
# The tests below are one per basic protocol surface and have not failed in
# any buildd build since 1.25.0-1:
#
#   test_connect_query  - startup/listener, client login, routing to the
#                         server, connect_query GUC propagation, shutdown
#   test_scram_client   - client -> pgbouncer SCRAM authentication, including
#                         rejection of wrong passwords
#   test_scram_server   - pgbouncer -> server SCRAM authentication
#   test_show           - admin console (separate listener, no database)
#
# Deliberately left out are the areas that flake on slow machines: SSL,
# prepared statement pipelines, pool/idle timeouts, notifications and load
# balancing.  They remain covered by the full suite, which can still be run
# manually ("make check").
SMOKE_TESTS = \
	test/test_misc.py::test_connect_query \
	test/test_auth.py::test_scram_client \
	test/test_auth.py::test_scram_server \
	test/test_admin.py::test_show

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	LANG=C.UTF-8 $(MAKE) check \
	     PATH=$(PGBINDIR):$(PATH) \
	     CONCURRENCY=1 \
	     PYTEST_FLAGS="-r s $(SMOKE_TESTS)"
endif

override_dh_auto_install:
	dh_auto_install --destdir=debian/pgbouncer
	rm -f $(addprefix $(CURDIR)/debian/pgbouncer/usr/share/doc/pgbouncer/,pgbouncer.ini userlist.txt)
	mkdir -p $(CURDIR)/debian/pgbouncer/etc/pgbouncer
	touch $(CURDIR)/debian/pgbouncer/etc/pgbouncer/userlist.txt

override_dh_installsystemd:
	# don't auto-enable/start pgbouncer.socket
	dh_installsystemd pgbouncer.service
