#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }')
ifneq ($(findstring bpo,$(DEB_VERSION)),)
	FLAVOR := debian-backports
else ifneq ($(findstring pgdg,$(DEB_VERSION)),)
	FLAVOR := pgdg
	# for apt.postgresql.org builds, pull in the repository key package
	PGDG_DEPENDS := pgdg-keyring
else
	FLAVOR := default
endif
SUPPORTED_VERSIONS := 14
DEFAULT_VER := 14

#py2#PYTHON2_PACKAGE=postgresql-plpython-$v

%:
	dh $@

override_dh_builddeb:
	dh_builddeb -- -Zgzip

override_dh_auto_configure:
	@echo "### Building percona-postgresql-common flavor $(FLAVOR)"
	@echo "### Supported PostgreSQL versions: $(SUPPORTED_VERSIONS) (default version: $(DEFAULT_VER))"

# do not (re)start postgresql.service on postgresql-common install/upgrades
override_dh_installinit:
	dh_installinit -ppostgresql-common --name=postgresql -u'defaults 19 21' --no-start
override_dh_installsystemd:
	dh_installsystemd --no-start

override_dh_gencontrol:
	dh_gencontrol -ppercona-postgresql-server-dev-all -- -Vserver-dev-all-depends="$(foreach v,$(SUPPORTED_VERSIONS),percona-postgresql-server-dev-$v,)"

	# the versionless metapackages need to have version numbers which match
	# the server version, not the p-common version
	dh_gencontrol -ppercona-postgresql -ppercona-postgresql-client -ppercona-postgresql-doc -ppercona-postgresql-contrib -ppercona-postgresql-all -- \
		-Vdefault-version="$(DEFAULT_VER)" -v'$${source:Version}' \
		-Vpostgresql-all-depends="$(foreach v,$(SUPPORTED_VERSIONS),percona-postgresql-contrib-$v,percona-postgresql-plperl-$v,$(if $(filter 8.% 9.% 10 11, $v), $(PYTHON2_PACKAGE)),percona-postgresql-plpython3-$v,percona-postgresql-pltcl-$v,)"

	dh_gencontrol -ppostgresql-client-common -- \
		-Vpgdg:Depends="$(PGDG_DEPENDS)"

	dh_gencontrol -ppercona-postgresql-all -- \
		-Vpostgresql-all-depends="$(foreach v,$(SUPPORTED_VERSIONS),percona-postgresql-contrib-$v,percona-postgresql-plperl-$v,$(if $(filter 8.% 9.% 10 11, $v), $(PYTHON2_PACKAGE)),percona-postgresql-plpython3-$v,percona-postgresql-pltcl-$v,)"

	dh_gencontrol --remaining-packages
