#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Network functions
#
# License
# =======
#
# Copyright (C) 2014-2016 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 Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# If the system is on an IPv6 network
IPV6_NETWORK='2001:470:26:307'

# The static IP address of the system within the local network
# By default the IP address is dynamic within your LAN
LOCAL_NETWORK_STATIC_IP_ADDRESS=

# IP address of the router (gateway)
ROUTER_IP_ADDRESS="192.168.1.254"

MESH_INSTALL_DIR=/var/lib

function install_static_network {
	if grep -Fxq "install_static_network" $COMPLETION_FILE; then
		return
	fi
	if [[ $INSTALLING_ON_BBB == "yes" ]]; then
		return
	fi
	if [ ! $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
		return
	fi

	echo '# The loopback network interface' > /etc/network/interfaces
	echo 'auto lo' >> /etc/network/interfaces
	echo 'iface lo inet loopback' >> /etc/network/interfaces
	echo '' >> /etc/network/interfaces
	echo '# The primary network interface' >> /etc/network/interfaces
	echo 'auto eth0' >> /etc/network/interfaces
	echo 'iface eth0 inet static' >> /etc/network/interfaces
	echo "    address $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/network/interfaces
	echo '    netmask 255.255.255.0' >> /etc/network/interfaces
	echo "    gateway $ROUTER_IP_ADDRESS" >> /etc/network/interfaces
	echo "    dns-nameservers $NAMESERVER1 $NAMESERVER2" >> /etc/network/interfaces
	echo '# Example to keep MAC address between reboots' >> /etc/network/interfaces
	echo '#hwaddress ether DE:AD:BE:EF:CA:FE' >> /etc/network/interfaces
	echo '' >> /etc/network/interfaces
	echo '# The secondary network interface' >> /etc/network/interfaces
	echo '#auto eth1' >> /etc/network/interfaces
	echo '#iface eth1 inet dhcp' >> /etc/network/interfaces
	echo '' >> /etc/network/interfaces
	echo '# WiFi Example' >> /etc/network/interfaces
	echo "#auto $WIFI_INTERFACE" >> /etc/network/interfaces
	echo "#iface $WIFI_INTERFACE inet dhcp" >> /etc/network/interfaces
	echo '#    wpa-ssid "essid"' >> /etc/network/interfaces
	echo '#    wpa-psk  "password"' >> /etc/network/interfaces
	echo '' >> /etc/network/interfaces
	echo '# Ethernet/RNDIS gadget (g_ether)' >> /etc/network/interfaces
	echo '# ... or on host side, usbnet and random hwaddr' >> /etc/network/interfaces
	echo '# Note on some boards, usb0 is automaticly setup with an init script' >> /etc/network/interfaces
	echo '#iface usb0 inet static' >> /etc/network/interfaces
	echo '#    address 192.168.7.2' >> /etc/network/interfaces
	echo '#    netmask 255.255.255.0' >> /etc/network/interfaces
	echo '#    network 192.168.7.0' >> /etc/network/interfaces
	echo '#    gateway 192.168.7.1' >> /etc/network/interfaces

	echo 'install_static_network' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
