#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_BUILD_OPTIONS := nostrip nocheck
export DEB_BUILD_OPTIONS

PG_BUILDEXT = pg_buildext
export PG_UPDATECONTROL=1

PG_VERSIONS = $$($(PG_BUILDEXT) supported-versions . )

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

%:
	dh $@

override_dh_auto_clean:
	dh_auto_clean -- clean || exit 0

override_dh_auto_install: 
	mkdir -p $(CURDIR)/debian/tmp
	for v in $(PG_VERSIONS); do \
		echo "### Building for PostgreSQL $${v}" && \
		$(MAKE) clean install \
			PG_CONFIG=/usr/lib/postgresql/$${v}/bin/pg_config \
			DESTDIR=$(CURDIR)/debian/tmp \
		|| exit 1 ; \
	done

genfiles:
	rm -f debian/control
	rm -rf debian/postgresql-*
	set -e; for ver in $(PG_VERSIONS); do \
		xmod=pgq; \
		dst="debian/postgresql-$${ver}-$${xmod}.install"; \
		echo "usr/share/postgresql/$${ver}/extension" > "$${dst}"; \
		echo "usr/share/postgresql/$${ver}/contrib" >> "$${dst}"; \
		echo "usr/lib/postgresql/$${ver}/lib" >> "$${dst}"; \
	done
	$(PG_BUILDEXT) updatecontrol

debclean: clean
	rm -f debian/control
	rm -rf debian/postgresql-*

