From 423fdcad5ec93d67b2d8c0de06d1971449f82de7 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob.mottram@codethink.co.uk>
Date: Tue, 12 Jul 2016 10:55:18 +0100
Subject: [PATCH] More consolidation of mesh functions

---
 src/freedombone-app-tox         | 36 +++++++++--------
 src/freedombone-image-customise |  8 ++--
 src/freedombone-utils-avahi     | 10 +++--
 src/freedombone-utils-firewall  |  4 ++
 src/freedombone-utils-setup     |  2 +-
 src/freedombone-utils-web       | 68 +++++++++++++++++----------------
 6 files changed, 71 insertions(+), 57 deletions(-)

diff --git a/src/freedombone-app-tox b/src/freedombone-app-tox
index 7cc9b3d86..36bd0a94e 100755
--- a/src/freedombone-app-tox
+++ b/src/freedombone-app-tox
@@ -246,11 +246,6 @@ function tox_avahi {
 }
 
 function install_tox_node {
-	if [ $INSTALLING_MESH ]; then
-		mesh_tox_node
-		return
-	fi
-
 	if grep -Fxq "install_tox_node" $COMPLETION_FILE; then
 		return
 	fi
@@ -321,17 +316,6 @@ function install_tox_client {
 	echo 'install_tox_client' >> $COMPLETION_FILE
 }
 
-function install_tox {
-	if grep -Fxq "install_tox" $COMPLETION_FILE; then
-		return
-	fi
-	configure_firewall_for_tox
-	install_tox_node
-	tox_avahi
-	install_tox_client
-	echo 'install_tox' >> $COMPLETION_FILE
-}
-
 function mesh_tox_node {
 	# obtain commits from the main file
 	TOXCORE_COMMIT_MAIN=$(cat /usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-tox | grep "TOXCORE_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
@@ -567,4 +551,24 @@ function enable_tox_repo {
 	echo "Tox Repository Installed."
 }
 
+function install_tox {
+	if grep -Fxq "install_tox" $COMPLETION_FILE; then
+		return
+	fi
+
+	configure_firewall_for_tox
+
+	if [ $INSTALLING_MESH ]; then
+		mesh_tox_node
+		mesh_tox_avahi
+		mesh_tox_client
+	else
+		install_tox_node
+		tox_avahi
+		install_tox_client
+	fi
+
+	echo 'install_tox' >> $COMPLETION_FILE
+}
+
 # NOTE: deliberately no exit 0
diff --git a/src/freedombone-image-customise b/src/freedombone-image-customise
index b6d8a6f56..36311d449 100755
--- a/src/freedombone-image-customise
+++ b/src/freedombone-image-customise
@@ -520,13 +520,11 @@ initialise_mesh() {
 
 	INSTALLING_MESH=1
 
-	mesh_firewall
-	mesh_avahi
+	configure_firewall
+	install_avahi
 	install_batman
 	install_tox_node
-	mesh_tox_avahi
-	mesh_tox_client
-	mesh_web_server
+	install_web_server
 	install_zeronet
 
 	MESH_SERVICE='mesh-setup.service'
diff --git a/src/freedombone-utils-avahi b/src/freedombone-utils-avahi
index 95ced47ab..66b40f1df 100755
--- a/src/freedombone-utils-avahi
+++ b/src/freedombone-utils-avahi
@@ -94,8 +94,12 @@ function mesh_avahi {
 	chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
 }
 
-function configure_avahi {
-	if grep -Fxq "configure_avahi" $COMPLETION_FILE; then
+function install_avahi {
+	if [ $INSTALLING_MESH ]; then
+		mesh_avahi
+		return
+	fi
+	if grep -Fxq "install_avahi" $COMPLETION_FILE; then
 		return
 	fi
 	# only enable avahi if we're doing mesh networking
@@ -119,7 +123,7 @@ function configure_avahi {
 		sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
 	fi
 
-	echo 'configure_avahi' >> $COMPLETION_FILE
+	echo 'install_avahi' >> $COMPLETION_FILE
 }
 
 # NOTE: deliberately there is no "exit 0"
diff --git a/src/freedombone-utils-firewall b/src/freedombone-utils-firewall
index f7857378c..1f994cafd 100755
--- a/src/freedombone-utils-firewall
+++ b/src/freedombone-utils-firewall
@@ -47,6 +47,10 @@ function enable_ipv6 {
 }
 
 function configure_firewall {
+	if [ $INSTALLING_MESH ]; then
+		mesh_firewall
+		return
+	fi
     if grep -q "RELATED" /etc/firewall.conf; then
         # recreate the firewall to remove RELATED
         sed -i "/firewall/d" $COMPLETION_FILE
diff --git a/src/freedombone-utils-setup b/src/freedombone-utils-setup
index 2eb098dcb..86dd54a7a 100755
--- a/src/freedombone-utils-setup
+++ b/src/freedombone-utils-setup
@@ -290,7 +290,7 @@ function setup_utils {
 	create_upgrade_script
 	letsencrypt_renewals
 	install_watchdog_script
-	configure_avahi
+	install_avahi
 	create_avahi_onion_domains
 	#install_atheros_wifi
 	route_outgoing_traffic_through_tor
diff --git a/src/freedombone-utils-web b/src/freedombone-utils-web
index 98de56b64..925a14c81 100755
--- a/src/freedombone-utils-web
+++ b/src/freedombone-utils-web
@@ -392,7 +392,43 @@ function install_dynamicdns {
 	echo 'install_dynamicdns' >> $COMPLETION_FILE
 }
 
+function install_command_line_browser {
+	if grep -Fxq "install_command_line_browser" $COMPLETION_FILE; then
+		return
+	fi
+	apt-get -y install elinks
+
+	# set the home page
+	if ! grep -q "WWW_HOME" /home/$MY_USERNAME/.bashrc; then
+		if ! grep -q 'control' /home/$MY_USERNAME/.bashrc; then
+			echo "export WWW_HOME=$DEFAULT_SEARCH" >> /home/$MY_USERNAME/.bashrc
+		else
+			sed -i "/control/i export WWW_HOME=$DEFAULT_SEARCH" /home/$MY_USERNAME/.bashrc
+		fi
+	fi
+
+	echo 'install_command_line_browser' >> $COMPLETION_FILE
+}
+
+function mesh_web_server {
+	if [ -d /etc/apache2 ]; then
+		chroot "$rootdir" apt-get -y remove --purge apache2
+		chroot "$rootdir" rm -rf /etc/apache2
+	fi
+
+	chroot "$rootdir" apt-get -y install nginx
+
+	if [ ! -d $rootdir/etc/nginx ]; then
+		echo $'Unable to install web server'
+		exit 346825
+	fi
+}
+
 function install_web_server {
+	if [ $INSTALLING_MESH ]; then
+		mesh_web_server
+		return
+	fi
 	if [[ $SYSTEM_TYPE == "$VARIANT_CHAT" ]]; then
 		return
 	fi
@@ -530,36 +566,4 @@ function install_web_server {
 	echo 'install_web_server' >> $COMPLETION_FILE
 }
 
-function install_command_line_browser {
-	if grep -Fxq "install_command_line_browser" $COMPLETION_FILE; then
-		return
-	fi
-	apt-get -y install elinks
-
-	# set the home page
-	if ! grep -q "WWW_HOME" /home/$MY_USERNAME/.bashrc; then
-		if ! grep -q 'control' /home/$MY_USERNAME/.bashrc; then
-			echo "export WWW_HOME=$DEFAULT_SEARCH" >> /home/$MY_USERNAME/.bashrc
-		else
-			sed -i "/control/i export WWW_HOME=$DEFAULT_SEARCH" /home/$MY_USERNAME/.bashrc
-		fi
-	fi
-
-	echo 'install_command_line_browser' >> $COMPLETION_FILE
-}
-
-function mesh_web_server {
-	if [ -d /etc/apache2 ]; then
-		chroot "$rootdir" apt-get -y remove --purge apache2
-		chroot "$rootdir" rm -rf /etc/apache2
-	fi
-
-	chroot "$rootdir" apt-get -y install nginx
-
-	if [ ! -d $rootdir/etc/nginx ]; then
-		echo $'Unable to install web server'
-		exit 346825
-	fi
-}
-
 # NOTE: deliberately no exit 0
-- 
GitLab