PROG         := govpp-snmp-agentx
MODULE       := govpp-snmp-agentx
NATIVE_ARCH  := $(shell go env GOARCH)
VERSION      := ${shell head -1 debian/changelog | sed -n 's/.*(\([^)]*\)).*/\1/p'}
COMMIT_HASH  := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
DATE         := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS      := -s -w \
                -X 'main.Version=$(VERSION)' \
                -X 'main.Commit=$(COMMIT_HASH)' \
                -X 'main.Date=$(DATE)'

# CGO_ENABLED=0 produces a fully static binary: the same .deb runs on
# any glibc/musl Linux of the matching arch, and govpp itself has no
# cgo dependency, so this is a no-op for VPP API compatibility.
export CGO_ENABLED := 0

.PHONY: help all build build-amd64 build-arm64 test pkg-deb clean

help: ## Show this help
	@printf "Usage: make <target>\n\nTargets:\n"
	@awk -F ':.*## ' '/^[A-Za-z][A-Za-z0-9_-]*:.*## / {printf "  %-16s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

all: build ## Alias for build (native arch)

build: ## Build the binary for the host architecture
	mkdir -p build/$(NATIVE_ARCH)
	cd src && go build -ldflags "$(LDFLAGS)" -o ../build/$(NATIVE_ARCH)/$(PROG) .

build-amd64: ## Cross-build the binary for linux/amd64
	mkdir -p build/amd64
	cd src && GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ../build/amd64/$(PROG) .

build-arm64: ## Cross-build the binary for linux/arm64
	mkdir -p build/arm64
	cd src && GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o ../build/arm64/$(PROG) .

test: ## Run all Go unit tests
	cd src && go test ./...

pkg-deb: build-amd64 build-arm64 ## Build .deb packages for amd64 and arm64
	debian/build-deb.sh $(PROG) amd64 $(VERSION)
	debian/build-deb.sh $(PROG) arm64 $(VERSION)

clean: ## Remove build artifacts
	rm -rf build/
	rm -f $(PROG)
