#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=+lto
export DEB_BUILD_OPTIONS += noddebs nocheck
export BUILD_UPSTREAM_VERSION := $(shell dpkg-parsechangelog --file "debian/changelog" --show-field Version | sed 's/^.*://; s/~[^~-]*$$//')
export BUILD_VERSION := $(shell dpkg-parsechangelog --file "debian/changelog" --show-field Version | sed 's/^.*://')
export ORIGINAL_DIR := $(shell pwd)
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

webengine_archs := amd64 arm64 armhf i386
ifeq ($(DEB_HOST_ARCH), $(filter $(DEB_HOST_ARCH),$(webengine_archs)))
BUILD_WEBENGINE := ON
else
BUILD_WEBENGINE := OFF
endif

override_dh_auto_clean:
	@echo "Downloading upstream files for '$(BUILD_UPSTREAM_VERSION)'..."
	@if ! curl -L -o "/tmp/upstream-files.tar.gz" "https://github.com/nextcloud/desktop/archive/refs/tags/v$(BUILD_UPSTREAM_VERSION).tar.gz"; then \
	    echo "No tar found for this version '$(BUILD_UPSTREAM_VERSION)'" \
	    exit 1; \
	fi
	mkdir -p /tmp/nextcloud-desktop/
	@tar -xzvf "/tmp/upstream-files.tar.gz" -C "/tmp/nextcloud-desktop/"; \
	cd "/tmp/nextcloud-desktop/" && \
	tar -acvf "nextcloud-desktop_$(BUILD_VERSION).orig.tar.xz" "desktop-$(BUILD_UPSTREAM_VERSION)/"
	cd "$(ORIGINAL_DIR)" && \
	mv "/tmp/nextcloud-desktop/nextcloud-desktop_$(BUILD_VERSION).orig.tar.xz" "../" && \
	cp -rpv "/tmp/nextcloud-desktop/desktop-$(BUILD_UPSTREAM_VERSION)/". "./"
	dh_auto_clean

CONFIGURE_OPTIONS := -DCMAKE_INSTALL_PREFIX=/usr \
	-DBUILD_UPDATER=OFF \
	-DPLUGINDIR=/usr/lib/$(DEB_HOST_GNU_TYPE)/qt6/plugins \
	-DEXTERNAL_VERSION=$(DEB_VERSION)

%:
	dh $@

override_dh_auto_configure:
ifeq (-i,$(findstring -i, $(DH_INTERNAL_OPTIONS)))
	@echo " -- Configure for arch all only build --"
	dh_auto_configure -- \
		-DBUILD_TESTING=OFF \
		-DBUILD_SHELL_INTEGRATION_DOLPHIN=OFF \
		-DBUILD_WITH_WEBENGINE=OFF \
		$(CONFIGURE_OPTIONS)
else
	dh_auto_configure -- \
		-DUNIT_TESTING=ON \
		-DBUILD_WITH_WEBENGINE=$(BUILD_WEBENGINE) \
		$(CONFIGURE_OPTIONS)
endif
execute_before_dh_auto_build-arch:
	@echo "blhc: ignore-line-regexp: .*/CMakeCXXCompilerABI.cpp.*"
	#tests are not built with all hardening, to debug stuff more easily
	@echo "blhc: ignore-line-regexp: .*Test.dir/.*"
	@echo "blhc: ignore-line-regexp: .*/check_.*.dir/.*"
	@echo "blhc: ignore-line-regexp: .*/test/csync/torture_.*"
	@echo "blhc: ignore-line-regexp: .*/test/.*"
	@echo "blhc: ignore-line-regexp: .*/LargeSyncBench\.dir/.*"

# unfortunately icons and translations are installed only together with the gui, so we need to run dh_auto_build
execute_before_dh_auto_build-indep:
	dh_auto_build -- -C shell_integration/nautilus

override_dh_auto_test-arch:
	mkdir -p obj-$(DEB_HOST_GNU_TYPE)/config
	- LC_ALL=C.UTF-8 XDG_CONFIG_HOME=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)/config xvfb-run -a \
	       -s "-screen 0 1024x768x24 +extension RANDR +extension RENDER +extension GLX" \
	       -- dh_auto_test --no-parallel
	rm -r obj-$(DEB_HOST_GNU_TYPE)/config

override_dh_auto_test-indep:

execute_after_dh_sphinxdoc:
	$(RM) -v debian/tmp/usr/share/doc/client/nextcloud-client/html/unthemed/.buildinfo

execute_after_dh_link-indep:
	jdupes -rl debian/nextcloud-desktop-doc/usr
