#!/usr/bin/make -f

# Enable verbose build output
export DH_VERBOSE = 1

# Use release profile for cargo
export CARGO_BUILD_JOBS = $(shell nproc)

# Rust flags for reproducible builds
export RUSTFLAGS = -C debuginfo=0

# Set CARGO_HOME to a writable location for cargo's cache
# The vendored config is in .cargo/config.toml (project-level)
export CARGO_HOME = $(CURDIR)/debian/.cargo-home

%:
	dh $@

override_dh_auto_build:
	@echo "=== Build environment ==="
	@echo "PWD: $(CURDIR)"
	@echo "Checking for vendor directory..."
	@ls -la $(CURDIR)/vendor/ | head -5 || echo "No vendor directory!"
	@echo "Checking for .cargo/config.toml..."
	@cat $(CURDIR)/.cargo/config.toml || echo "No .cargo/config.toml!"
	@echo "Cargo version:"
	@cargo --version
	@echo "Rustc version:"
	@rustc --version
	@echo "=== Starting build ==="
	cargo build --release --offline

override_dh_auto_install:
	# Install binary
	install -D -m 755 target/release/teleddns debian/teleddns/usr/bin/teleddns
	# Install systemd service
	install -D -m 644 teleddns.service debian/teleddns/lib/systemd/system/teleddns.service
	# Install man page
	install -D -m 644 teleddns.1 debian/teleddns/usr/share/man/man1/teleddns.1
	# Install documentation
	install -D -m 644 README.md debian/teleddns/usr/share/doc/teleddns/README.md
	install -D -m 644 teleddns.yaml.sample debian/teleddns/usr/share/doc/teleddns/teleddns.yaml.sample
	# Create config directory
	install -d debian/teleddns/etc/teleddns

override_dh_auto_test:
	# Skip tests during package build (requires network for some tests)
	@echo "Skipping tests"

override_dh_auto_clean:
	cargo clean || true
	rm -rf debian/.cargo-home || true
	dh_auto_clean

override_dh_clean:
	dh_clean -Xvendor/

override_dh_compress:
	dh_compress -X.yaml.sample
