VERSION := $(shell ./scripts/read-version.sh)
BINARY  := ivoxd
# macOS codesign identity（个人证书，其他 contributor 可留空或替换）
SIGN_ID ?= 4A287668E97BC130AA6D19F4D64799394CAACBAD

.PHONY: build test deploy release clean help

help:
	@echo "iVox $(VERSION)"
	@echo ""
	@echo "  make build      # 编译"
	@echo "  make test       # 测试"
	@echo "  make deploy     # 编译 + 安装到 ~/.local/bin/ + 重启"
	@echo "  make release    # 发布到 npm"
	@echo "  make clean      # 清理"

build:
	@mkdir -p build
	@rm -rf py/ivox_tts/__pycache__
	go build -ldflags="-s -w -X main.version=$(VERSION)" -o build/$(BINARY) ./go/...

test:
	go test -count=1 ./go
	go build -o /dev/null ./go
	bash scripts/test-hook-speak.sh
	PYTHONPATH=py python3 -m compileall py

deploy: build
	@mkdir -p $$HOME/.local/bin
	@cp build/$(BINARY) $$HOME/.local/bin/$(BINARY)
	@mkdir -p $$HOME/.config/ivox/py
	@rm -rf $$HOME/.config/ivox/py/ivox_tts
	@cp -R py/ivox_tts $$HOME/.config/ivox/py/ivox_tts
	@codesign --remove-signature $$HOME/.local/bin/$(BINARY) 2>/dev/null; true
	@codesign -s $(SIGN_ID) -f --identifier com.xdfnet.ivoxd $$HOME/.local/bin/$(BINARY) 2>/dev/null || true
	@pkill -f $(BINARY) 2>/dev/null; true

release: test
	git push origin HEAD
	npm publish --access public

clean:
	rm -rf build
