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/
BASE_URI = https://thoughtroam.xn--abcdefghijklmnopqrstuvxyz-0fc0a81c.dk/

#PAD_ARTICLES = worlding
ARTICLES     = waste worlding

ONTOLOGIES = learn waste
ONTOLOGIES_LEARN = tentacular bisected trap
ONTOLOGIES_VERSION = 0.1

GRAPHS = $(patsubst ex/%.ttl,%,$(wildcard ex/*.ttl))

PERL_FILES = bin/biblatex-tidy 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)

prepare: \
 $(patsubst %,learn/$(ONTOLOGIES_VERSION)/%.ttl,core $(ONTOLOGIES_LEARN)) \
 $(foreach ext,svg tex,learn/rdfs.$(ext)) \
 $(foreach dia,tentacular trap all,$(foreach ext,svg tex,learn/$(dia)_rdfs.$(ext)))

learn/$(ONTOLOGIES_VERSION)/%.ttl: learn/%.ttl
	mkdir --parents learn/0.1
	perl -gp \
	 -e 's,\b$(subst bisected,learn2sect,$(subst tentacular,learn10tac,$(subst core,learn,$*))):,:,g;' \
	 -e 's,\Q$(BASE_URI)stub/foaf#,http://xmlns.com/foaf/0.1/,g;' \
	 -e 's,\Q$(BASE_URI)stub/doap#,http://usefulinc.com/ns/doap#,g;' \
	 -e 's,purl\.org/learn/\K(?=[^\d]),$(ONTOLOGIES_VERSION)/,g;' \
	 -e 's,\n\s*owl:imports\s[^\n]+;(?=\n),,g;' \
	 < $< > $@

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_ARTICLES:%=pad-export-%)

pad-export-worlding: ref.bib worlding/index.qmd \
 $(patsubst %,worlding/_%.qmd,baggrund teori_og_metode undersøgelser konklusion perspektivering kilder planer)

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

worlding/index.qmd:
	hedgedoc export --md MJei_2e-SjahCtmRT-xiPw $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_baggrund.qmd:
	hedgedoc export --md mGHhKvXyQ5SVeTIU11isQQ $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_teori_og_metode.qmd:
	hedgedoc export --md 5AbKoR-mTOij6TsCMFFDRw $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_undersøgelser.qmd:
	hedgedoc export --md 5XR69V5rSEm9LwsLNtSXcQ $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_konklusion.qmd:
	hedgedoc export --md ofRbx2f4QcWLR-o_kqTNCQ $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_perspektivering.qmd:
	hedgedoc export --md lU8aUxCkR5GNrP-8frklFA $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_kilder.qmd:
	hedgedoc export --md Jt6S4tAhQM-BUrAP-OllCg $@
	perl -g -e 's/\s*\z/\n/' $@

worlding/_planer.qmd:
	hedgedoc export --md qfFl3xiaQWCbpoyNPbNsEA $@~
	bin/hedgedoc2quarto $@~ $@

$(PAD_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: ref.bib
	bin/biblatex-tidy $<

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\\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

%/concepts.svg: %/core.sdm
	sdml draw --diagram concepts -i $< -f svg -o $@

%/er.svg: %/core.sdm
	sdml draw --diagram entity-relationship -i $< -f svg -o $@

%/uml.svg: %/core.sdm
	sdml draw --diagram uml-class -i $< -f svg -o $@

%.ttl: %.sdm
	sdml convert -i $< -f rdf -o $@ --no-color

%/core.puml: %/core.ttl %/prefixes.ttl
	rdfpuml $<

%/core.png: %/core.puml
	plantuml -tpng $<

%/core.svg: %/core.puml
	plantuml -tsvg $<

%/rdfs.svg: %/core.ttl
	rdfs2dot -f turtle $< | dot -Tsvg -o$@

learn/%_rdfs.svg: learn/%.ttl
	rdfs2dot -f turtle $< | dot -Tsvg -o$@

learn/all_rdfs.svg: learn/core.ttl $(ONTOLOGIES_LEARN:%=learn/%.ttl)
	rdfs2dot -f turtle $^ | dot -Tsvg -o$@

%/rdfs.tex: %/core.ttl
	rdfs2dot -f turtle $< | dot2tex --figonly -o$@

learn/%_rdfs.tex: learn/%.ttl
	rdfs2dot -f turtle $< | dot2tex --figonly -o$@

learn/all_rdfs.tex: learn/core.ttl $(ONTOLOGIES_LEARN:%=learn/%.ttl)
	rdfs2dot -f turtle $^ | dot2tex --figonly -o$@

ex/%.puml: ex/%.ttl ex/prefixes.ttl
	rdfpuml $<

ex/%.png: ex/%.puml
	plantuml -tpng $<

ex/%.svg: ex/%.puml
	plantuml -tsvg $<

%/docs/index.html: %/core.ttl
	mkdir --parents $*/docs
	ontospy gendocs --type 2 -x -o $(CURDIR)/$*/docs $<

$(ONTOLOGIES:%=view-%-concept-diagram): view-%-concept-diagram: %/concepts.svg
	inkview $<

$(ONTOLOGIES:%=view-%-er-diagram): view-%-er-diagram: %/er.svg
	inkview $<

$(ONTOLOGIES:%=view-%-uml-diagram): view-%-uml-diagram: %/uml.svg
	inkview $<

$(ONTOLOGIES:%=view-%-rdfs-graf): view-%-rdfs-graf: %/core.ttl
	rdfs2dot -f turtle $< | xdot -

$(ONTOLOGIES_LEARN:%=view-learn_%-rdfs-graf): view-learn_%-rdfs-graf: learn/%.ttl
	rdfs2dot -f turtle $< | xdot -

view-learn_avoid-rdfs-graf: $(patsubst %,learn/%.ttl,bisected trap)
	rdfs2dot -f turtle $^ | xdot -

view-learn_all-rdfs-graf: learn/core.ttl $(ONTOLOGIES_LEARN:%=learn/%.ttl)
	rdfs2dot -f turtle $^ | xdot -

view-learn_worlding-rdfs-graf: $(ONTOLOGIES_LEARN:%=learn/%.ttl)
	rdfs2dot -f turtle $^ | xdot -

#$(ONTOLOGIES:%=view-%-puml-diagram): view-%-puml-diagram: %/core.svg
#	inkview $<
$(ONTOLOGIES:%=view-%-puml-diagram): view-%-puml-diagram: %/core.png
	imv-wayland $<
$(GRAPHS:%=view-%-puml-diagram): view-%-puml-diagram: ex/%.png
	imv-wayland $<

$(ONTOLOGIES:%=render-%-ontology): render-%-ontology: %/docs/index.html