#!/usr/bin/make -f

# Plain debhelper: no dh-golang dependency, manual Go build commands.
# The go build links against golang-gopkg-yaml.v3-dev and validates
# the shipped tools.yaml via tests (AGENTS.md §7).

VERSION := $(shell dpkg-parsechangelog -S Version)

export DH_VERBOSE = 1
export DEB_BUILD_OPTIONS = noautodbgsym
export HOME := $(CURDIR)/debian/fakehome

%:
	dh $@

# Build the nest binary with version injected from changelog.
# GOPATH points to a temp dir; GOFLAGS=-mod=vendor is not needed
# since golang-gopkg-yaml.v3-dev is installed in the system GOPATH.
override_dh_auto_build:
	mkdir -p $(CURDIR)/.gopath
	GOPATH=$(CURDIR)/.gopath \
	CGO_ENABLED=0 \
	go build \
		-ldflags "-X main.version=$(VERSION) -w -s" \
		-o nest \
		./src

# Run tests against the shipped tools.yaml (AGENTS.md §7).
override_dh_auto_test:
	GOPATH=$(CURDIR)/.gopath \
	CGO_ENABLED=0 \
	go test ./src/...

# Install binary and catalog; strip debug symbols.
override_dh_auto_install:
	install -Dm755 nest $(CURDIR)/debian/nest/usr/bin/nest
	install -Dm644 tools.yaml $(CURDIR)/debian/nest/usr/share/nest/tools.yaml

# Strip the binary (dh_strip handles this, but we also strip in build).
override_dh_strip:
	dh_strip --no-automatic-dbgsym

# Clean up build artifacts.
override_dh_auto_clean:
	rm -f nest
	rm -rf $(CURDIR)/.gopath
