summaryrefslogtreecommitdiff
path: root/save-graph
blob: 005e6be29091a1e95bb06ebe66b12da9d561045c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash

graph=$(
  tinc --net=$1 dump digraph
)

pre=$(
  printf '%s\n' "${graph}" \
  | sed '
    / -> /,$ d
  '
)
post=$(
  printf '%s\n' "${graph}" \
  | sed '
    1,/ -> /d
    / -> /d
  '
)
inter=$(
  printf '%s\n' "${graph}" \
  | sed -n '
    / -> /p
  ' \
  | while read -r a b c d; do
    mapfile -t e < <(
      printf '%s\n' "$a" "$c" \
      | sort
    )
    b4=$(
      printf '%s\n' "$d" \
      | base64 -w0
    )
    printf '%s %s %s %s %s\n' \
      "${a}" "${c}" "${b4}" "${e[0]}" "${e[1]}"
  done \
  | sort -k4,4 -k5,5 -k3,3 \
  | uniq -cf2 \
  | while read -r c a b b4 _ _; do
    printf '%s -> %s ' "${a}" "${b}"
    printf '%s\n' "${b4}" \
    | base64 -d \
    | if [ "${c}" = '1' ]; then
      cat
    else
      sed 's@];$@, dir=both\0@'
    fi
  done
)

if printf '%s\n' "${graph}" \
| grep -qwF backup \
&& printf '%s\n' "${graph}" \
| greo -qwF backup_lo; then
  extra='
"backup" -> "backup_lo" [style=dashed, constraint=false, dir=none];'
else
  extra=''
fi

{
  printf '%s\n' "${pre}" "${inter}" "${extra}" "${post}"
} \
| if [ $# -eq 0 ]; then
  /usr/bin/dot -Tpng
else
  cat
fi