summaryrefslogtreecommitdiffstats
path: root/retiolum/bin/update_tinc_hosts
blob: 46076cf2cc04f2f932870cab1f7019636e8909d3 (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
#! /bin/sh
set -euf

if test "${nosudo-false}" != true -a `id -u` != 0; then
  echo "we're going sudo..." >&2
  exec sudo "$0" "$@"
  exit 23 # go to hell
fi

list_hosts=$(cd -P "$(dirname "$0")" && pwd -P)/hosts

hosts() { "$list_hosts"; }

hosts="${hosts-/etc/hosts}"

bs='# BEGIN OF RETIOLUM'
es='# END OF RETIOLUM'

case "${*-I am made of stupid}" in
  (start|restart)
      hosts | egrep  "^(10|42)" | $0 replace magic
    ;;
  (stop)
    $0 clear magic
    ;;
  ('print magic')
    echo "$bs"
    cat
    echo "$es"
    ;;
  ('create magic')
    $0 has magic || $0 print magic >> $hosts < /dev/null
    $0 start
    ;;
  ('destroy magic')
    if $0 has magic; then
      cache="`cat $hosts`"
      echo "$cache" | sed "/^$bs$/,/^$es$/d" > $hosts 
    fi
    ;;
  ('has magic')
    grep -q "^$bs$" $hosts && grep -q "^$es$" $hosts
    ;;
  ('replace magic')
    $0 destroy magic && $0 print magic >> $hosts
    ;;
  ('clear magic')
    $0 replace magic < /dev/null
    ;;
  (*)
    echo 'Error 1: You are made of stupid!' >&2
    exit 23
esac