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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Skip building the unit-test binaries (build.sh forwards CMAKE_EXTRA_ARGS to cmake).
export CMAKE_EXTRA_ARGS = -DBUILD_UNIT_TESTS=OFF
# Use the Unix Makefiles generator (make), so the build doesn't depend on
# ninja-build — keeps the toolchain uniform with the RPM build.
export CMAKE_GENERATOR = Unix Makefiles

# valkey-search needs a C++20 compiler (g++ >= 12). Prefer g++-12 when present
# (e.g. Ubuntu jammy ships g++ 11 by default); bookworm/noble default g++ is >= 12.
GXX12 := $(shell command -v g++-12 2>/dev/null)
ifneq ($(GXX12),)
export CC = gcc-12
export CXX = g++-12
endif

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:
	# build.sh builds ICU from source, FetchContent-builds gRPC/Protobuf/Abseil/
	# highwayhash, then compiles the module -> .build-release/libsearch.so.
	./build.sh --jobs=$$(nproc)

override_dh_auto_install:
	install -d debian/percona-valkey-search/usr/lib/valkey/modules
	install -m 0755 .build-release/libsearch.so \
	    debian/percona-valkey-search/usr/lib/valkey/modules/libsearch.so
	# Embedded SBOM set (SPDX/CycloneDX json+xml, Syft table, license manifest),
	# generated from the built tree by the shipped gen-module-sbom.sh. Runs AFTER
	# build.sh, so the statically-linked .build-release/.deps (gRPC, Protobuf,
	# Abseil, ICU) are cataloged; fails loudly if Syft is missing (installed by
	# valkey_builder.sh --search_deps).
	sh gen-module-sbom.sh percona-valkey-search $$(dpkg-parsechangelog -S Version 2>/dev/null || echo 0) \
	    debian/percona-valkey-search/usr/share/percona-valkey-search/sbom .

override_dh_fixperms:
	dh_fixperms
	# Valkey refuses to load a module without the execute bit; dh_fixperms
	# normalises the .so back to 0644, so restore 0755.
	chmod 0755 debian/percona-valkey-search/usr/lib/valkey/modules/libsearch.so

# The module build runs no tests during packaging.
override_dh_auto_test:

# Build the .deb with xz (not the zstd default on Ubuntu jammy+/Debian bookworm+):
# the Percona repo's reprepro can't read zstd-compressed control.tar/data.tar.
override_dh_builddeb:
	dh_builddeb -- -Zxz
