# SPDX-License-Identifier: BlueOak-1.0.0
# This file is licensed under the Blue Oak Model License 1.0.0.

SHELL = /bin/bash

# ~~~ Variables used by rules ~~~
#

core-files := pollen.rkt dust.rkt
html-deps  := snippets-html.rkt tags-html.rkt crystalize.rkt cache.rkt series-list.rkt

article-sources := $(wildcard articles/*.poly.pm)
articles-html   := $(patsubst %.poly.pm, %.html, $(article-sources))
articles-pdf    := $(patsubst %.poly.pm, %.pdf,  $(article-sources))

series-sources  := $(wildcard series/*.poly.pm)
series-html     := $(patsubst %.poly.pm, %.html, $(series-sources))

# ~~~ Rules ~~
#

# The order of these dependencies is important. They will be processed left to right.
web: vitreous.sqlite $(articles-html) $(series-html) 
web: index.html blog-pg1.html keyword-index.html web-extra/martin.css feed.xml
web: ## Rebuild all web content (not PDFs)

# The file vitreous.sqlite is a cache of the rendered HTML and metadata. If it is older than any of
# its dependencies (or missing) all of the articles will be rebuilt. Its dependencies are also on
# the Pollen cache watchlist (see pollen.rkt)
#
# After articles are rendered in this way, all the series pages are "touched", to trick Pollen into
# re-rendering those pages without relying on its cache. Without this step, it seems those pages
# are rendered “transitively” during the article render, and the listings are incomplete.
vitreous.sqlite: $(core-files) $(html-deps) template.html.p
	raco make pollen.rkt
	racket -tm util/init.rkt
	raco pollen setup -p articles/
	raco pollen render -p -t html articles/*.poly.pm
	touch series/*.poly.pm
	raco pollen setup -p series/
	raco pollen render -p -t html series/*.poly.pm
	rm -f template.html series/template.html
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no articles/*.html || true
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no series/*.html || true

# If the rule for vitreous.sqlite was triggered, all the article HTML files will already have been
# re-rendered.  (That rule comes before this one in the list of dependencies for "web") But if not,
# any individual files that have been edited will get re-rendered.
$(articles-html): %.html: %.poly.pm 
	raco pollen render $@
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no $@ || true

# Note that if any article is part of a series, it will touch its series .poly.pm file during its
# render, triggering this rule for that series.
$(series-html): %.html: %.poly.pm
	raco pollen render $@
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no $@ || true

index.html: $(core-files) $(html-deps) $(series-html) index-template.html.p index.html.pm
	raco pollen render index.html
	fossil uv add scribbled/site-footer.html

# This target will also rebuild pg2, pg3, etc. as needed
blog-pg1.html: $(core-files) $(html-deps) $(articles-html) blog.rkt
	rm -f blog*.html
	racket -tm blog.rkt
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no blog*.html || true

keyword-index.html: $(core-files) $(html-deps) $(articles-html) keyword-index.rkt
	racket -tm keyword-index.rkt
	tidy -quiet -modify -indent --wrap 100 --wrap-attributes no --tidy-mark no $@ || true

web-extra/martin.css: web-extra/martin.css.pp
	raco pollen render $@

feed.xml: vitreous.sqlite rss-feed.rkt
	racket -tm rss-feed.rkt

zap: ## Clear Pollen and Scribble cache, and remove all HTML output
	raco pollen reset
	rm -f *.html articles/*.html series/*.html vitreous.sqlite 

publish: check-env
publish: ## Sync all HTML and PDF stuff to the public web server (does not rebuild any files)
	raco pollen publish
	./util/relativize ~/Desktop/publish/
	rsync -av ~/Desktop/publish/ -e 'ssh -p $(WEB_SRV_PORT)' $(LOCALYARN_SRV) \
		--delete \
		--exclude=drafts \
		--exclude=code-docs \
		--exclude=util \
		--exclude=x-mockup \
		--exclude=repo-www \
		--exclude=scribbled \
		--exclude='*.sqlite' \
		--exclude='*.fossil' \
		--exclude=.fossil-settings \
		--exclude=.fslckout \
		--exclude='*.ltx' \
		--exclude='*.swp' \
		--exclude=.DS_Store \
	    --exclude='template*.*' \
		--exclude=makefile 
	rm -rf ~/Desktop/publish
	fossil uv sync

scribble: ## Rebuild code documentation and update Fossil repo
	scribble --htmls +m --redirect https://docs.racket-lang.org/local-redirect/ code-docs/main.scrbl
	fossil uv rm scribbled/*
	mv scribbled/site-footer.html main/ || true
	rm -rf scribbled/*
	mv main/* scribbled/
	cp code-docs/scribble-iframe.html scribbled/scribble.html
	rm -rf main
	fossil uv add scribbled/*
	fossil uv sync

# Self-documenting makefile (http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html)
help: ## Displays this help screen
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

article: ## Start a new article from a template
	racket -tm util/newpost.rkt

.PHONY: web scribble help zap article publish check-env

.DEFAULT_GOAL := help

check-env:
ifndef LOCALYARN_SRV
	$(error LOCALYARN_SRV env variable not set, should be a destination valid for rsync)
endif
ifndef WEB_SRV_PORT
	$(error WEB_SRV_PORT env variable not set, should be SSH port number for web server)
endif
