#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export CARGO_HOME=$(CURDIR)/debian/cargo_home

ALLOW_MULTIPLE=-Wl,--allow-multiple-definition

define QMAKE_BUILDFLAGS
QMAKE_CPPFLAGS *= $(shell dpkg-buildflags --get CPPFLAGS)
QMAKE_CFLAGS   *= $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
QMAKE_CXXFLAGS *= $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
QMAKE_LFLAGS   *= $(shell dpkg-buildflags --get LDFLAGS) $(if $(findstring arm,$(shell uname -m)),$(ALLOW_MULTIPLE))
endef
export QMAKE_BUILDFLAGS

# for cargo
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export PREFIX=/usr
export CONFIGDIR=/etc
export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)

%:
	dh $@

execute_before_dh_auto_configure:
	/usr/share/cargo/bin/cargo prepare-debian debian/cargo_registry --link-from-system

execute_after_dh_auto_install:
	/usr/share/cargo/bin/dh-cargo-built-using pushpin

override_dh_auto_configure:
	# Upstream Cargo.lock contains versions not valid for debian
	-rm Cargo.lock
	cargo update

override_dh_auto_install:
	dh_auto_install -- INSTALL_ROOT="$(CURDIR)/debian/pushpin"
	mkdir -p debian/pushpin/var/log/pushpin
	mkdir -p debian/pushpin/usr/share/pushpin
	mv debian/pushpin/etc/pushpin/pushpin.conf debian/pushpin/usr/share/pushpin/pushpin.conf

override_dh_installexamples:
	dh_installexamples -X.o -XMakefile

override_dh_installman:
	help2man \
		--name "HTTP/WebSocket connection manager" \
		--no-info \
		debian/pushpin/usr/bin/pushpin-connmgr > debian/pushpin-connmgr.1
	ln -s pushpin-connmgr.1 debian/pushpin-condure.1
	dh_installman -O--buildsystem=cargo
