.ONESHELL:
templates=head.html header.html footer.html

pages=index.html privacy.html 404.html
deploy-path=lighttpd@saikrupagas.com:/srv/www/saikrupagas.com/

# Explicit allowlist of what actually belongs on the live site -- NOT
# `git ls-files`, which would ship every tracked file in the repo
# (internal tooling, dev configs, build scripts, anything else that
# happens to be checked in) straight to production. head.json and
# html/components/*.html are deliberately excluded -- they're only ever
# pulled in via m4 include() at build time, never fetched at runtime.
# This same list drives both `deploy-src` (scp to production) and `dist`
# (a local servable folder) -- one source of truth for "what's the site".
deploy-files=$(pages) css js robots.txt sitemap.xml favicon.ico \
	gl4xcy2rrdndf2uq9crpfq2oelee7w.html styles vendor
HOST_PORT=8000
LIVESIDE_PORT=8001
DIST=dist

dev-all: M4FLAGS = -D LIVERELOAD=1 -D LIVESIDE_PORT=$(LIVESIDE_PORT)
dev-all: $(pages)

# m4's include() resolves bare names (`header.html`, not `html/header.html`
# -- that's how every .html.in under html/ writes it) against m4's own
# current directory, not the including file's directory. So rather than
# rewrite every include() path, just run m4 with html/ as its cwd and
# send the output back up to the repo root, where it's served from.
html/head.html: html/head.html.in
	cd html && m4 $(M4FLAGS) head.html.in > head.html

%.html: html/%.html.in $(addprefix html/,$(templates))
	cd html && m4 $(M4FLAGS) $*.html.in > ../$*.html

all: $(pages)

autorefresh:
	darkhttpd . --port $(HOST_PORT) &
	liveside -d . -p $(LIVESIDE_PORT) -c "make dev-all"

host:
	darkhttpd . --port $(HOST_PORT)

refresh: all
	liveside -d . -p $(LIVESIDE_PORT) -c "make dev-all"

deploy-src: clean all
	scp -Cr $(deploy-files) $(deploy-path)

deploy-assets:
	scp -Cr assets $(deploy-path)

deploy: deploy-src deploy-assets

clean:
	-rm html/head.html $(foreach page,$(pages),$(page))

# A single self-contained, servable folder -- everything deploy-src/
# deploy-assets would ship to production, gathered in one place instead.
# Point any static server at it: `darkhttpd $(DIST) --port 8000`.
dist: clean all
	rm -rf $(DIST)
	mkdir -p $(DIST)
	cp -R $(deploy-files) $(DIST)/
	cp -R assets $(DIST)/

.PHONY: all dev-all autorefresh host refresh deploy-src deploy-assets deploy clean dist
