#!/usr/bin/make -f

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

%:
	dh $@

override_dh_builddeb:
	dh_builddeb -- -Zgzip
	# Ubuntu's dh_builddeb renames auto-dbgsym .deb -> .ddeb (so they would be
	# routed to ddebs.ubuntu.com) and records the renamed name in debian/files.
	# OBS's apt-repo publisher only indexes *.deb, so undo both: rename the
	# files on disk AND rewrite debian/files, otherwise dpkg-genbuildinfo /
	# dpkg-genchanges fail to fstat the .ddeb names listed there. No-op on
	# Debian.
	if ls ../*-dbgsym_*.ddeb >/dev/null 2>&1; then \
	  for f in ../*-dbgsym_*.ddeb; do \
	    mv -v "$$f" "$${f%.ddeb}.deb"; \
	  done; \
	  sed -i 's/\(-dbgsym_[^ ]*\)\.ddeb/\1.deb/g' debian/files; \
	fi

override_dh_auto_clean:
	rm -rf build

override_dh_auto_configure:

override_dh_auto_build:
	make USE_PGXS=1 \
	CFLAGS="$(pkg-config --cflags openssl) $$CFLAGS" \
	LDFLAGS_EX="$$LDFLAGS_EX -L/usr/lib/postgresql/18/lib \
	/usr/lib/postgresql/18/lib/libpgcommon.a \
	/usr/lib/postgresql/18/lib/libpgport.a \
	/usr/lib/postgresql/18/lib/libpgfeutils.a \
	-lpq -lcurl $$(pkg-config --libs openssl)"

override_dh_auto_install:
	make USE_PGXS=1 install DESTDIR=$(CURDIR)/debian/tmp \
	CFLAGS="$(pkg-config --cflags openssl) $$CFLAGS" \
	LDFLAGS_EX="$$LDFLAGS_EX -L/usr/lib/postgresql/18/lib \
	/usr/lib/postgresql/18/lib/libpgcommon.a \
	/usr/lib/postgresql/18/lib/libpgport.a \
	/usr/lib/postgresql/18/lib/libpgfeutils.a \
	-lpq -lcurl $$(pkg-config --libs openssl)"
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/postgresql/18/lib/pgxs/src/test/perl/PostgreSQL/Test
	install -m 644 ci_scripts/perl/PostgreSQL/Test/TdeCluster.pm $(CURDIR)/debian/tmp/usr/lib/postgresql/18/lib/pgxs/src/test/perl/PostgreSQL/Test/

override_dh_auto_test:

override_dh_installdocs:
	dh_installdocs --all README.*
