# convert png to eps for embedding in pdf
.PHONY: eps
eps: cornbeast.eps
cornbeast.eps:
	convert assets/cornbeast.png cornbeast.eps

# create dot file
.PHONY: dot
dot: tmp/dot
tmp/dot: db/game.rec
	echo "digraph {" > tmp/dot
	recsel -t game db/game.rec \
	| recfmt '{{id}} -> {{text}}|' \
	| sed -e 's/|/\n\n/g' \
	| sed -e '/./{H;$!d;}' -e 'x; s/\n//g; G;' \
	| sed -e 's/}}[^{]*{{/}} {{/g' -e 's/> [^{]* {{/> {{/' \
	| sed -e 's/}}[^{]*/}} /' \
	| sed -e 's/[^}]$$//' \
	| sed -e 's/^\([0-9]*\) -> [^{].*/\1/' \
	| sed -e 's/}} {{/}}, {{/g' \
	>> tmp/dot
	echo "}" >> $@

# create data object
.PHONY: data
data: tmp/data
tmp/data: db/game.rec
	echo "{" `just _map` "}" \
	| sed -e 's/, }/ }/' \
	> $@

# create the graph
.PHONY: graph
graph: out/graph.png
out/graph.png: tmp/data tmp/dot
	mustache tmp/data tmp/dot \
	| dot -Tpng > $@

# create the ms template file
.PHONY: ms
ms: tmp/story.ms
tmp/story.ms: tmp/data templates/story.ms.template
	recsel -t game db/game.rec \
	| rec2csv \
	| csvjson \
	| jq '. | { data: . }' \
	| mustache - templates/story.ms.template \
	> $@


# make the pdf output
.PHONY: pdf
pdf: out/story.pdf
out/story.pdf: tmp/story.ms
	mustache tmp/data tmp/story.ms \
	| groff -ms -Tps -dpaper=a5 -P-pa5 \
	| ps2pdfwr - $@

# make the ascii output
.PHONY: ascii
ascii: out/story.txt
out/story.txt: tmp/data tmp/story.ms
	mustache tmp/data tmp/story.ms \
	| groff -ms -Tascii \
	| sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" \
	> $@

# generate u-ctags
tags: db/game.rec
	ctags --options=./lib/game.ctags db/game.rec
