#!/usr/bin/make -f

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

LDFLAGS += -Wl,--as-needed

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) -C src/sql clean
	rm -f config.log
	$(MAKE) -C src/sql/pgpool_adm clean USE_PGXS=1
	rm -rf src/test/jdbc/*.class src/test/jdbc/result src/test/jdbc/pgpool.properties
	# tests disabled because they have been broken in commit cdb49d3
	#$(MAKE) -C src/test/parser clean
	#rm -rf src/test/parser/result src/test/parser/test.diff
	#$(MAKE) -C src/test/timestamp clean
	#rm -rf src/test/timestamp/result src/test/timestamp/test.diff
	cd src/test/regression && bash clean.sh
	rm -rf debian/postgresql-*-pgpool2/ debian/*.substvars

override_dh_auto_configure:
	autoreconf --force --install; ./configure --prefix=/usr \
		--sysconfdir=/etc/pgpool2 \
		--bindir=/usr/sbin \
		--includedir=/usr/include/pgpool2 \
		--disable-rpath \
		--with-ldap \
		--with-openssl \
		--with-pam \
		--with-memcached=/usr/include/libmemcached
	# create directory here so we can unlink it in case autopkgtest leaves files owned by root behind
	mkdir -p src/test/jdbc/result

override_dh_auto_build:
	$(MAKE)
	$(MAKE) -C doc

override_dh_auto_test:
	dh_auto_test
	# tests disabled because they have been broken in commit cdb49d3
	#$(MAKE) -C src/test/parser test
	#chmod +x src/test/timestamp/run-test
	#$(MAKE) -C src/test/timestamp test
	# src/test/jdbc is executed by autopkgtest

override_dh_auto_install:
	dh_auto_install
	set -e ; \
	for v in `pg_buildext supported-versions .` ; do \
		echo "# $$v ##################################################################" ; \
		PG_CONFIG=/usr/lib/postgresql/$$v/bin/pg_config ; \
		$(MAKE) -C src/sql clean PG_CONFIG=$$PG_CONFIG ; \
		$(MAKE) -C src/sql PG_CONFIG=$$PG_CONFIG ; \
		chrpath -d src/sql/pgpool_adm/pgpool_adm.so ; \
		$(MAKE) -C src/sql install DESTDIR=$(CURDIR)/debian/postgresql-$$v-pgpool2 PG_CONFIG=$$PG_CONFIG ; \
	done

override_dh_install:
	# pgpool.conf is handled by ucf
	mkdir -p debian/tmp/usr/share/pgpool2
	mv debian/tmp/etc/pgpool2/pgpool.conf.sample debian/tmp/usr/share/pgpool2/pgpool.conf
	set -e ; for f in pcp.conf pool_hba.conf ; do mv debian/tmp/etc/pgpool2/$$f.sample debian/tmp/etc/pgpool2/$$f ; done
	rm -vf debian/tmp/usr/lib/libpgpoolpcp.la # not interested in .la
	dh_install

override_dh_installchangelogs:
	dh_installchangelogs NEWS

execute_before_dh_installdocs:
	sed -i -e "s/CONTENT=\"20..-..-..T..:..:..\"/CONTENT=\"$(shell date -ud "@$$SOURCE_DATE_EPOCH" "+%Y-%m-%dT%H:%M:%S")\"/" doc/src/sgml/html/*.html

override_dh_builddeb:
	dh_builddeb -- -Zgzip

%:
	dh $@
