From 0c569eff66d7b3cc68569c4cf2eab50b2ccb44af Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Fri, 21 Oct 2016 10:24:07 +0100
Subject: [PATCH] Consolidation of wifi functions

---
 src/freedombone-mesh-batman |  28 +--
 src/freedombone-utils-wifi  | 342 ++++++++++++++++++++++++++++++++++++
 src/freedombone-wifi        | 330 +---------------------------------
 3 files changed, 344 insertions(+), 356 deletions(-)

diff --git a/src/freedombone-mesh-batman b/src/freedombone-mesh-batman
index 0e2b3a7b6..587c241a2 100755
--- a/src/freedombone-mesh-batman
+++ b/src/freedombone-mesh-batman
@@ -32,33 +32,7 @@ PROJECT_NAME='freedombone'
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
 HOTSPOT_PASSPHRASE='mesh'
 
-function count_wlan {
-    # counts the number of wlan devices
-    ctr=0
-
-    for i in $(seq 0 1 10); do
-        if grep -q "wlan${i}" /proc/net/dev; then
-            ctr=$((ctr + 1))
-        fi
-    done
-    echo $ctr
-}
-
-function update_wifi_adaptors {
-    IFACE=
-    IFACE_SECONDARY=
-
-    for i in $(seq 10 -1 0); do
-        if grep -q "wlan${i}" /proc/net/dev; then
-            if [ ! $IFACE ]; then
-                IFACE="wlan${i}"
-            else
-                IFACE_SECONDARY="wlan${i}"
-                return
-            fi
-        fi
-    done
-}
+source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
 
 if [[ $1 == "start" ]]; then
     # install avahi
diff --git a/src/freedombone-utils-wifi b/src/freedombone-utils-wifi
index 44f5db8e6..4b6676c43 100755
--- a/src/freedombone-utils-wifi
+++ b/src/freedombone-utils-wifi
@@ -131,4 +131,346 @@ function install_atheros_wifi {
     mark_completed $FUNCNAME
 }
 
