summaryrefslogtreecommitdiffstats
path: root/retiolum/bin/supernode-update-hosts-and-hup
blob: b6b03c69d18e1f76720c7ae33b33081fd8541517 (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
#! /bin/sh
#
# @oxberg we do put this into crontab:
# * * * * * /supernode-update-hosts-and-hup
#
set -euf

cd /etc/tinc/retiolum/hosts

temp1="`mktemp`"
temp2="`mktemp`"
trap "rm -f $temp1 $temp2" EXIT INT TERM

old_commit="`cat .commit 2>/dev/null || :`"

_hrefs() {
  sed -n 's/href="\([^"]\+\)"/\n&\n/gp' |
      sed -n 's/^href="\([^"]\+\)"$/\1/p'; }

if curl -fsS \
    "https://github.com/krebscode/painload/tree/master/retiolum/hosts" |
    _hrefs |
    grep '^/krebscode/painload/blob/' |
    grep -v "/blob/$old_commit/" >$temp1
then

  new_commit=`sed 's|^/krebscode/painload/blob/\([^/]*\)/.*|\1|;q' $temp1`

  sed '
    s|^/krebscode/painload/blob/[^/]*/retiolum/hosts/\([^/]*\)$|\1|
  ' $temp1 > $temp2

  xargs rm -v -f <$temp2

  sed "
    s|^.*$|https://raw.github.com/krebscode/painload/$new_commit/retiolum/hosts/&|
  " $temp2 > $temp1

  wget -qi- < $temp1

  echo $new_commit > .commit

  pkill -HUP tincd
fi