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

NULL =

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Only enable the GPS provider if this is an Ubuntu build with "native" packaging
SOURCE_FORMAT = $(shell dpkg-source --print-format . | tr -d "3.0() ")
BUILD_FOR_UBUNTU = $(shell dpkg-vendor --derives-from Ubuntu)

ENABLE_GPS_PROVIDER = OFF
ifneq ($(SOURCE_FORMAT), quilt)
ifeq ($(shell dpkg-vendor --query vendor),Ubuntu)
	ENABLE_GPS_PROVIDER = ON
endif
endif

%:
	dh $@

override_dh_install:
	rm -Rv debian/tmp/usr/lib/*/lomiri-location-service/examples/
	rm -Rv debian/tmp/usr/share/lomiri-location-service/examples/
	cd debian/tmp/usr/share/doc/lomiri-location-service/html/ && rdfind -makesymlinks true -makeresultsfile false .
	cd debian/tmp/usr/share/doc/lomiri-location-service/html/ && symlinks -rc .
	dh_install -Xlomiri-location-service.1
ifeq ($(filter $(ENABLE_GPS_PROVIDER),ON),ON)
	dh_install -p liblomiri-location-service3 etc/gps.conf etc/
endif

override_dh_auto_configure:
	if [ ! -e po/lomiri-location-service.pot.deb-bak ]; then cp po/lomiri-location-service.pot po/lomiri-location-service.pot.deb-bak; fi
	dh_auto_configure --builddirectory=build-qt5 -- \
	        -DCMAKE_INSTALL_LIBEXECDIR=/usr/lib/$(DEB_HOST_MULTIARCH)/lomiri-location-service \
	        -DENABLE_TRUST_STORE=$(if $(filter native,$(SOURCE_FORMAT)),ON,OFF) \
	        -DLOCATION_SERVICE_ENABLE_GPS_PROVIDER=$(ENABLE_GPS_PROVIDER) \
	        -DLOCATION_SERVICE_ENABLE_GEOCLUE_PROVIDER=$(if $(filter native,$(SOURCE_FORMAT)),OFF,ON) \
	        -DENABLE_QT6=OFF \
	        $(NULL)
	dh_auto_configure --builddirectory=build-qt6 -- \
	        -DCMAKE_INSTALL_LIBEXECDIR=/usr/lib/$(DEB_HOST_MULTIARCH)/lomiri-location-service \
	        -DENABLE_TRUST_STORE=$(if $(filter native,$(SOURCE_FORMAT)),ON,OFF) \
	        -DLOCATION_SERVICE_ENABLE_GPS_PROVIDER=$(ENABLE_GPS_PROVIDER) \
	        -DLOCATION_SERVICE_ENABLE_GEOCLUE_PROVIDER=$(if $(filter native,$(SOURCE_FORMAT)),OFF,ON) \
	        -DENABLE_QT6=ON \
	        $(NULL)

override_dh_auto_build:
	dh_auto_build --builddirectory=build-qt5
	dh_auto_build --builddirectory=build-qt6

# Don't run tests in parallel as fake web servers all bind to same port
override_dh_auto_test:
	dh_auto_test --builddirectory=build-qt5 --no-parallel -- ARGS+="-E '^(delayed_service_test|ichnaea_reporter_test|remote_providerd_test)'"
	dh_auto_test --builddirectory=build-qt6 --no-parallel -- ARGS+="-E '^(delayed_service_test|ichnaea_reporter_test|remote_providerd_test)'"

override_dh_auto_install:
	dh_auto_install --builddirectory=build-qt5
	dh_auto_install --builddirectory=build-qt6

execute_before_dh_clean:
	-mv po/lomiri-location-service.pot.deb-bak po/lomiri-location-service.pot

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