+function update_wifi_adaptors {
+    IFACE=
+    IFACE_SECONDARY=
+
+    for i in $(seq 10 -1 0); do
+        if grep -q "wlan${i}" /proc/net/dev; then
+            if [ ! $IFACE ]; then
+                IFACE="wlan${i}"
+            else
+                IFACE_SECONDARY="wlan${i}"
+                return
+            fi
+        fi
+    done
+}
+
+function wifi_get_psk {
+    ssid=$1
+    passphrase=$2
+
+    psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
+    echo $psk
+}
+
+function hotspot_off {
+    if [ ! -f /etc/hostapd/hostapd.conf ]; then
+        return
+    fi
+    systemctl stop hostapd
+
+    rm /etc/hostapd/hostapd.conf
+
+    if [ -f /etc/network/interfaces_original ]; then
+        cp /etc/network/interfaces_original /etc/network/interfaces
+    else
+        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
+        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
+        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
+    fi
+
+    systemctl restart network-manager
+    ifdown $WIFI_INTERFACE
+}
+
+function hotspot_on {
+    if [ ! -f /etc/default/hostapd ]; then
+        echo $'/etc/default/hostapd was not found'
+        exit 67241
+    fi
+    if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
+        echo $'Wifi hotspot passphrase is too short'
+        exit 25719
+    fi
+
+    sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
+
+    echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
+    echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
+    echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
+    echo "country_code=UK" >> /etc/hostapd/hostapd.conf
+    echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
+    echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
+    echo 'channel=6' >> /etc/hostapd/hostapd.conf
+    echo 'wpa=2' >> /etc/hostapd/hostapd.conf
+    echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
+    echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
+    echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
+    echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
+    echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
+    echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo '## Shared Key Authentication ##'
+    echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
+    echo '' >> /etc/hostapd/hostapd.conf
+    echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
+    echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
+
+    if [ ! -f /etc/network/interfaces_original ]; then
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
+            cp /etc/network/interfaces /etc/network/interfaces_original
+        fi
+    fi
+
+    echo '# wifi enabled' > /etc/network/interfaces
+    echo 'auto lo br0' >> /etc/network/interfaces
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
+    echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
+    echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
+    echo 'iface eth0 inet manual' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo '# Setup bridge' >> /etc/network/interfaces
+    echo 'iface br0 inet static' >> /etc/network/interfaces
+    echo "    bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
+
+    systemctl restart network-manager
+    ifup $WIFI_INTERFACE
+    systemctl restart hostapd
+}
+
+function wifi_wpa2_psk {
+    ssid=$1
+    passphrase=$2
+
+    if [ ! -f /etc/network/interfaces_original ]; then
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
+            cp /etc/network/interfaces /etc/network/interfaces_original
+        fi
+    fi
+
+    echo '# wifi enabled' > /etc/network/interfaces
+    echo 'auto lo' >> /etc/network/interfaces
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
+
+    wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
+
+    systemctl restart network-manager
+    ifup ${WIFI_INTERFACE}
+}
+
+function wifi_none {
+    ssid=$1
+
+    if [ ! -f /etc/network/interfaces_original ]; then
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
+            cp /etc/network/interfaces /etc/network/interfaces_original
+        fi
+    fi
+
+    echo '# wifi enabled' > /etc/network/interfaces
+    echo 'auto lo' >> /etc/network/interfaces
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
+
+    echo 'network={' > $WIFI_CONFIG
+    echo "  ssid=\"${ssid}\"" >> $WIFI_CONFIG
+    echo '  key_mgmt=NONE' >> $WIFI_CONFIG
+    echo '}' >> $WIFI_CONFIG
+
+    systemctl restart network-manager
+    ifup ${WIFI_INTERFACE}
+}
+
+function networks_from_file {
+    if [ ! -f $WIFI_NETWORKS_FILE ]; then
+        exit 4
+    fi
+
+    if [ ! -f /etc/network/interfaces_original ]; then
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
+            cp /etc/network/interfaces /etc/network/interfaces_original
+        fi
+    fi
+
+    echo '# wifi enabled' > /etc/network/interfaces
+    echo 'auto lo' >> /etc/network/interfaces
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
+    echo '' >> /etc/network/interfaces
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
+
+    # remove wpa_supplicant.conf if it exists
+    if [ -f $WIFI_CONFIG ]; then
+        rm -f $WIFI_CONFIG
+    fi
+
+    ctr=0
+    while read -r line
+    do
+        if [ ${#line} -gt 1 ]; then
+            if [[ "$line" != '#'* ]]; then
+                if [ $ctr -eq 0 ]; then
+                    WIFI_SSID="$line"
+                fi
+                if [ $ctr -eq 1 ]; then
+                    WIFI_TYPE="$line"
+                    if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
+                        echo 'network={' >> $WIFI_CONFIG
+                        echo "  ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
+                        echo '  key_mgmt=NONE' >> $WIFI_CONFIG
+                        echo '}' >> $WIFI_CONFIG
+                        ctr=0
+                        continue
+                    fi
+                fi
+                if [ $ctr -eq 2 ]; then
+                    WIFI_PASSPHRASE="$line"
+                    wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
+                    ctr=0
+                    continue
+                fi
+
+                ctr=$((ctr + 1))
+            fi
+        fi
+    done < $WIFI_NETWORKS_FILE
+
+    systemctl restart network-manager
+    ifup ${WIFI_INTERFACE}
+}
+
+function create_networks_interactive {
+    update_wifi_adaptors
+    if [ ! $IFACE ]; then
+        # Don't try to configure wifi if there are no adaptors
+        return
+    fi
+
+    if [ -f $WIFI_NETWORKS_FILE ]; then
+        rm $WIFI_NETWORKS_FILE
+    fi
+
+    echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
+    echo '#' >> $WIFI_NETWORKS_FILE
+    echo $'# MySSID' >> $WIFI_NETWORKS_FILE
+    echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
+    echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
+    echo '#' >> $WIFI_NETWORKS_FILE
+    echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
+    echo $'# none' >> $WIFI_NETWORKS_FILE
+    echo '#' >> $WIFI_NETWORKS_FILE
+
+    wifi_ctr=0
+    wifi_networks_done=
+    while [ ! $wifi_networks_done ]
+    do
+        data=$(tempfile 2>/dev/null)
+        trap "rm -f $data" 0 1 2 5 15
+        dialog --backtitle $"Freedombone Configuration" \
+               --title $"Wifi Settings ${wifi_ctr}" \
+               --form $"\nIf you wish to use wifi and have a Free Software compatible adapter (eg. Atheros) rather than wired ethernet then enter the details below, otherwise just select Ok:" 15 55 4 \
+               $"SSID:" 1 1 "$WIFI_SSID" 1 16 30 30 \
+               $"Type:" 2 1 "$WIFI_TYPE" 2 16 10 10 \
+               $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 16 30 30 \
+               2> $data
+        sel=$?
+        case $sel in
+            1) return;;
+            255) return;;
+        esac
+        WIFI_SSID=$(cat $data | sed -n 1p)
+        WIFI_TYPE=$(cat $data | sed -n 2p)
+        WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
+
+        # if these fields are empty then there are no more wifi networks
+        if [ ${#WIFI_SSID} -lt 2 ]; then
+            wifi_networks_done='yes'
+            continue
+        fi
+        if [ ${#WIFI_TYPE} -lt 2 ]; then
+            wifi_networks_done='yes'
+            continue
+        fi
+
+        # update the wifi networks file
+        echo '' >> $WIFI_NETWORKS_FILE
+        echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
+        echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
+        if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
+            echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
+        fi
+
+        if [ ${#WIFI_SSID} -gt 1 ]; then
+            if [ ${#WIFI_TYPE} -gt 1 ]; then
+                if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
+                    return
+                else
+                    if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
+                        return
+                    fi
+                fi
+            fi
+        fi
+
+        # clear values
+        WIFI_SSID=
+        WIFI_PASSPHRASE=
+
+        wifi_ctr=$((wifi_ctr + 1))
+    done
+}
+
+function disable_wifi {
+    if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
+        hotspot_off
+        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
+        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
+        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
+        systemctl restart network-manager
+        ifdown ${WIFI_INTERFACE}
+    else
+        networks_from_file
+    fi
+}
+
+function count_wlan {
+    # counts the number of wlan devices
+    ctr=0
+
+    for i in $(seq 0 1 10); do
+        if grep -q "wlan${i}" /proc/net/dev; then
+            ctr=$((ctr + 1))
+        fi
+    done
+    echo $ctr
+}
+
 # NOTE: deliberately no exit 0
diff --git a/src/freedombone-wifi b/src/freedombone-wifi
index ff1e96e50..2cdfcfa47 100755
--- a/src/freedombone-wifi
+++ b/src/freedombone-wifi
@@ -50,335 +50,7 @@ WIFI_DISABLE=
 IFACE=
 IFACE_SECONDARY=
 
-function update_wifi_adaptors {
-    IFACE=
-    IFACE_SECONDARY=
-
-    for i in $(seq 10 -1 0); do
-        if grep -q "wlan${i}" /proc/net/dev; then
-            if [ ! $IFACE ]; then
-                IFACE="wlan${i}"
-            else
-                IFACE_SECONDARY="wlan${i}"
-                return
-            fi
-        fi
-    done
-}
-
-function wifi_get_psk {
-    ssid=$1
-    passphrase=$2
-
-    psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
-    echo $psk
-}
-
-function hotspot_off {
-    if [ ! -f /etc/hostapd/hostapd.conf ]; then
-        return
-    fi
-    systemctl stop hostapd
-
-    rm /etc/hostapd/hostapd.conf
-
-    if [ -f /etc/network/interfaces_original ]; then
-        cp /etc/network/interfaces_original /etc/network/interfaces
-    else
-        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
-        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
-        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
-    fi
-
-    systemctl restart network-manager
-    ifdown $WIFI_INTERFACE
-}
-
-function hotspot_on {
-    if [ ! -f /etc/default/hostapd ]; then
-        echo $'/etc/default/hostapd was not found'
-        exit 67241
-    fi
-    if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
-        echo $'Wifi hotspot passphrase is too short'
-        exit 25719
-    fi
-
-    sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
-
-    echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
-    echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
-    echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
-    echo "country_code=UK" >> /etc/hostapd/hostapd.conf
-    echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
-    echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
-    echo 'channel=6' >> /etc/hostapd/hostapd.conf
-    echo 'wpa=2' >> /etc/hostapd/hostapd.conf
-    echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
-    echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
-    echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
-    echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
-    echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
-    echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo '## Shared Key Authentication ##'
-    echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
-    echo '' >> /etc/hostapd/hostapd.conf
-    echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
-    echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
-
-    if [ ! -f /etc/network/interfaces_original ]; then
-        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
-            cp /etc/network/interfaces /etc/network/interfaces_original
-        fi
-    fi
-
-    echo '# wifi enabled' > /etc/network/interfaces
-    echo 'auto lo br0' >> /etc/network/interfaces
-    echo 'iface lo inet loopback' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
-    echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
-    echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
-    echo 'allow-hotplug eth0' >> /etc/network/interfaces
-    echo 'iface eth0 inet manual' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo '# Setup bridge' >> /etc/network/interfaces
-    echo 'iface br0 inet static' >> /etc/network/interfaces
-    echo "    bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
-
-    systemctl restart network-manager
-    ifup $WIFI_INTERFACE
-    systemctl restart hostapd
-}
-
-function wifi_wpa2_psk {
-    ssid=$1
-    passphrase=$2
-
-    if [ ! -f /etc/network/interfaces_original ]; then
-        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
-            cp /etc/network/interfaces /etc/network/interfaces_original
-        fi
-    fi
-
-    echo '# wifi enabled' > /etc/network/interfaces
-    echo 'auto lo' >> /etc/network/interfaces
-    echo 'iface lo inet loopback' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'allow-hotplug eth0' >> /etc/network/interfaces
-    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
-    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
-    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'iface default inet dhcp' >> /etc/network/interfaces
-
-    wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
-
-    systemctl restart network-manager
-    ifup ${WIFI_INTERFACE}
-}
-
-function wifi_none {
-    ssid=$1
-
-    if [ ! -f /etc/network/interfaces_original ]; then
-        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
-            cp /etc/network/interfaces /etc/network/interfaces_original
-        fi
-    fi
-
-    echo '# wifi enabled' > /etc/network/interfaces
-    echo 'auto lo' >> /etc/network/interfaces
-    echo 'iface lo inet loopback' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'allow-hotplug eth0' >> /etc/network/interfaces
-    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
-    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
-    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'iface default inet dhcp' >> /etc/network/interfaces
-
-    echo 'network={' > $WIFI_CONFIG
-    echo "  ssid=\"${ssid}\"" >> $WIFI_CONFIG
-    echo '  key_mgmt=NONE' >> $WIFI_CONFIG
-    echo '}' >> $WIFI_CONFIG
-
-    systemctl restart network-manager
-    ifup ${WIFI_INTERFACE}
-}
-
-function networks_from_file {
-    if [ ! -f $WIFI_NETWORKS_FILE ]; then
-        exit 4
-    fi
-
-    if [ ! -f /etc/network/interfaces_original ]; then
-        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
-            cp /etc/network/interfaces /etc/network/interfaces_original
-        fi
-    fi
-
-    echo '# wifi enabled' > /etc/network/interfaces
-    echo 'auto lo' >> /etc/network/interfaces
-    echo 'iface lo inet loopback' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'allow-hotplug eth0' >> /etc/network/interfaces
-    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
-    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
-    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
-    echo '' >> /etc/network/interfaces
-    echo 'iface default inet dhcp' >> /etc/network/interfaces
-
-    # remove wpa_supplicant.conf if it exists
-    if [ -f $WIFI_CONFIG ]; then
-        rm -f $WIFI_CONFIG
-    fi
-
-    ctr=0
-    while read -r line
-    do
-        if [ ${#line} -gt 1 ]; then
-            if [[ "$line" != '#'* ]]; then
-                if [ $ctr -eq 0 ]; then
-                    WIFI_SSID="$line"
-                fi
-                if [ $ctr -eq 1 ]; then
-                    WIFI_TYPE="$line"
-                    if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
-                        echo 'network={' >> $WIFI_CONFIG
-                        echo "  ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
-                        echo '  key_mgmt=NONE' >> $WIFI_CONFIG
-                        echo '}' >> $WIFI_CONFIG
-                        ctr=0
-                        continue
-                    fi
-                fi
-                if [ $ctr -eq 2 ]; then
-                    WIFI_PASSPHRASE="$line"
-                    wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
-                    ctr=0
-                    continue
-                fi
-
-                ctr=$((ctr + 1))
-            fi
-        fi
-    done < $WIFI_NETWORKS_FILE
-
-    systemctl restart network-manager
-    ifup ${WIFI_INTERFACE}
-}
-
-function create_networks_interactive {
-    update_wifi_adaptors
-    if [ ! $IFACE ]; then
-        # Don't try to configure wifi if there are no adaptors
-        return
-    fi
-
-    if [ -f $WIFI_NETWORKS_FILE ]; then
-        rm $WIFI_NETWORKS_FILE
-    fi
-
-    echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
-    echo '#' >> $WIFI_NETWORKS_FILE
-    echo $'# MySSID' >> $WIFI_NETWORKS_FILE
-    echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
-    echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
-    echo '#' >> $WIFI_NETWORKS_FILE
-    echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
-    echo $'# none' >> $WIFI_NETWORKS_FILE
-    echo '#' >> $WIFI_NETWORKS_FILE
-
-    wifi_ctr=0
-    wifi_networks_done=
-    while [ ! $wifi_networks_done ]
-    do
-        data=$(tempfile 2>/dev/null)
-        trap "rm -f $data" 0 1 2 5 15
-        dialog --backtitle $"Freedombone Configuration" \
-               --title $"Wifi Settings ${wifi_ctr}" \
-               --form $"\nIf you wish to use wifi and have a Free Software compatible adapter (eg. Atheros) rather than wired ethernet then enter the details below, otherwise just select Ok:" 15 55 4 \
-               $"SSID:" 1 1 "$WIFI_SSID" 1 16 30 30 \
-               $"Type:" 2 1 "$WIFI_TYPE" 2 16 10 10 \
-               $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 16 30 30 \
-               2> $data
-        sel=$?
-        case $sel in
-            1) return;;
-            255) return;;
-        esac
-        WIFI_SSID=$(cat $data | sed -n 1p)
-        WIFI_TYPE=$(cat $data | sed -n 2p)
-        WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
-
-        # if these fields are empty then there are no more wifi networks
-        if [ ${#WIFI_SSID} -lt 2 ]; then
-            wifi_networks_done='yes'
-            continue
-        fi
-        if [ ${#WIFI_TYPE} -lt 2 ]; then
-            wifi_networks_done='yes'
-            continue
-        fi
-
-        # update the wifi networks file
-        echo '' >> $WIFI_NETWORKS_FILE
-        echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
-        echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
-        if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
-            echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
-        fi
-
-        if [ ${#WIFI_SSID} -gt 1 ]; then
-            if [ ${#WIFI_TYPE} -gt 1 ]; then
-                if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
-                    return
-                else
-                    if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
-                        return
-                    fi
-                fi
-            fi
-        fi
-
-        # clear values
-        WIFI_SSID=
-        WIFI_PASSPHRASE=
-
-        wifi_ctr=$((wifi_ctr + 1))
-    done
-}
-
-function disable_wifi {
-    if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
-        hotspot_off
-        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
-        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
-        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
-        systemctl restart network-manager
-        ifdown ${WIFI_INTERFACE}
-    else
-        networks_from_file
-    fi
-}
+source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
 
 function show_help {
     echo ''
-- 
GitLab