BINARY  := registry_mcp
TAGS    := fts5
GOFLAGS := CGO_ENABLED=1
ROOT    := $(shell git rev-parse --show-toplevel 2>/dev/null || cd ../.. && pwd)

.PHONY: build test clean install-claude-code install-claude-desktop

build:
	$(GOFLAGS) go build -tags $(TAGS) -o $(BINARY) .

test:
	$(GOFLAGS) go test -tags $(TAGS) -count=1 ./...

clean:
	rm -f $(BINARY) registry.db registry.db-shm registry.db-wal

install-claude-code: build
	@if [ ! -f .mcp.json ]; then echo '{ "mcpServers": {} }' > .mcp.json; fi
	@python3 -c "import json,sys,os; \
p='.mcp.json'; d=json.load(open(p)); \
d.setdefault('mcpServers',{})['registry']={'command':os.path.abspath('$(BINARY)'),'args':['--enable-run','--enable-write'],'env':{'FN_REGISTRY_ROOT':'$(ROOT)'}}; \
json.dump(d,open(p,'w'),indent=2)"
	@echo "Wrote .mcp.json with registry MCP entry."

install-claude-desktop: build
	@cfg=$$(if [ "$$(uname)" = "Darwin" ]; then echo "$$HOME/Library/Application Support/Claude/claude_desktop_config.json"; else echo "$$HOME/.config/Claude/claude_desktop_config.json"; fi); \
	mkdir -p "$$(dirname $$cfg)"; \
	[ -f "$$cfg" ] || echo '{ "mcpServers": {} }' > "$$cfg"; \
	python3 -c "import json,sys,os; \
p=os.environ['CFG']; d=json.load(open(p)); \
d.setdefault('mcpServers',{})['registry']={'command':os.path.abspath('$(BINARY)'),'env':{'FN_REGISTRY_ROOT':'$(ROOT)'}}; \
json.dump(d,open(p,'w'),indent=2)" CFG=$$cfg; \
	echo "Updated $$cfg with registry MCP entry."
