#!/bin/sh

THIS_UPLINKNAME="tunnelberlin_tunneldigger"

. /lib/functions/freifunk-berlin-network.sh 
. /lib/functions/guard.sh
. /lib/functions/system.sh

# always set correct masquerading, regardless of guard
uci set firewall.zone_ffuplink.masq=1
uci commit firewall

current_preset=$(uci get ffberlin-uplink.preset.current)
if [ ${current_preset} != ${THIS_UPLINKNAME} ]; then
  if [ ${current_preset} != "undefined" ]; then
      # when the uplink-preset has changed, recreate remember the preset we are coming from
      # and prepare for reinit of the relevant settings via freifunk-berlin-ffuplink-defaults
      # uci-defaults
      logger -t "ffuplink" "uplink-preset has been changed."
      uci set ffberlin-uplink.preset.previous=${current_preset}
      create_ffuplink
  fi
  uci set ffberlin-uplink.preset.current=${THIS_UPLINKNAME}
fi
# set set auth-type required for this uplink-type, e.g. for freifunk-wizard
uci set ffberlin-uplink.uplink.auth=none

macaddr=$(uci -q get ffberlin-uplink.uplink.macaddr)
if [ -z "$macaddr" ]; then
  macaddr=$(generate_random_mac_hex "fe")
  uci set ffberlin-uplink.uplink.macaddr=$macaddr
fi

uci commit ffberlin-uplink

guard "tunnelberlin_tunneldigger"

uci delete network.ffuplink_dev
uci set network.ffuplink_dev=device
uci set network.ffuplink_dev.name=ffuplink
uci set network.ffuplink_dev.macaddr=$macaddr
uci commit network.ffuplink_dev

uci set network.ffuplink.proto=dhcp
uci commit network.ffuplink

# tunneldigger setup
UUID=$(uci -q get tunneldigger.ffuplink.uuid)
if [ $? -eq 1 ]; then
  UUID=$macaddr
  for byte in 7 8 9 10; do
    UUID=$UUID`dd if=/dev/urandom bs=1 count=1 2> /dev/null | hexdump -e '1/1 ":%02x"'`
  done
fi

uci delete tunneldigger.ffuplink
uci set tunneldigger.ffuplink=broker
uci -q delete tunneldigger.ffuplink.address
for srv in a b c d e f; do
  uci add_list tunneldigger.ffuplink.address=$srv.tunnel.berlin.freifunk.net:8942
done
uci set tunneldigger.ffuplink.uuid=$UUID
uci set tunneldigger.ffuplink.interface=ffuplink
uci set tunneldigger.ffuplink.broker_selection=usage
uci set tunneldigger.ffuplink.bind_interface=wan
uci set tunneldigger.ffuplink.enabled=1
uci commit tunneldigger

