#!/bin/sh

set -e
set -x

# https://openwrt.org/docs/guide-user/network/wifi/mesh/batman

# Notes:
# wpad-basic-wolfssl only has 802.11r and 802.11w support.
# wpad-mesh-openssl and wpad-mesh-wolfssl only have 802.11r/w and 802.11s support.
# wpad-openssl and wpad-wolfssl are the full version of wpad and have 802.11k/v/r/w and 802.11s support.
# The full version of wpad means that nothing was trimmed to reduce its size like the basic or mesh versions.

opkg update
opkg install batman-adv
opkg install kmod-batman-adv
opkg install batctl
opkg install wpad-openssl
opkg install luci-proto-batman-adv

# wget -c https://github.com/onemarcfifty/luci-proto-batman-adv/releases/download/batman-adv/luci-proto-batman-adv_git-22.104.47289-0a762fd_all.ipk

# enable batman-adv
uci set batman-adv.bat0.enabled='1'
uci commit batman-adv


# Set the Mesh Configuration:

uci set wireless.@wifi-iface[0].device='radio5pci'
uci set wireless.@wifi-iface[0].ifname='mesh0'
uci set wireless.@wifi-iface[0].network='nwi_mesh0'
uci set wireless.@wifi-iface[0].mode='mesh'
uci set wireless.@wifi-iface[0].mesh_fwding='0'
uci set wireless.@wifi-iface[0].mesh_id='<your advertised mesh "name" goes here>'
uci set wireless.@wifi-iface[0].encryption='psk2+ccmp'
uci set wireless.@wifi-iface[0].key='<your secure pass phrase goes here>'

uci commit wireless


# Set the bat0 Interface Configuration:

uci set network.bat0=interface
uci set network.bat0.proto='batadv'
uci set network.bat0.routing_algo='BATMAN_IV'
uci set network.bat0.aggregated_ogms='1'
uci set network.bat0.ap_isolation='0'
uci set network.bat0.bonding='0'
uci set network.bat0.fragmentation='1'
uci set network.bat0.gw_mode='off'
uci set network.bat0.log_level='0'
uci set network.bat0.orig_interval='1000'
uci set network.bat0.bridge_loop_avoidance='1'
uci set network.bat0.distributed_arp_table='1'
uci set network.bat0.multicast_mode='1'
uci set network.bat0.network_coding='0'
uci set network.bat0.hop_penalty='30'
uci set network.bat0.isolation_mark='0x00000000/0x00000000'

uci set network.bat0.ifname='mesh0'

uci set network.nwi_mesh0=interface
uci set network.nwi_mesh0.proto='batadv_hardif'
uci set network.nwi_mesh0.master='bat0'
uci set network.nwi_mesh0.mtu='2304'


uci commit network


