#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

# For DEB_HOST_ARCH_OS
include /usr/share/dpkg/architecture.mk

# For proper pkg-config
DPKG_EXPORT_BUILDTOOLS = 1
include /usr/share/dpkg/buildtools.mk

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

CPPFLAGS+=$(shell $(PKG_CONFIG) --cflags nss)

ifeq (linux,$(DEB_HOST_ARCH_OS))
       configure_flags +=--with-cap --with-selinux
endif

configure_flags += \
	--datadir=/usr/share \
	--with-external-db \
	--with-lua \
	--with-debian \
	--with-vendor=debian \
	--enable-shared \
	--enable-python \
	LDFLAGS="$(LDFLAGS) -Wl,--as-needed" \
	CPPFLAGS="$(CPPFLAGS)"

override_dh_auto_configure:
	dh_auto_configure -- $(configure_flags)
	cd python && ln -s ../debian

override_dh_auto_build:
	dh_auto_build
	cd python && dh_auto_build --buildsystem=pybuild
	if which help2man >/dev/null 2>&1; then \
	  help2man \
	    --no-info \
	    --section=8 \
	    --name='Debuginfo editing helper' \
	    --version-string="$$(./debugedit --version | sed 's/RPM *//')" \
	    --include=debian/debugedit.h2m ./debugedit \
	    > debian/debugedit.8; \
	fi

override_dh_auto_install:
	dh_auto_install
	# Remove Python, we use distutils built modules
	rm -rf debian/tmp/usr/lib/python*
	cd python && dh_auto_install --buildsystem=pybuild
	# Kill all the junk this installs.
	rm -f debian/tmp/usr/lib/rpm/config.guess \
		debian/tmp/usr/lib/rpm/config.sub \
		debian/tmp/usr/lib/*/*.la \
		debian/tmp/usr/lib/*/rpm-plugins/*.la \
		debian/tmp/usr/lib/rpm/rpm.log
	# Fix permissions
	chmod +x debian/tmp/usr/lib/rpm/rpm.daily

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	dh_auto_clean
	rm -rf python/build
	rm -rf python/.pybuild
	rm -f python/debian

%:
	dh $@
