#!/usr/bin/make -f

export DH_VERBOSE=1
export DEB_BUILD_OPTIONS=nocheck
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

export pkgname=linphone-desktop
export _topdir=/usr/src/packages
export _builddir=$(_topdir)/BUILD
export _sourcedir=$(_topdir)/SOURCES
export srcdir=$(_sourcedir)
export _uploaddir=$(_sourcedir)
export uploaddir=$(_uploaddir)
export pkgdir_head=/usr/src/packages/BUILD/debian
export pkgdir=$(pkgdir_head)/tmp
export buildroot=$(pkgdir)

export ispell_commit_hash=05574fe160222c3d0b6283c1433c9b087271fad1

ISPELL_TARBALL := $(_uploaddir)/ispell-$(ispell_commit_hash).tar.gz

DEB_VERSION_UPSTREAM := $(shell \
	dpkg-parsechangelog -S Version | \
	sed 's/-[^-]*$$//' \
)

DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

LINPHONE_OUTPUT_ROOT := $(CURDIR)/debian/linphone-desktop$(CURDIR)/build/OUTPUT
ISPELL_OUTPUT_ROOT := $(CURDIR)/build/OUTPUT

%:
	dh $@ --buildsystem=cmake --builddirectory=build

override_dh_auto_configure:
	@echo "Configuring Linphone from: $(CURDIR)"
	@echo "Ispell archive: $(ISPELL_TARBALL)"
	@echo "Debian architecture: $(DEB_HOST_MULTIARCH)"

	# Replace the bundled Ispell tree.
	rm -rf "$(CURDIR)/external/ispell"
	mkdir -p "$(CURDIR)/external/ispell"

	tar -xzf "$(ISPELL_TARBALL)" \
		--strip-components=1 \
		-C "$(CURDIR)/external/ispell"

	# Apply the Linphone-specific patch.
	if [ -f "$(_uploaddir)/fix_ispell_return_type_error.patch" ]; then \
		patch -d "$(CURDIR)/external/ispell" -p1 \
			< "$(_uploaddir)/fix_ispell_return_type_error.patch"; \
	fi

	# The generated Linphone install rule expects this directory to exist.
	# It can be empty for the desktop package.
	#mkdir -p "$(CURDIR)/external/ispell/ispell_dictionaries"

	# Prepare the output directories expected by Linphone.
	mkdir -p "$(CURDIR)/build/OUTPUT"
	mkdir -p "$(CURDIR)/build/linphone-sdk/desktop/bin"
	mkdir -p "$(CURDIR)/build/linphone-sdk/desktop/share"

	# The source archive is not built from Git.
	touch "$(CURDIR)/linphone-sdk/CMakeLists.txt"

	# Provide the version normally generated from Git.
	printf '%s\n' \
		'#define LINPHONE_QT_GIT_VERSION "$${PROJECT_VERSION}"' \
		>> "$(CURDIR)/linphone-app/src/config.h.cmake"

	printf '%s\n' \
		'project(linphoneqt VERSION $(DEB_VERSION_UPSTREAM))' \
		> "$(CURDIR)/linphone-app/linphoneqt_version.cmake"

	# Prevent Git-submodule find modules from shadowing installed packages.
	rm -f \
		"$(CURDIR)/linphone-app/cmake/FindMediastreamer2.cmake" \
		"$(CURDIR)/linphone-app/cmake/FindLibLinphone.cmake" \
		"$(CURDIR)/linphone-app/cmake/FindLinphoneCxx.cmake"

	# Configure and install Ispell first.
	cmake -S "$(CURDIR)/external/ispell" \
		-B "$(CURDIR)/build/ispell" \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX="$(CURDIR)/build/OUTPUT" \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_C_STANDARD=99 \
		-DCMAKE_C_EXTENSIONS=ON \
		-DCMAKE_C_FLAGS="-std=gnu99" \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON

	cmake --build "$(CURDIR)/build/ispell" --parallel "$$(nproc)"
	cmake --install "$(CURDIR)/build/ispell"

	# Provide the Ispell package configuration expected by Linphone.
	mkdir -p "$(CURDIR)/build/OUTPUT/lib/cmake/ISpell"

	printf '%s\n' \
		'set(ISpell_FOUND TRUE)' \
		'' \
		'if(NOT TARGET ISpell)' \
		'    add_library(ISpell SHARED IMPORTED GLOBAL)' \
		'    set_target_properties(ISpell PROPERTIES' \
		'        IMPORTED_LOCATION "$${CMAKE_CURRENT_LIST_DIR}/../../libISpell.so"' \
		'        INTERFACE_INCLUDE_DIRECTORIES "$(CURDIR)/external/ispell"' \
		'    )' \
		'endif()' \
		'' \
		'set(ISpell_TARGET ISpell)' \
		> "$(CURDIR)/build/OUTPUT/lib/cmake/ISpell/ISpellConfig.cmake"

	test -f \
		"$(CURDIR)/build/OUTPUT/lib/cmake/ISpell/ISpellConfig.cmake"

	# Configure Linphone.
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_SKIP_RPATH=ON \
		-DLINPHONE_OUTPUT_DIR="$(CURDIR)/build/OUTPUT" \
		-DLINPHONEAPP_VERSION="$(DEB_VERSION_UPSTREAM)" \
		-DLINPHONESDK_VERSION="$(DEB_VERSION_UPSTREAM)" \
		-DLINPHONESDK_STATE="snapshots" \
		-DLINPHONESDK_BRANCH="-alpha.384" \
		-DLINPHONE_QT_ONLY=ON \
		-DENABLE_BUILD_VERBOSE=ON \
		-DENABLE_QT_KEYCHAIN=OFF \
		-DENABLE_UPDATE_CHECK=OFF \
		-DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
		-DISpell_DIR="$(CURDIR)/build/OUTPUT/lib/cmake/ISpell" \
		-DCMAKE_PREFIX_PATH="$(CURDIR)/build/OUTPUT"

	# Print the actual generated rule responsible for the failure.
	@echo "Generated Ispell installation rules:"
	@grep -RInF 'ispell_dictionaries' \
		"$(CURDIR)/build/linphone-app/cmake_builder" \
		"$(CURDIR)/linphone-app/cmake_builder" || true

