#!/usr/bin/make -f

ifeq (,$(filter terse,${DEB_BUILD_OPTIONS}))
export DH_VERBOSE=1
export V=1
export VERBOSE=1
endif

LC_ALL:=C.UTF-8
export LC_ALL

shellescape='$(subst ','\'',$(1))'
shellexport=$(1)=$(call shellescape,${$(1)})

include /usr/share/dpkg/buildtools.mk

OUR_CPPFLAGS:=
OUR_CFLAGS:=
OUR_LDFLAGS:=

ifneq (,$(filter debug,${DEB_BUILD_OPTIONS}))
OUR_CFLAGS+=	-Og -g3
endif

OUR_CPPFLAGS+=	-DPAX_SAFE_PATH=\"/usr/bin\"
OUR_CFLAGS+=	-Wall -Wextra -Wformat -fno-strict-aliasing

dpkgbuildflagsmkescape=$(subst \,\\\,$(1))
export DEB_BUILD_MAINT_OPTIONS:=hardening=+all optimize=-lto
export DEB_CPPFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_CPPFLAGS})
export DEB_CFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_CFLAGS})
export DEB_LDFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_LDFLAGS})
include /usr/share/dpkg/buildflags.mk

%:
	dh $@ --buildsystem=none

execute_before_dh_auto_clean:
	rm -rf builddir

override_dh_auto_build:
	rm -rf builddir
	mkdir builddir
	(cd builddir; env \
	    $(foreach i,CC CPPFLAGS CFLAGS LDFLAGS,$(call shellexport,$i)) \
	    sh ../Build.sh -r -tpax; \
	    echo $$? >Build.rv \
	) 2>&1 | sed \
	    -e 's!conftest.c:\([0-9]*\(:[0-9]*\)*\): error:!cE(\1) -!g' \
	    -e 's!conftest.c:\([0-9]*\(:[0-9]*\)*\): warning:!cW(\1) -!g' \
	    -e 's!conftest.c:\([0-9]*\(:[0-9]*\)*\): note:!cN(\1) -!g'
	test x"$$(cat builddir/Build.rv 2>&1)" = x"0"

execute_before_dh_installdeb:
	if command -v dh_movetousr >/dev/null 2>&1; then dh_movetousr; fi