#!/usr/bin/make -f

VERSION := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2 \
		| cut -d- -f1)

# Enable compiler hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow,+pie

# Force linking with -lpthread, working around a bug in libtool that drops
# the linkage because it uses -nostdlib.  See #468555.  Add --as-needed to
# drop a few unnecessary dependencies.
export DEB_LDFLAGS_MAINT_APPEND = -lpthread -Wl,--as-needed

# Set the results of dpkg-buildflags as make variables.  We don't use this
# to communicate to dh_auto_configure; it calls dpkg-buildflags itself.
# It's only used to strip those flags out of the pkgconfig file.  If we
# can't import this, we don't care.
-include /usr/share/dpkg/buildflags.mk

DOCS = debian/liblog4shib-doc/usr/share/doc/liblog4shib-doc

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    DEBUG = --enable-debug
else
    DEBUG =
endif

override_dh_auto_configure:
	dh_auto_configure -- --disable-dependency-tracking $(DEBUG)

# Remove the upstream-installed documentation, and strip the build flags out
# of the Cflags and Libs pkgconfig configuration.  LDFLAGS and CPPFLAGS are
# set by /usr/share/dpkg/buildflags.mk above.
override_dh_install:
	pod2man --release=$(VERSION) --section=1 --center=log4shib \
	    debian/log4shib-config.pod debian/log4shib-config.1
	dh_install -a -i -Xlog4shib.la -Xman3 --fail-missing
	perl -i -pe 's/^(Libs: .*)\Q$(LDFLAGS)\E/$$1/' \
	    debian/*/usr/lib/*/pkgconfig/log4shib.pc
	perl -i -pe 's/^(Cflags: .*)\Q$(CPPFLAGS)\E/$$1/' \
	    debian/*/usr/lib/*/pkgconfig/log4shib.pc

override_dh_installdocs:
	dh_installdocs
	set -e; if [ -d "$(DOCS)" ] ; then				\
	    if [ -f "$(DOCS)/html/api/jquery.js" ] ; then		\
		rm $(DOCS)/html/api/jquery.js ;				\
		ln -s /usr/share/javascript/jquery/jquery.min.js	\
		    $(DOCS)/html/api/jquery.js ;			\
	    fi ;							\
	fi

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_builddeb:
	dh_builddeb -- -Zxz

%:
	dh $@ --with autoreconf

# Don't attempt to build the buildflags.mk file if it doesn't exist.
/usr/share/dpkg/buildflags.mk:
