#!/usr/bin/make -f

export DH_VERBOSE=1

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_VERSION ?= $(shell dpkg-parsechangelog -SVersion)

# Ensure we use cargo/rust 1.85 or later.
RUSTLANG_NATIVE_VERSION = $(shell dpkg-query --showformat='${Version}' --show rustc 2>/dev/null || echo 0)
ifneq (ok,$(shell dpkg --compare-versions $(RUSTLANG_NATIVE_VERSION) ge 1.85 && echo ok))
	export PATH := /usr/lib/rust-1.85/bin:$(PATH)
endif

# Check Qt features.
QT_INSTALL_HEADERS := $(shell pkg-config --variable=includedir Qt6Core || qmake6 -query QT_INSTALL_HEADERS || true)
qtfeature = $(shell test -d "$(QT_INSTALL_HEADERS)" && echo "$(1)" | gcc -I "$(QT_INSTALL_HEADERS)" -include QtCore/qconfig.h -E - | grep -e '^[[:alnum:]_-]')
ifeq (1,$(call qtfeature,QT_FEATURE_static))
ifeq (1,$(call qtfeature,QT_FEATURE_openssl_linked))
	OPENSSL_BUILD_ARGS := -DOPENSSL_USE_STATIC_LIBS=ON
endif
endif

export SYSTEMD_UNIT_PATH := $(shell (pkg-config --variable=systemdsystemunitdir systemd || echo "/lib/systemd/system") | sed 's/^[/]//g')

%:
	dh $@ --buildsystem=cmake+ninja --builddirectory=build-$(DEB_HOST_MULTIARCH) --warn-missing

override_dh_clean:
	dh_clean --exclude=3rdparty

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_CXX_STANDARD=17 \
		-DWEBEXT_INSTALL_LIBDIR=/lib \
		-DBUILD_ID=$(DEB_VERSION) \
		-DBUILD_TESTS=OFF \
		-DBUILD_APPARMOR=ON \
		${OPENSSL_BUILD_ARGS}

override_dh_auto_test:

override_dh_install:
	dh_install
	dh_apparmor --profile-name=usr.bin.mozillavpn -p mozillavpn

override_dh_installdocs:

override_dh_installinfo:

override_dh_builddeb:
	dh_builddeb -- -Zgzip

override_dh_dwz:
