From 1d7c46db8f2380dbe96c91f4f3c8730843ba3ada Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Sat, 29 Aug 2015 16:26:51 +0100
Subject: [PATCH] Script to update zeronet bootstrap

---
 Makefile             |  2 ++
 src/freedombone-prep |  1 +
 src/zeronetavahi     | 82 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)
 create mode 100755 src/zeronetavahi

diff --git a/Makefile b/Makefile
index d140a3fa..40cb3ce7 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ source:
 install:
 	mkdir -p ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP} ${DESTDIR}${PREFIX}/bin
+	install -m 755 src/zeronetavahi ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-keydrive ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-splitkey ${DESTDIR}${PREFIX}/bin
 	install -m 755 src/${APP}-recoverkey ${DESTDIR}${PREFIX}/bin
@@ -81,6 +82,7 @@ uninstall:
 	rm -f ${PREFIX}/share/man/man1/${APP}-mesh.1.gz
 	rm -rf ${PREFIX}/share/${APP}
 	rm -f ${PREFIX}/bin/${APP}
+	rm -f ${PREFIX}/bin/zeronetavahi
 	rm -f ${PREFIX}/bin/${APP}-keydrive
 	rm -f ${PREFIX}/bin/${APP}-splitkey
 	rm -f ${PREFIX}/bin/${APP}-recoverkey
diff --git a/src/freedombone-prep b/src/freedombone-prep
index 08f2e932..0bcf76a8 100755
--- a/src/freedombone-prep
+++ b/src/freedombone-prep
@@ -243,6 +243,7 @@ $SUDO sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $MICROSD_MOUNT
 
 # copy the commands to the card
 $SUDO cp -f $(which freedombone)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
+$SUDO cp -f $(which zeronetavahi)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
 if [ ! -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone ]; then
     echo 'There was a problem with writing freedombone commands to the SD card'
     exit 8736
diff --git a/src/zeronetavahi b/src/zeronetavahi
new file mode 100755
index 00000000..94a56a72
--- /dev/null
+++ b/src/zeronetavahi
@@ -0,0 +1,82 @@
+#!/bin/bash
+#
+# .---.                  .              .
+# |                      |              |
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
+#
+#                    Freedom in the Cloud
+#
+# A script for using avahi to discover peers and update zeronet trackers
+
+# License
+# =======
+#
+# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+TRANSMISSION_PORT=
+BOOTSTRAP_FILE=/opt/zeronet/bootstrap
+
+if [ ! -d /opt/zeronet ]; then
+    exit 0
+fi
+
+if [ ! -d /etc/avahi ]; then
+    exit 0
+fi
+
+TEMPFILE=/tmp/tmpzeronetavahi.txt
+avahi-browse -atrl | grep "Workstation\|hostname =\|address =\|port =" > $TEMPFILE
+if [ ! -f $TEMPFILE ]; then
+    exit 1
+fi
+
+state=0
+address=""
+port=0
+peer=""
+while IFS='' read -r line || [[ -n "$line" ]]; do
+    if [ ${state} -eq "3" ]; then
+        if [[ $line == *"port ="* ]]; then
+            port=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
+            echo "udp $address $port" >> $BOOTSTRAP_FILE.new
+            state=0
+        fi
+    fi
+    if [ ${state} -eq "2" ]; then
+        if [[ $line == *"address ="* ]]; then
+            address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
+            state=3
+        fi
+    fi
+    if [ ${state} -eq "1" ]; then
+        if [[ $line == *"hostname ="* ]]; then
+            peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
+            state=2
+        fi
+    fi
+    if [[ $line == *"Workstation"* && $line == "= "* ]]; then
+        state=1
+    fi
+done < "$TEMPFILE"
+
+rm -f $TEMPFILE
+cp -f $BOOTSTRAP_FILE.new $BOOTSTRAP_FILE
+rm -f $BOOTSTRAP_FILE.new
+sudo chown zeronet:zeronet /opt/zeronet/bootstrap
+
+exit 0