HUGO ?= hugo

.PHONY: all build serve clean upload upload-check citations

all: build

# Fetch + cache CSL-JSON for the DOIs in data/publications.yaml.
# Cached results are committed, so `build` needs no network.
#   make citations           # fetch anything not yet cached
#   make citations FORCE=1    # re-fetch everything
citations:
	./scripts/fetch-citations.sh

# The hextra theme is a git submodule. Pull it if a fresh clone left
# themes/hextra/ empty; the marker file means this runs only when needed.
themes/hextra/theme.toml:
	git submodule update --init --recursive

build: themes/hextra/theme.toml
	$(HUGO) --minify

serve: themes/hextra/theme.toml
	$(HUGO) server --buildDrafts --navigateToChanged

clean:
	rm -rf public resources

# --- deploy configuration -------------------------------------------------
DEPLOY_HOST := ovh
DEPLOY_PATH := /var/www/levitation/

# Sync/editor cruft that must never be pushed (patterns are quoted so the
# shell does not expand them before rsync sees them).
DEPLOY_JUNK := --exclude='.syncthing.*.tmp' --exclude='.stfolder' \
               --exclude='.stversions' --exclude='.DS_Store'

# The deploy root holds Hugo output only (out-of-band content lives on its own
# subdomain, files.levitation.wales), so --delete is unconditionally safe.
# --no-owner --no-group: the deploy user cannot chown/chgrp on the server, so
# do not try to preserve ownership (avoids "chgrp ... Operation not permitted").
RSYNC := rsync -avz --delete --no-owner --no-group $(DEPLOY_JUNK)

# Preview exactly what a deploy would add, change, and delete, without touching
# the server.
upload-check: build
	$(RSYNC) --dry-run ./public/ $(DEPLOY_HOST):$(DEPLOY_PATH)

# Deploy to the VPS, but only from the main branch. `citations` runs first so
# a freshly added DOI is never deployed from a stale cache (do not run under
# `make -j`, which would not guarantee that ordering).
upload: citations build
	@branch=$$(git symbolic-ref --short HEAD); \
	if [ "$$branch" = "main" ]; then \
		$(RSYNC) ./public/ $(DEPLOY_HOST):$(DEPLOY_PATH); \
	else \
		echo "Refusing to upload from branch '$$branch' (deploy only from main)."; \
		exit 1; \
	fi
