#!/bin/bash

read_nullbyte() { for v in "$@"; do export "$v"; read -r -d '' "$v"; done }

# Warning: this script is unsafe and certain characters (which are perfectly legal in CSV values) might cause invalid DOT (GraphViz) streams. (you have been warned)

nodes() {
	cat messages.csv \
		| relpipe-in-csv \
		| relpipe-tr-cut '.*' 'message|sender' \
		| relpipe-tr-sed '.*' 'sender' 'executor' 'coral' \
		| relpipe-tr-sed '.*' 'sender' 'streamlet'  'gold2' \
		| relpipe-out-nullbyte \
		| while read_nullbyte message color; do echo "$message[style=\"filled,rounded\", fillcolor=$color];"; done
	echo;
}

edges() {
	cat protocol.csv \
		| relpipe-in-csv \
		| relpipe-out-nullbyte \
		| while read_nullbyte from to label; do echo "$from -> $to [label=\"  $label\"];"; done
}

header() {
	echo "digraph G {
#rankdir=LR;
node[shape=box, style=rounded, fontname=Ubuntu];
start[shape=doublecircle];
end[shape=doublecircle];
";
}

footer() {
	echo "}";
}

( header; nodes; edges; footer ) | dot -Tpdf > protocol.pdf
