#!/usr/bin/make -f

MANTEMPLATE="${CURDIR}/doc/man/pgbackrest.1.txt"

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

#export DH_VERBOSE = 1

%:
	dh $@ --buildsystem=meson

override_dh_builddeb:
	dh_builddeb -- -Zgzip

override_dh_auto_configure:
	# src contains a separate configure script.
	dh_auto_configure -- --prefix=/usr --bindir=/usr/bin

override_dh_auto_build:
	# Official release tarballs ship pre-rendered docs under doc/man and doc/html.
	# Git checkouts do not, so pull them from the matching GitHub release when missing.
	mkdir -p ${CURDIR}/doc/man
	if [ ! -f ${MANTEMPLATE} ]; then \
		VERSION=$$(sed -n "s/^[[:space:]]*version: '\([^']*\)'.*/\1/p" meson.build | head -1); \
		wget -qO- "https://github.com/pgbackrest/pgbackrest/releases/download/release/$${VERSION}/pgbackrest-$${VERSION}.tar.gz" \
		| tar -xzf - --strip-components=1 -C ${CURDIR} --wildcards '*/doc/man/*' '*/doc/html/*'; \
	fi
	txt2man \
	  -v "pgBackRest Command Reference" \
	  -t pgbackrest \
	  -s 1 \
	  ${MANTEMPLATE} > ${CURDIR}/doc/man/pgbackrest.1
	dh_auto_build

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

override_dh_auto_clean:
	! [ -f src/Makefile ] || $(MAKE) -C src clean-all

# pgBackRest must be configured before the TLS server can be started.
# Moreover a user must configure valid CA, client and server certificates.
override_dh_installsystemd:
	dh_installsystemd --no-enable --no-start --restart-after-upgrade
