summaryrefslogtreecommitdiffstats
path: root/retiolum/bin/update_tinc_hosts
diff options
context:
space:
mode:
Diffstat (limited to 'retiolum/bin/update_tinc_hosts')
-rwxr-xr-xretiolum/bin/update_tinc_hosts56
1 files changed, 56 insertions, 0 deletions
diff --git a/retiolum/bin/update_tinc_hosts b/retiolum/bin/update_tinc_hosts
new file mode 100755
index 00000000..ce1be497
--- /dev/null
+++ b/retiolum/bin/update_tinc_hosts
@@ -0,0 +1,56 @@
+#! /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="$(
+ basename="`readlink -f "$0"`"
+ bindir="`dirname "$basename"`"
+ echo "$bindir/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