#!/usr/bin/make -f

include /usr/share/postgresql-common/pgxs_debian_control.mk

override_dh_auto_build:
	+pg_buildext build build-%v

override_dh_auto_clean:
	+pg_buildext clean build-%v

override_dh_auto_test:
	# nothing to do here, see debian/tests/* instead

override_dh_auto_configure:
	+pg_buildext configure build-%v

override_dh_auto_install:
	+pg_buildext install build-%v postgresql-%v-citus

	# Install alternatives for our binaries. Could simply link to pg_wrapper,
	# but there's no harm in just using the binary installed with the latest
	# version. Below inspired by a similar block in pg_repack's installer.
	set -ex; \
	binaries="csql copy_to_distributed_table"; \
	for v in $(shell pg_buildext supported-versions); do \
		i=$$(echo $$v | tr -d .); \
		for bin in $$binaries; do \
			echo "update-alternatives --install /usr/bin/$$bin citus-$$bin /usr/lib/postgresql/$$v/bin/$$bin $$i" >> debian/postgresql-$$v-citus.postinst.debhelper; \
			echo "update-alternatives --remove citus-$$bin /usr/lib/postgresql/$$v/bin/$$bin" >> debian/postgresql-$$v-citus.prerm.debhelper; \
		done \
	done

%:
	dh $@
