#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export V=1

PG_CONFIG=pg_config
PGBINDIR=$(shell $(PG_CONFIG) --bindir)

ifeq ($(DEB_HOST_ARCH_OS),linux)
        SYSTEMD = --with-systemd
endif

%:
	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:
	[ ! -f config.mak ] || $(MAKE) distclean
	rm -f doc/pgbouncer.?

override_dh_autoreconf:

override_dh_auto_configure:
	./autogen.sh
	dh_auto_configure -- \
		--bindir=/usr/sbin \
		--with-cares=yes \
		--with-pam \
		--with-ldap\
		$(SYSTEMD)

# "make check" broken in 1.12: https://github.com/pgbouncer/pgbouncer/issues/431
override_dh_auto_test:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	$(MAKE) -C test check PATH=$(PGBINDIR):$(PATH)
#endif

override_dh_auto_install:
	dh_auto_install --destdir=debian/percona-pgbouncer
	rm -f $(addprefix $(CURDIR)/debian/percona-pgbouncer/usr/share/doc/pgbouncer/,pgbouncer.ini userlist.txt)
	mkdir -p $(CURDIR)/debian/percona-pgbouncer/etc/pgbouncer
	touch $(CURDIR)/debian/percona-pgbouncer/etc/pgbouncer/userlist.txt

override_dh_installinit:
	dh_installinit -R

override_dh_installsystemd:
	dh_installsystemd pgbouncer.service

#dbg#override_dh_strip:
#dbg#	dh_strip --dbg-package=percona-pgbouncer-dbg
