#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export CARGO_HOME = $(CURDIR)/debian/cargo_home

%:
	dh $@

override_dh_auto_build:
	# Build AVX2 baseline binary (compatible with most CPUs from 2013+)
	# Disable AVX-512 via compiler flags (WHISPER_NO_AVX512 doesn't work with whisper-rs-sys 0.14.1)
	CMAKE_C_FLAGS="-mno-avx512f" CMAKE_CXX_FLAGS="-mno-avx512f" cargo build --release --locked
	cp target/release/voxtype target/release/voxtype-avx2
	# Build AVX-512 optimized binary (for Zen 4+, some Intel)
	cargo clean
	cargo build --release --locked
	cp target/release/voxtype target/release/voxtype-avx512

override_dh_auto_install:
	# Install tiered binaries to /usr/lib/voxtype/
	# The postinst script creates a symlink at /usr/bin/voxtype
	install -D -m 755 target/release/voxtype-avx2 debian/voxtype/usr/lib/voxtype/voxtype-avx2
	install -D -m 755 target/release/voxtype-avx512 debian/voxtype/usr/lib/voxtype/voxtype-avx512
	# Install default configuration
	install -D -m 644 config/default.toml debian/voxtype/etc/voxtype/config.toml
	# Install documentation
	install -D -m 644 README.md debian/voxtype/usr/share/doc/voxtype/README.md
	install -D -m 644 docs/INSTALL.md debian/voxtype/usr/share/doc/voxtype/INSTALL.md
	# Install shell completions
	install -D -m 644 packaging/completions/voxtype.bash \
		debian/voxtype/usr/share/bash-completion/completions/voxtype
	install -D -m 644 packaging/completions/voxtype.zsh \
		debian/voxtype/usr/share/zsh/vendor-completions/_voxtype
	install -D -m 644 packaging/completions/voxtype.fish \
		debian/voxtype/usr/share/fish/vendor_completions.d/voxtype.fish

override_dh_auto_test:
	# Only test with AVX2 build to avoid SIGILL in build environments
	CMAKE_C_FLAGS="-mno-avx512f" CMAKE_CXX_FLAGS="-mno-avx512f" cargo test --release --locked

override_dh_installsystemd:
	dh_installsystemd --user --name=voxtype

override_dh_auto_clean:
	cargo clean || true
	rm -rf debian/cargo_home

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
