#!/bin/sh
#
# Dieses Skript wird nur ein einziges Mal nach einem Upgrade oder der Erstinstallation ausgefuehrt:
#   http://wiki.openwrt.org/doc/uci#defaults
#

# shellcheck source=opennet/packages/on-core/files/usr/lib/opennet/on-helper.sh
. "${IPKG_INSTROOT:-}/usr/lib/opennet/on-helper.sh"


# add channel whitelist for 5GHz devices to /etc/config/wireless
# info: the additional option "chanlist" is enabled by one of our patches
# TODO: move this adjustment to a future "configure as wifi master" web interface action
find_all_uci_sections wireless wifi-device "band=5g" "channel=auto" | while read -r device_uci_prefix; do
	# do not touch non-empty chanlists
	[ -n "$(uci_get "${device_uci_prefix}.chanlist")" ] && continue
	# the device name could be "radio0" or similar
	device_name=$(echo "$device_uci_prefix" | cut -f 2 -d .)
	# do not touch devices without a master interface
	ap_interfaces=$(find_all_uci_sections wireless wifi-iface "device=$device_name" "mode=ap")
	[ -z "$ap_interfaces" ] && continue
	# Use one (random) indoor channel and multiple outdoor channels. This allows a usable
	# fallback even under bad conditions.
	indoor_channel=$(( 36 + 4 * $(get_random 4) ))
	# remove TDWR channels + bandwidth
	uci set "${device_uci_prefix}.chanlist=$indoor_channel 100-116 132-140"
	oldhtmode=$(uci_get "${device_uci_prefix}.htmode")
	# Fix 11ac default bandwidth
	if [ "$oldhtmode" = "VHT80" ]; then
	  uci set "${device_uci_prefix}.htmode=VHT20"
	fi
	uci commit "$device_uci_prefix"
done
