# 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. Intentionally tolerant: cmd/ does not exist until
# phase 3, so this is a no-op until then.
build:
	@if [ -d cmd ]; then go build ./cmd/...; else \
		echo "cmd/ not present yet (added in phase 3); nothing to build"; fi

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.
# Requires frontend/ (added in phase 2b).
bundle:
	cd frontend && npm ci && \
		npx esbuild src/app.ts --bundle --minify --format=esm \
			--outfile=../web/static/bundle.js

# Local development run against ./config.ini. Fails until cmd/ exists (phase 3).
run-dev:
	go run ./cmd/$(BIN)

install: build
	mkdir -p $(DESTDIR)$(BINDIR)
	go build -o $(DESTDIR)$(BINDIR)/$(BIN) ./cmd/$(BIN)
	mkdir -p $(DESTDIR)$(STATICDIR)
	install -Dm644 -t $(DESTDIR)$(STATICDIR) web/static/*

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