SSH_HOST = www-ruc@xayide.jones.dk
SSH_BASE_PATH = public_websites/thoughtroam.abcde.../

BASE_SSH = $(SSH_HOST):$(SSH_BASE_PATH)
BASE_IRI = https://thoughtroam.abcdefghijklmnopqrstuvxyzæøå.dk/

ARTICLES = learn code use worlding

PERL_FILES = bin/hedgedoc2quarto bin/xmp2rdfxml

TIDY_MARKDOWN = worlding

export HEDGEDOC_COOKIES_FILE = $(CURDIR)/cookies.txt
export HEDGEDOC_SERVER = https://pad.degrowth.net/

all: preview

preview:
	quarto preview

render:
	quarto render

publish:
	rsync -avH --delete-after _site/ $(BASE_SSH)

cal-all: cal-fetch cal-render cal-publish

cal-fetch:
	bin/cal-fetch.sh

cal-render:
	bin/cal-publish.sh

cal-publish:
	ssh $(SSH_HOST) mkdir -p $(SSH_BASE_PATH)events/
	rsync -avH --delete-after $(patsubst %,events/%,$(ARTICLES) superteam) $(BASE_SSH)events/

pad-login:
	hedgedoc login --email

pad-list:
	hedgedoc history

pad-export-all: pad-export-superteam $(ARTICLES:%=pad-export-%) pad-export-code-gantt pad-export-worlding-gantt

pad-export-superteam: ref.bib
	hedgedoc export --md 8krqOrqnR0iAIExDTtTrxw $<
	perl -g -e 's/\s*\z/\n/' $<

pad-export-learn: learn/index.qmd
	hedgedoc export --md CP1pFdg_QlGiYxtKc9IHBg $<
	perl -g -e 's/\s*\z/\n/' $<

pad-export-code: code/index.qmd
	hedgedoc export --md 8QGRP0iTRwe7hFmoAX4QnA $<
	perl -g -e 's/\s*\z/\n/' $<

pad-export-code-gantt: code/_gantt.qmd
	hedgedoc export --md mJnfBHZ7Qr2W1Hu-QppL8A $<~
	bin/hedgedoc2quarto $<~ $<

pad-export-use: use/index.qmd
	hedgedoc export --md aFt_nNkYTQibl0CQrBJhtQ $<
	perl -g -e 's/\s*\z/\n/' $<

pad-export-worlding: worlding/index.qmd
	hedgedoc export --md qfFl3xiaQWCbpoyNPbNsEA $<
	perl -g -e 's/\s*\z/\n/' $<

pad-export-worlding-gantt: worlding/_gantt.qmd
	hedgedoc export --md RcgcY770QC6L2IKWsUXuBA $<~
	bin/hedgedoc2quarto $<~ $<

$(ARTICLES:%=pad-import-%): pad-import-%:
	hedgedoc import $*/index.qmd

pad-import-ref:
	hedgedoc import ref.bib

lint: lint-markdown

lint-markdown:
	@mdl --ignore-front-matter --style .mdl.rb $(filter-out %/index.qmd,$(wildcard $(addsuffix /*.qmd,$(TIDY_MARKDOWN))))

tidy: tidy-bibtex tidy-perl

tidy-bibtex:
	bibclean -max-width 0 ref.bib > ref.bib~
	mv ref.bib~ ref.bib

tidy-perl:
	perltidy -b -bext='/' --profile=.perltidyrc $(PERL_FILES)

$(ARTICLES:%=screening-of-%): screening-of-%:
	QUARTO_LOG_LEVEL=quiet \
	quarto render $*/index.qmd --to man --output - | man -l -

$(ARTICLES:%=wordcount-of-%): wordcount-of-%:
	QUARTO_LOG_LEVEL=quiet \
	quarto render $*/index.qmd --to plain --columns=9999 --output - \
	| perl -nE 'next if /^-*$$/; $$bilag += $$_ =~ "^Bilag 1\\b"; $$chars += length unless $$bilag; END { say $$chars }'

$(ARTICLES:%=turtle-from-%): turtle-from-%:
	@bin/xmp2rdfxml _site/$*/S*.pdf \
	| rapper -i rdfxml -o turtle - $(BASE_IRI)$*/

$(ARTICLES:%=dotgraph-from-%): dotgraph-from-%:
	@bin/xmp2rdfxml _site/$*/S*.pdf \
	| rapper -i rdfxml -o dot - $(BASE_IRI)$*/

diff-all: $(ARTICLES:%=diff-of-%)

$(ARTICLES:%=diff-of-%): diff-of-%: %/changes.tex

$(ARTICLES:%=%/changes.tex): %/changes.tex: %/a.tex %/b.tex
	latexdiff --type CULINECHBAR --subtype COLOR --replace-context2cmd '' $^ > $*/changes.tex
	perl -i -pe 's/\[pdftex\]{changebar}/[luatex]{changebar}/g' $*/changes.tex
	latexmk --gg --lualatex --cd --silent --Werror $*/changes.tex
	find $*/ -name 'changes.*' -not -name changes.pdf -delete
	rm -f $^

$(ARTICLES:%=%/a.tex): %/a.tex:
	git switch --detach $(shell git describe --tags --abbrev=0)
	QUARTO_LOG_LEVEL=quiet \
	quarto render $*/index.qmd --to pdf -M keep-tex:true --output a.pdf
	mv --force $*/index.tex $@
	rm --force _site/a.pdf

$(ARTICLES:%=%/b.tex): %/b.tex:
	git switch -
	QUARTO_LOG_LEVEL=quiet \
	quarto render $*/index.qmd --to pdf -M keep-tex:true --output b.pdf
	mv --force $*/index.tex $@
	rm --force _site/b.pdf