.DEFAULT_GOAL := help

# CLOUDFLARE_API_TOKEN lives in .env (gitignored, written by `make .env`) -
# load it into every recipe's environment, e.g. for infra-plan/infra-apply/deploy.
ifneq (,$(wildcard .env))
include .env
export
endif

# languages.txt is the single source of truth for the site's language set,
# shared with infra/main.tf - blog/hugo.toml's [languages.*] blocks are the
# one place it can't drive directly (Tofu has no TOML parser), so keep those
# in sync by hand.
LANGS := $(shell cat languages.txt)

# Deliberately not named LANG: make imports the environment, and LANG is the
# locale (en_US.UTF-8), which would silently point prod-server at result/en_US.
SITE_LANG ?= $(firstword $(LANGS))
PROD_PORT ?= 8788

# deploy-% targets aren't listed here: giving an instantiated pattern target
# (e.g. deploy-en) to .PHONY creates an explicit no-recipe rule for it, which
# then blocks the deploy-% pattern rule below from ever supplying a recipe.
.PHONY: help dev infra build check-tracked serve prod-server typo infra-plan infra-apply deploy clean

help:
	@grep -E '^[a-zA-Z_.-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-14s\033[0m %s\n", $$1, $$2}'

# The token is the CLOUDFLARE_API_TOKEN field of the "mykiwi.blog" Bitwarden
# item; bw prompts for the master password unless BW_SESSION is set. A file
# target, so it only runs while .env is missing or older than .env.dist.
.env: .env.dist ## Write .env from .env.dist, filling in the token from Bitwarden
	@umask 077; token="$$(bw get item mykiwi.blog | jq -er '.fields[] | select(.name == "CLOUDFLARE_API_TOKEN") | .value')" \
		&& sed "s|@CLOUDFLARE_API_TOKEN@|$$token|" .env.dist > $@

dev: ## Drop into the Hugo dev shell (hugo, dart-sass)
	nix develop

infra: ## Drop into the Cloudflare provisioning shell (opentofu, wrangler, cf)
	nix develop .#infra

# --impure so the derivation can read PAGE_COMMITS (see flake.nix): the footer
# names the commit each page was last changed in, which is history, not source,
# and so cannot be derived from the tree the sandbox is handed. `nix build` on
# its own still works - it just builds pages that name the site's revision
# instead of their own.
build: check-tracked ## Build the site (all languages) into ./result
	PAGE_COMMITS="$$(scripts/page-commits.sh)" nix build --impure

# nix build hands the derivation the repo as git knows it, so a file git has
# never seen is not in the build at all - a new layout, partial or asset goes
# missing from the site with nothing in the output to say so, and `make
# prod-server` and `make deploy` both serve the gap. Only the paths the build
# actually reads are checked, and only files git would otherwise report: a
# gitignored one is not a surprise.
#
# A warning and not an error: an untracked file next to the site's sources is
# a normal thing to have half way through writing one, and only the person
# looking at the list can say which case this is.
check-tracked:
	@untracked="$$(git ls-files --others --exclude-standard -- blog scripts flake.nix flake.lock 2>/dev/null)"; \
	if [ -n "$$untracked" ]; then \
		printf '\033[33m%s\033[0m\n' "warning: untracked files - nix build cannot see these, so they will not be in the site:"; \
		printf '\033[33m  %s\033[0m\n' $$untracked; \
		printf '\033[33m%s\033[0m\n\n' "  git add them (a commit is not needed) to build with them."; \
	fi

serve: ## Run the Hugo dev server with live reload
	nix develop --command hugo server --source blog

# Wrangler's Pages runtime over ./result, so routing, redirects, 404 handling
# and content types are Cloudflare's rather than Hugo's approximation of them.
# Depends on build: it serves the deploy artifact, so it is only as fresh.
prod-server: build ## Serve the built site exactly as deployed, e.g. `make prod-server SITE_LANG=fr PROD_PORT=8080`
	nix run .#prod-server -- $(SITE_LANG) $(PROD_PORT)

# Read-only: prints what JoliTypo would change in ./result, e.g. `make typo
# TYPO_LANGS=fr`. Exits 1 if there is anything, so it can gate CI later.
TYPO_LANGS ?= $(LANGS)
typo: build ## Show typography fixes JoliTypo would make in the built pages
	nix run .#typo-check -- $(TYPO_LANGS)

infra-plan: ## Show what infra-apply would change on Cloudflare
	nix run .#infra -- plan

infra-apply: ## Apply infra/ changes to Cloudflare
	nix run .#infra -- apply

# .env before build: the sub-make below re-reads the Makefile, so a token
# fetched here is in its environment; deploy-% run directly is not covered.
deploy: .env build ## Build the site and push it to Cloudflare Pages (all languages, in parallel)
	$(MAKE) --no-print-directory --output-sync=target -j$(words $(LANGS)) $(addprefix deploy-,$(LANGS))

deploy-%: ## Deploy a single language to Cloudflare Pages, e.g. `make deploy-en` (assumes `make build` already ran)
	nix run .#deploy -- $*

clean: ## Remove build outputs
	rm -rf result blog/public blog/resources
