#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Set proper soname for the cdylib at compile time
export RUSTFLAGS = -C link-arg=-Wl,-soname,libblockdeviceid.so.1

# Convenience variables for package staging directories
pkg_bin     = debian/block-device-id
pkg_lib     = debian/libblockdeviceid1
pkg_dev     = debian/libblockdeviceid-dev
libdir      = usr/lib/$(DEB_HOST_MULTIARCH)

%:
	dh $@ --buildsystem=cargo

override_dh_auto_configure:
	dh_auto_configure
	# Generate cargo metadata using vendored registry for cbindgen
	# cbindgen needs metadata but will try to fetch from crates.io if not provided
	# Use CARGO_HOME=debian/cargo_home which contains config.toml that replaces crates.io
	# with the local Debian vendored registry (set up by dh_auto_configure)
	CARGO_HOME=$$(pwd)/debian/cargo_home \
		cargo metadata --format-version 1 --all-features --manifest-path Cargo.toml > debian/cargo-metadata.json
	# Generate C header using pre-generated metadata (cbindgen may create Cargo.lock, remove it)
	cbindgen --config cbindgen.toml --crate block-device-id --metadata debian/cargo-metadata.json --output block_device_id.h
	rm -f Cargo.lock

override_dh_auto_test:
	dh_auto_test
	# Verify C example compiles and links against generated header/library
	# Note: dh_auto_test uses debug builds, so library is in target/*/debug/
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -I. \
		-o debian/test-c-example examples/get_device_id.c \
		-L target/*/debug -l:libblock_device_id.so
	# Smoke test: verify binary runs (can't test real devices in build env)
	LD_LIBRARY_PATH=target/*/debug debian/test-c-example || true
	rm -f debian/test-c-example

override_dh_auto_install:
	dh_auto_install

	# --- block-device-id: CLI binary ---
	install -d $(pkg_bin)/usr/bin
	mv $(pkg_dev)/usr/bin/block-device-id $(pkg_bin)/usr/bin/
	rmdir $(pkg_dev)/usr/bin
	# Install man page from build output directory
	install -d $(pkg_bin)/usr/share/man/man1
	find target -name "block-device-id.1" -exec install -m 644 {} $(pkg_bin)/usr/share/man/man1/ \;

	# --- libblockdeviceid1: shared library ---
	install -d $(pkg_lib)/$(libdir)
	install -m 644 target/*/release/libblock_device_id.so \
		$(pkg_lib)/$(libdir)/libblockdeviceid.so.1.0.0
	ln -s libblockdeviceid.so.1.0.0 $(pkg_lib)/$(libdir)/libblockdeviceid.so.1

	# --- libblockdeviceid-dev: development files ---
	install -d $(pkg_dev)/$(libdir)
	ln -s libblockdeviceid.so.1 $(pkg_dev)/$(libdir)/libblockdeviceid.so
	install -d $(pkg_dev)/usr/include
	install -m 644 block_device_id.h $(pkg_dev)/usr/include/
	install -d $(pkg_dev)/usr/share/doc/libblockdeviceid-dev/examples
	install -m 644 examples/get_device_id.c examples/Makefile \
		$(pkg_dev)/usr/share/doc/libblockdeviceid-dev/examples/

override_dh_makeshlibs:
	# Only process the shared library package (block-device-id has no libs)
	dh_makeshlibs -p libblockdeviceid1

override_dh_shlibdeps:
	dh_shlibdeps -l $(pkg_lib)/$(libdir)

override_dh_gencontrol:
	# Generate Built-Using/Static-Built-Using for packages with compiled Rust
	# (not libblockdeviceid-dev: it only contains generated header + symlink)
	/usr/share/cargo/bin/dh-cargo-built-using block-device-id
	/usr/share/cargo/bin/dh-cargo-built-using libblockdeviceid1
	# Clear unused substvars for -dev package to suppress warnings
	sed -i '/^cargo:/d' $(pkg_dev).substvars 2>/dev/null || true
	dh_gencontrol
