export GOTOOLCHAIN := go1.26.5
export GOPRIVATE := $(GOPRIVATE),amadan.net/oficina/estilo.git

APP := memoria

# What `carlos ship -target` defaults to, and therefore what a release
# must be built for. Building for your own machine and shipping that is a
# silent architecture mismatch: the binary uploads fine and fails to exec
# on the instance. Override for a one-off:
#   make release RELEASE_GOARCH=amd64
RELEASE_GOOS   ?= linux
RELEASE_GOARCH ?= arm64
RELEASE_BIN := releases/$(APP)-$(RELEASE_GOOS)-$(RELEASE_GOARCH)

.PHONY: build release deps test vet fmt-check migration-check ci browser

# build is the compile check: with more than one package matched, go
# build discards the output, so this catches a broken package without
# producing an artifact.
build:
	CGO_ENABLED=0 go build ./...

# release is what ships. -s -w drops the symbol table and DWARF, which on
# this family's apps is 23-31% off the binary and 45-53% off the
# compressed artifact — and compressed size is what actually gets
# transferred. carlos is built the same way (see platform's Makefile).
#
# What survives: panic traces keep their function names AND line numbers
# (Go's pclntab is untouched by these flags), debug.ReadBuildInfo works,
# and go version -m still reports module metadata. What you lose is
# source-level debugging with delve or gdb — build without the flags for
# that. rastrillo dev never strips, for the same reason.
# The scaffold's release target stamped no version and did not refuse a
# dirty tree. Both matter: rastrillo.BuildVersion defaults to "dev", and
# an unstamped binary answers "dev" from /api/version forever — which
# makes the deploy check structurally incapable of failing, since the
# thing it compares can never disagree.
#
# The stamp was a short sha because there were no tags to describe. Now
# that a release IS a tag, git describe --tags --always answers with the
# tag when the build sits on one and falls back to the same short sha
# when it does not — so the reasoning above is unchanged, and a
# published binary reports the name it was published under.
VERSION ?= $(shell git describe --tags --always 2>/dev/null)

release:
	@git diff --quiet && git diff --cached --quiet || \
		{ echo "refusing to build a release from a dirty tree"; exit 1; }
	@test -n "$(VERSION)" || \
		{ echo "make release: no version to stamp; commit, or name it: make release VERSION=v0.1.0"; exit 1; }
	@mkdir -p releases
	CGO_ENABLED=0 GOOS=$(RELEASE_GOOS) GOARCH=$(RELEASE_GOARCH) go build -trimpath \
		-ldflags "-s -w -X amadan.net/rastrillo/rastrillo.BuildVersion=$(VERSION)" \
		-o $(RELEASE_BIN) ./cmd/$(APP)
	@echo "built $(RELEASE_BIN) reporting version $(VERSION)"
	@echo "deploy it with:"
	@echo "  carlos deploy --app $(APP) --account bds --channel edge --timeout 5m $(RELEASE_BIN)"
	@echo "publish it with:"
	@echo "  amadan release create -repo oficina/$(APP) -notes-file NOTES.md $(VERSION) $(RELEASE_BIN)"

test:
	go test ./...

# deps makes sure the module cache holds what go.sum says, and repairs it
# when it does not. The CI runner's cache held an extracted
# amadan.net/rastrillo/rastrillo whose embedded files (locales/*.toml,
# migrations/*.sql, ui/calendar.js) were missing, so vet failed with
# "pattern ...: no matching files found" while the same commit passed
# here (Calendar met it first, oficina/calendar#26). `go mod verify`
# names such a directory as "dir has been modified"; deleting it lets
# `go mod download` extract it again from the checksummed zip.
deps:
	@go mod download
	@out=$$(go mod verify 2>&1) || { \
		echo "$$out"; \
		echo "$$out" | sed -n 's/.*: dir has been modified (\(.*\))$$/\1/p' | while read -r dir; do \
			echo "deps: re-extracting $$dir"; chmod -R u+w "$$dir" && rm -rf "$$dir"; \
		done; \
		go mod download && go mod verify; \
	}

# Also vets the browser-tagged tests: they are outside `make ci`, and a
# test behind a tag that nothing compiles rots silently.
vet: deps
	go vet ./...
	go vet -tags browser ./internal/memoriatest/

fmt-check:
	@out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

# A step of its own, not folded into another target's recipe: ci.d/
# steps each exec one make target, so migration-check needs its own
# name to get its own reported step on a runner with step support.
#
# go tool, not a bare "rastrillo": go.mod pins the CLI as a tool at the
# module's own version, so this needs nothing on PATH but the Go
# toolchain, works offline after go mod download, and always runs the
# migrate the app is built against. A bare "rastrillo migration check"
# failed a fresh clone's very first CI run with "make: rastrillo:
# Command not found", and nothing in the scaffold said what to install.
migration-check:
	go tool rastrillo migration check

# ci is the one gate: what a runner executes and what you run before
# pushing are the same definition. amadan's runner falls back to this
# target when .amadan/ci is absent; ci.d/ carries the same targets,
# reported one by one, for a runner with step support. migration-check
# fails the build the moment models.go and migrations/ disagree,
# instead of at boot on whatever machine notices next. If the app
# declares manifest resources, also add: rastrillo generate --check
ci: vet fmt-check test migration-check

# The browser checks (internal/memoriatest/*_browser_test.go), in a real
# Chromium through chromedp. Deliberately NOT part of `ci`: the gate
# every push runs must not need a browser. Run it before shipping a
# change to the CSS or to rastrillo's pin; TestNoZoomOnTap is the iOS
# zoom-on-tap check, and TestPagesRenderUnderTheProductionCSP is the one
# browser check served through rastrillo's production stack, so it sees
# the real CSP. The Chromium comes from RASTRILLO_CHROME, or on a
# Mac from the Playwright cache.
RASTRILLO_CHROME ?= $(firstword $(wildcard $(HOME)/Library/Caches/ms-playwright/chromium-*/chrome-mac*/Chromium.app/Contents/MacOS/Chromium))
browser:
	RASTRILLO_CHROME="$(RASTRILLO_CHROME)" CGO_ENABLED=0 go test -tags browser -count=1 -run 'OnTap|ProductionCSP' ./internal/memoriatest/

.PHONY: credits
credits:
	@set -eu; notices_tmp=$$(mktemp -d); trap 'rm -rf "$$notices_tmp"' EXIT; \
		GOOS=$$(go env GOHOSTOS) GOARCH=$$(go env GOHOSTARCH) CGO_ENABLED=0 \
		go build -o "$$notices_tmp/notices" amadan.net/oficina/estilo.git/cmd/oficina-notices; \
		GOOS=linux GOARCH=arm64 CGO_ENABLED=0 "$$notices_tmp/notices" \
		-package ./cmd/memoria -lucide -onest \
		-exclude amadan.net/oficina/estilo.git,amadan.net/oficina/home/connect,amadan.net/oficina/home/handoff,amadan.net/oficina/suite,amadan.net/rastrillo/native/server \
		> "$$notices_tmp/notices.json"; \
		mv "$$notices_tmp/notices.json" internal/memoria/credits/notices.json
