#!/bin/sh

if [ ! -f /etc/config/tunspace ]; then
  cat <<EOT > /etc/config/tunspace
package 'tunspace'

config tunspace "tunspace"
  # Namespace where the uplink will live.
  option uplink_netns "uplink"
  # Existing interface that we'll use as the uplink.
  option uplink_ifname "br-wan"
  # How the uplink in the namespace is constructed.
  # - bridge: creates a macvlan child in bridge mode, useful for creating multiple uplinks from the same original uplink interface.
  # - direct: moves the original uplink interface into the namespace directly, useful for wonky cheap USB sticks with broken drivers.
  option uplink_mode "bridge"
  # Our own static uplink IPv4 address in CIDR format. Leave empty to use DHCP.
  option uplink_ipv4 ""
  # IPv4 address of the gateway. Required in combination with uplink_ipv4, ignored when using DHCP.
  option uplink_gateway ""
  # Maintenance consists of checking the uplink, refreshing the DHCP lease, checking the tunnel endpoints, and switching endpoints if neccessary.
  option maintenance_interval "15"
  # Enables detailed output of Tunspace's operations. If disabled, only tunnel endpoint changes are reported.
  option debug "0"

config wg-server
  option name "ak36"
  option url "https://77.87.51.11/ubus"
  option insecure_cert "1"
  option disabled "0"

config wg-server
  option name "l105"
  option url "https://77.87.49.8/ubus"
  option insecure_cert "1"
  option disabled "0"

config wg-server
  option name "ohlauer"
  option url "https://176.74.57.19/ubus"
  option insecure_cert "1"
  option disabled "0"

config wg-server
  option name "saarbruecker"
  option url "https://176.74.57.43/ubus"
  option insecure_cert "1"
  option disabled "0"

config wg-server
  option name "strom"
  option url "https://77.87.51.131/ubus"
  option insecure_cert "1"
  option disabled "0"

config wg-interface
  option ifname "ts_wg0"
  option ipv6 "fe80::2/64"
  option ipv4 "10.31.174.138/32"
  option mtu "1280"
  option port "51820"
  option disabled "0"

EOT
fi

if [ ! -f /usr/share/tunspace/udhcpc.script ]; then
  mkdir -p /usr/share/tunspace
  cp /usr/share/udhcpc/default.script /usr/share/tunspace/udhcpc.script
  sed -i -e 's/ip -4 addr flush/#ip -4 addr flush/g' /usr/share/tunspace/udhcpc.script
fi