override_dh_auto_build:
	dh_auto_build -- -j$$(nproc)

override_dh_auto_install:
	# Remove the optional Ispell dictionary installation command.
	find "$(CURDIR)/build" \
		-type f \
		-name "cmake_install.cmake" \
		-print0 \
		| xargs -0 -r sed -i \
		-e '\|file(INSTALL.*ispell_dictionaries|d'

	if grep -RInF "ispell_dictionaries" "$(CURDIR)/build" \
		--include="cmake_install.cmake"; then \
		echo "ERROR: dictionary install rule remains" >&2; \
		exit 1; \
	fi

	dh_auto_install

	# The Linphone CMake files install into:
	#   debian/linphone-desktop$(CURDIR)/build/OUTPUT
	#
	# Ispell is installed into the non-DESTDIR output tree:
	#   $(CURDIR)/build/OUTPUT
	#
	# Re-stage both trees in the normal Debian location.
	rm -rf "$(CURDIR)/debian/tmp"
	mkdir -p "$(CURDIR)/debian/tmp/usr"

	OUTPUT_ROOT="$(LINPHONE_OUTPUT_ROOT)"; \
	test -d "$$OUTPUT_ROOT" || { \
		echo "ERROR: Linphone output directory not found: $$OUTPUT_ROOT" >&2; \
		exit 1; \
	}; \
	\
	if test -d "$$OUTPUT_ROOT/bin"; then \
		mkdir -p "$(CURDIR)/debian/tmp/usr/bin"; \
		cp -a "$$OUTPUT_ROOT/bin/." \
			"$(CURDIR)/debian/tmp/usr/bin/"; \
	fi; \
	\
	if test -d "$$OUTPUT_ROOT/share"; then \
		mkdir -p "$(CURDIR)/debian/tmp/usr/share"; \
		cp -a "$$OUTPUT_ROOT/share/." \
			"$(CURDIR)/debian/tmp/usr/share/"; \
	fi; \
	\
	mkdir -p "$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)"; \
	\
	if test -f "$$OUTPUT_ROOT/lib/libISpell.so"; then \
		cp -a "$$OUTPUT_ROOT/lib/libISpell.so" \
			"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/"; \
	elif test -f "$(ISPELL_OUTPUT_ROOT)/lib/libISpell.so"; then \
		cp -a "$(ISPELL_OUTPUT_ROOT)/lib/libISpell.so" \
			"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/"; \
	else \
		echo "ERROR: libISpell.so not found" >&2; \
		exit 1; \
	fi; \
	\
	if test -f "$$OUTPUT_ROOT/lib/libapp-plugin.so"; then \
		cp -a "$$OUTPUT_ROOT/lib/libapp-plugin.so" \
			"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/"; \
	else \
		echo "ERROR: libapp-plugin.so not found" >&2; \
		exit 1; \
	fi; \
	\
	if test -d "$$OUTPUT_ROOT/lib"; then \
		find "$$OUTPUT_ROOT/lib" -mindepth 1 -maxdepth 1 \
			-type d \
			-exec cp -a {} \
			"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/" \; ; \
	fi; \
	\
	if test -d "$$OUTPUT_ROOT/include"; then \
		mkdir -p "$(CURDIR)/debian/tmp/usr/include"; \
		cp -a "$$OUTPUT_ROOT/include/." \
			"$(CURDIR)/debian/tmp/usr/include/"; \
	fi

	# Confirm the required libraries were staged correctly.
	test -f \
		"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libISpell.so"
	test -f \
		"$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libapp-plugin.so"

	# Show the files that dh_install will consume.
	find "$(CURDIR)/debian/tmp" \
		\( -type f -o -type l \) \
		-print | sort

override_dh_install:
	dh_install --sourcedir=debian/tmp

execute_after_dh_auto_install:
	echo "--- Start execute_after_dh_auto_install ---"
	echo pkgdir=${pkgdir}
	echo "--- Files that will be packaged ---"
	find $(buildroot) | sed "s|${pkgdir}||g" | sort
	echo "--- End files that will be packaged ---"
	echo "--- End execute_after_dh_auto_install ---"

override_dh_auto_clean:
	dh_auto_clean
	rm -rf external/ispell
	rm -f linphone-app/linphoneqt_version.cmake
