LECTIO     := lectio
LECTIO_UI  := lectio-ui
LECTIO_WEB := lectio-web
PREFIX   ?= $(HOME)/.local
BINDIR   := $(PREFIX)/bin
MANDIR   := $(PREFIX)/share/man/man1

.PHONY: help build build-full install install-full uninstall test vet fmt clean cross check-corpora add-corpus ci release install-hooks
help: ## show this help
	@grep -hE '^[a-z-]+:.*##' $(MAKEFILE_LIST) | sed -E 's/:.*## /\t/' | sort
build: ## build all three binaries (embeds only the Vulgate)
	go build -o $(LECTIO) ./cmd/lectio
	go build -o $(LECTIO_UI) ./cmd/lectio-ui
	go build -o $(LECTIO_WEB) ./cmd/lectio-web
build-full: ## build all three with the optional corpora (wuj, drb, grb) embedded
	go build -tags fullbible -o $(LECTIO) ./cmd/lectio
	go build -tags fullbible -o $(LECTIO_UI) ./cmd/lectio-ui
	go build -tags fullbible -o $(LECTIO_WEB) ./cmd/lectio-web
install: ## build and install the three binaries and man pages
	@mkdir -p $(BINDIR) $(MANDIR)
	go build -o $(BINDIR)/$(LECTIO) ./cmd/lectio
	go build -o $(BINDIR)/$(LECTIO_UI) ./cmd/lectio-ui
	go build -o $(BINDIR)/$(LECTIO_WEB) ./cmd/lectio-web
	install -m 644 man/lectio.1 man/lectio-ui.1 man/lectio-web.1 $(MANDIR)
	@echo "installed binaries to $(BINDIR) and man pages to $(MANDIR)"
install-full: ## like install, but embeds the optional corpora (wuj, drb, grb) in all three
	@mkdir -p $(BINDIR) $(MANDIR)
	go build -tags fullbible -o $(BINDIR)/$(LECTIO) ./cmd/lectio
	go build -tags fullbible -o $(BINDIR)/$(LECTIO_UI) ./cmd/lectio-ui
	go build -tags fullbible -o $(BINDIR)/$(LECTIO_WEB) ./cmd/lectio-web
	install -m 644 man/lectio.1 man/lectio-ui.1 man/lectio-web.1 $(MANDIR)
	@echo "installed FULL binaries (wuj/drb/grb embedded) to $(BINDIR); man pages to $(MANDIR)"
	@echo "note: wuj/drb/grb are third-party scripture; do not redistribute these binaries where that is not permitted (see NOTICE)"
uninstall: ## remove installed binaries and man pages
	rm -f $(BINDIR)/$(LECTIO) $(BINDIR)/$(LECTIO_UI) $(BINDIR)/$(LECTIO_WEB)
	rm -f $(MANDIR)/lectio.1 $(MANDIR)/lectio-ui.1 $(MANDIR)/lectio-web.1
test: check-corpora ## run tests (with all corpora embedded, so corpus tests can resolve text)
	go test -tags fullbible ./...
vet: ## go vet
	go vet ./...
fmt: ## gofmt the tree
	gofmt -w .
ci: ## full pre-push gate: gofmt clean, vet, both tag sets, corpora
	@u=$$(gofmt -l .); test -z "$$u" || { echo "gofmt needed:"; echo "$$u"; exit 1; }
	go vet ./...
	go test ./...
	$(MAKE) test
install-hooks: ## install the git pre-push hook (runs make ci)
	@mkdir -p .git/hooks
	@cp scripts/hooks/pre-push .git/hooks/pre-push
	@chmod +x .git/hooks/pre-push
	@echo "installed .git/hooks/pre-push -> runs 'make ci' (bypass a push with --no-verify)"
release: ## cut a release: make release VERSION=0.46.0  (add its CHANGELOG line first)
	@test -n "$(VERSION)" || { echo "set VERSION=X.Y.Z" >&2; exit 2; }
	@echo "$(VERSION)" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$$' || { echo "VERSION must be X.Y.Z" >&2; exit 2; }
	@test -z "$$(git status --porcelain)" || { echo "working tree not clean; commit or stash first" >&2; exit 2; }
	@grep -q '## \[$(VERSION)\]' CHANGELOG.md || { echo "add a one-line CHANGELOG.md entry under '## [$(VERSION)]' first" >&2; exit 2; }
	@sed -i 's/^const Version = ".*"/const Version = "$(VERSION)"/' internal/config/config.go
	@grep -q 'const Version = "$(VERSION)"' internal/config/config.go || { echo "version bump failed" >&2; exit 2; }
	$(MAKE) ci
	git add internal/config/config.go CHANGELOG.md
	git commit -m "release: v$(VERSION)"
	git tag -a "v$(VERSION)" -m "lectio $(VERSION)"
	@echo "tagged v$(VERSION). publish with: git push origin main v$(VERSION)"
clean: ## remove build artifacts
	rm -f $(LECTIO) $(LECTIO_UI) $(LECTIO_WEB); rm -rf dist
cross: ## cross-compile into dist/
	@mkdir -p dist
	@for t in linux/amd64 linux/arm64 darwin/arm64 darwin/amd64 windows/amd64; do \
		os=$${t%/*}; arch=$${t#*/}; ext=; [ $$os = windows ] && ext=.exe; \
		echo "  $$os/$$arch"; \
		GOOS=$$os GOARCH=$$arch go build -o dist/$(LECTIO)-$$os-$$arch$$ext ./cmd/lectio; \
		GOOS=$$os GOARCH=$$arch go build -o dist/$(LECTIO_UI)-$$os-$$arch$$ext ./cmd/lectio-ui; \
		GOOS=$$os GOARCH=$$arch go build -o dist/$(LECTIO_WEB)-$$os-$$arch$$ext ./cmd/lectio-web; \
	done

# Validate every embedded corpus (CI gate). grb is EXEMPT: it is a scholarly
# Septuagint corpus whose Book column intentionally carries Orthodox-canon
# extras (1-4 Esdras/Maccabees, Odes, Psalms of Solomon) and manuscript variants
# (…(Theodotion)/(Sinaiticus)/(Vaticanus)) that are not in the 73-key Catholic
# canon and would collide with canonical keys if renamed. --corpus-check grb
# correctly reports these; they are by design, not defects to fix here.
check-corpora: ## validate every bundled corpus, core + optional (skips grb, see comment)
	@for f in internal/bible/corpora/*.tsv internal/bible/corpora_optional/*.tsv; do \
		code=$$(basename $$f .tsv); \
		[ "$$code" = grb ] && { echo "skipping grb (broader LXX canon)"; continue; }; \
		echo "checking $$code"; \
		go run -tags fullbible ./cmd/lectio --corpus-check $$code || exit 1; \
	done

# Validate then embed a corpus pair: make add-corpus CORPUS=fr-crampon SRC=/path/to/dir
add-corpus: ## validate + copy a corpus pair into internal/bible/corpora/, then build
	@test -n "$(CORPUS)" || { echo "set CORPUS=<code>" >&2; exit 2; }
	@test -n "$(SRC)" || { echo "set SRC=<dir with $(CORPUS).tsv/.ini>" >&2; exit 2; }
	go run ./cmd/lectio --corpus-check "$(SRC)/$(CORPUS).tsv"
	cp "$(SRC)/$(CORPUS).tsv" "$(SRC)/$(CORPUS).ini" internal/bible/corpora/
	$(MAKE) build
