# compare.sr.ht — build scaffolding (git.sr.ht / sourcehut-dolt style).
#
# Go binary + Go-template SSR live under cmd/ and web/. The CSS is built from
# the shared sourcehut scss partials with sassc; the frontend diff bundle is
# built once with esbuild and vendored into web/static/. Node is a build-time
# dependency only.

SERVICE=compare.sr.ht
BIN=comparesrht

PREFIX?=/usr/local
BINDIR?=$(PREFIX)/bin
SHAREDIR?=$(PREFIX)/share
ASSETS?=/usr/share/sourcehut
STATICDIR?=$(ASSETS)/$(SERVICE)/static

SASSC?=sassc
SASSC_INCLUDE=-I$(ASSETS)/scss

all: build

# Compile the service into ./comparesrht.
build:
	go build -o $(BIN) ./cmd/$(BIN)

test:
	go test ./...

# CSS pipeline: sassc -> minify -> content-hashed filename. The running
# service globs web/static/main.min.*.css at startup, so the hash in the name
# is the cache-busting version. Requires the shared scss partials to be
# installed at $(ASSETS)/scss (core.sr.ht `make install`, which installs both
# base.scss and bootstrap/scss there) and scss/main.scss to exist (phase 2b).
# Produces exactly ONE web/static/main.min.<sha256[:8]>.css (old ones and the
# intermediate main.css are removed).
css:
	mkdir -p web/static
	rm -f web/static/main.css web/static/main.min.*.css
	$(SASSC) $(SASSC_INCLUDE) scss/main.scss web/static/main.css
	minify -o web/static/main.min.css web/static/main.css
	mv web/static/main.min.css \
		web/static/main.min.$$(sha256sum web/static/main.min.css | cut -c1-8).css
	rm -f web/static/main.css

# Frontend diff/tree bundle: built once, output committed to web/static. The
# filename carries a content hash (like the CSS) so a deploy busts the browser
# cache — a stale bundle.js is otherwise served for up to max-age and can leave
# the file tree rendering blank after an upgrade. Produces exactly ONE
# web/static/bundle.<sha256[:8]>.js (old ones removed).
bundle:
	mkdir -p web/static
	rm -f web/static/bundle.js web/static/bundle.*.js
	cd frontend && npm ci && \
		npx esbuild src/app.ts --bundle --minify --format=esm \
			--outfile=../web/static/bundle.js
	mv web/static/bundle.js \
		web/static/bundle.$$(sha256sum web/static/bundle.js | cut -c1-8).js

# Local development run. Requires a ./config.ini in the working directory (or
# ../config.ini, /etc/sr.ht/config.ini): copy config.example.ini and fill in the
# instance's shared [sr.ht]/[webhooks] keys, plus a [git.sr.ht] repos root and an
# api-origin pointing at a real (or stubbed, see contrib/dev-stub) GraphQL API.
# See the "Development" section of README.md for the full recipe.
run-dev: build
	./$(BIN) -b localhost:5090

install: build
	install -Dm755 $(BIN) $(DESTDIR)$(BINDIR)/$(BIN)
	mkdir -p $(DESTDIR)$(STATICDIR)
	install -Dm644 -t $(DESTDIR)$(STATICDIR) web/static/*

.PHONY: all build test css bundle run-dev install
