MODULE  := gitea.mynodes.xyz/ilker/llmgw
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
LDFLAGS := -X '$(MODULE)/internal/version.Version=$(VERSION)'

.PHONY: build test race lint fmt vuln check clean rename proto proto-tools

build:
	go build -ldflags "$(LDFLAGS)" -o bin/llmgw ./cmd/llmgw
	go build -ldflags "$(LDFLAGS)" -o bin/llmgw-kb ./cmd/llmgw-kb

test:
	go test ./...

race:
	go test -race ./...

lint:
	golangci-lint run

fmt:
	go fmt ./...
	go vet ./...

# Known vulnerabilities reachable from this code, in the standard library and
# in dependencies. A toolchain left behind is the commonest cause: the go
# directive in go.mod is what fixes it.
vuln:
	go run golang.org/x/vuln/cmd/govulncheck@latest ./...

# What has to pass before a change is merged.
check: test
	go vet ./...
	$(MAKE) vuln

# Regenerate the agent protocol. Generated code is committed, so this only
# needs running when api/proto changes.
proto: proto-tools
	buf lint
	buf generate

proto-tools:
	go install github.com/bufbuild/buf/cmd/buf@latest
	go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	go install connectrpc.com/connect/cmd/protoc-gen-connect-go@latest

clean:
	rm -rf bin dist

# Change the module path:  make rename NEW=git.example.com/team/llmgw
rename:
	@test -n "$(NEW)" || (echo "usage: make rename NEW=<new/module/path>"; exit 1)
	go mod edit -module=$(NEW)
	grep -rl '$(MODULE)' --include='*.go' . | xargs sed -i '' 's|$(MODULE)|$(NEW)|g'
	sed -i '' 's|^MODULE  := .*|MODULE  := $(NEW)|' Makefile
	go build ./...
