.PHONY: all build

CHROMIUM_BIN = $(shell if command -v chromium >/dev/null 2>&1; then echo chromium; elif command -v google-chrome-stable >/dev/null 2>&1; then echo google-chrome-stable; elif command -v google-chrome >/dev/null 2>&1; then echo google-chrome; elif command -v chromium-browser >/dev/null 2>&1; then echo chromium-browser; fi)
CHROMIUM_EXTRA_FLAGS = $(shell if [ "$$(id -u)" -eq 0 ]; then echo --no-sandbox; fi)
PANDOC_CMD = pandoc content.md --standalone --template=template.html --css=styles.css -o index.html
PDF_CMD = $(CHROMIUM_BIN) $(CHROMIUM_EXTRA_FLAGS) --headless --disable-gpu --allow-file-access-from-files --virtual-time-budget=10000 --print-to-pdf=output.pdf --no-pdf-header-footer "file://$$PWD/index.html"

all: build

build:
	@test -n "$(CHROMIUM_BIN)" || (echo "Error: no supported browser found (chromium/google-chrome/chromium-browser)" >&2; exit 127)
	$(PANDOC_CMD)
	$(PDF_CMD)
