#!/bin/sh /etc/rc.common

. /lib/functions.sh

generate_mapagent_config=0
generate_wireless_sta_config=0
generate_aps_config=1

network_mode="$(fw_printenv -n netmode)" # default is layer3
multiap_mode="$(fw_printenv -n multiap_mode)" # default is full
disable_mlo="$(fw_printenv -n disable_mlo)"

is_airoha() {
	[ -f /proc/device-tree/compatible ] || return
	strings /proc/device-tree/compatible | grep -qE '^(econet,|airoha,)'; return
}

is_broadcom() {
	[ -e /dev/bcm ] && return 0
	return 1
}

is_qualcomm() {
	[ -d /sys/module/ath12k ] && return 0
	return 1
}

generate_multiap_config() {
	devidx=0

	generate_config() {
		dev="$1"

		[ -n "$dev" ] || continue

		config_get band $dev band

		mode_band=""
		priority=
		dpp_chan=
		channels=

		case "$band" in
			2g)
				mode_band=2
				priority=2
				channels="1 6 11"
			;;
			5g)
				mode_band=5
				priority=1
				channels="36-64 100-112"
			;;
			6g)
				mode_band=6
				priority=0
			;;
		esac

		[ -n "$mode_band" ] || continue

		devidx=$(echo ${dev} |tr -dc '0-9')

		device="$dev"

		ifprefix_radio=""
		if is_airoha; then
			if [ -d "/sys/module/mt76" ]; then
				ifprefix="wlan%_%"
				ifname="wlan${devidx}_0"
				ifname_bh="wlan${devidx}_1"
			else
				uci set mapagent.agent.mld_prefix="bss"
				ifname_sta=""
				case "$band" in
					2g)
						ifprefix="ra%"
						ifname="ra0"
						ifname_bh="ra1"
						ifname_sta="apcli0"
					;;
					5g)
						ifprefix="rai%"
						ifname="rai0"
						ifname_bh="rai1"
						ifname_sta="apclii0"
					;;
					6g)
						ifprefix="rax%"
						ifname="rax0"
						ifname_bh="rax1"
						ifname_sta="apclix0"
					;;
				esac
				ifprefix_radio="${ifprefix}"
				if [ "${network_mode}" == "extender" ]; then
					ifname="${ifname_sta}"
				fi

				[ "$disable_mlo" == "1" ] || {
					uci set wireless.$dev.mlo="1"
					uci set wireless.$dev.mlo_capable="1"
				}
			fi

			uci set wireless.$dev.channels="$channels"
			uci commit wireless
		elif is_broadcom; then
			ifprefix="wl"
			ifname="wl${devidx}"
			ifname_bh="$ifname.1"
			device="wl${devidx}"
			brcm_setup="1"
		elif is_qualcomm; then
			ifprefix="wlan0%-"
			ifname="wlan${devidx}"
			ifname_bh="wlan${devidx}-1"
			ap_follow_sta_dfs="1"

			[ "$disable_mlo" == "1" ] || {
				uci set wireless.$dev.mlo="1"
				uci set wireless.$dev.mlo_capable="1"
				uci commit wireless

				# Disable for MLD/MLO
				uci set mapagent.agent.island_prevention="0"
				uci commit mapagent

				# wait ap-autoconfiguration
				generate_aps_config=0
			}
		else
			ifprefix="wlan%-"
			ifname="wlan$devidx"
			ifname_bh="$ifname-1"
		fi

		if [ $generate_mapagent_config -eq 1 ]; then
			if [ -z "${ifprefix_radio}" ]; then
				uci set mapagent.agent.ifprefix="$ifprefix"
			fi
			uci set mapagent.agent.brcm_setup="$brcm_setup"
			uci set mapagent.agent.ap_follow_sta_dfs="$ap_follow_sta_dfs"
			uci add mapagent radio
			uci set mapagent.@radio[-1].device="$device"
			uci set mapagent.@radio[-1].band="$mode_band"
			if [ -n "${ifprefix_radio}" ]; then
				uci set mapagent.@radio[-1].ifprefix="${ifprefix}"
			fi

			if [ "$network_mode" == "extender" ]; then
				uci add mapagent bsta
				uci set mapagent.@bsta[-1].device="$device"
				uci set mapagent.@bsta[-1].ifname="$ifname"
				uci set mapagent.@bsta[-1].band="$mode_band"
				uci set mapagent.@bsta[-1].priority="$priority"

				# add dpp_chirp section for 2.4GHz bSTA
				if [ $mode_band -eq 2 ]; then
					uci add mapagent dpp_chirp
					uci set mapagent.@dpp_chirp[-1].type="qrcode"
					uci set mapagent.@dpp_chirp[-1].device="$device"
					uci set mapagent.@dpp_chirp[-1].ifname="$ifname"
					uci set mapagent.@dpp_chirp[-1].band="$mode_band"
					for channel in $channels; do
						uci add_list mapagent.@dpp_chirp[-1].channel="$channel"
					done
				fi

				if [ $generate_wireless_sta_config -eq 1 ]; then
					secname="default_sta_${device}"
					uci set wireless.$secname=wifi-iface
					uci set wireless.$secname.device="$device"
					uci set wireless.$secname.mode="sta"
					uci set wireless.$secname.ifname="$ifname"
					uci set wireless.$secname.disabled="0"
					uci set wireless.$secname.default_disabled="1"
					uci set wireless.$secname.multi_ap="1"
					uci commit wireless
				else
					ubus call uci set "{\"config\":\"wireless\",\"type\":\"wifi-iface\", \
						\"match\":{\"mode\":\"sta\", \"ifname\":\"$ifname\", \"device\":\"$device\"},\"values\":{\"multi_ap\":\"1\"}}" 2>/dev/null
				fi
			else
				if [ $generate_aps_config -eq 1 ]; then
					uci add mapagent ap
					uci set mapagent.@ap[-1].type="fronthaul"
					uci set mapagent.@ap[-1].device="$device"
					uci set mapagent.@ap[-1].ifname="$ifname"
					uci set mapagent.@ap[-1].band="$mode_band"
					uci add mapagent ap
					uci set mapagent.@ap[-1].type="backhaul"
					uci set mapagent.@ap[-1].device="$device"
					uci set mapagent.@ap[-1].ifname="$ifname_bh"
					uci set mapagent.@ap[-1].band="$mode_band"

					if [ "$multiap_mode" != "none" ]; then
						ubus call uci set "{\"config\":\"wireless\",\"type\":\"wifi-iface\", \
							\"match\":{\"ifname\":\"$ifname\", \"device\":\"$device\"},\"values\":{\"multi_ap\":\"2\"}}" 2>/dev/null
						ubus call uci set "{\"config\":\"wireless\",\"type\":\"wifi-iface\", \
							\"match\":{\"ifname\":\"$ifname_bh\", \"device\":\"$device\"},\"values\":{\"multi_ap\":\"1\"}}" 2>/dev/null
					fi
				fi
			fi
		fi
	}

	config_load wireless
	config_foreach generate_config wifi-device
}

map_genconf () {
	if [ -f /etc/config/mapagent -a -z "$(uci -q get mapagent.@radio[0].band)" ]; then
		generate_mapagent_config=1
	fi

	if [ "$network_mode" == "extender" ]; then
		if ! uci show wireless | grep -q "mode=.*sta"; then
			generate_wireless_sta_config=1

			for section in $(uci show wireless | grep "mode=.*ap" | cut -d'.' -f2); do
				uci delete wireless.$section
			done
		fi
	fi

	generate_multiap_config

	if [ $generate_mapagent_config -eq 1 ]; then
		uci set mapagent.agent.enabled="1"
		if [ "$multiap_mode" == "agent" ]; then
			uci -q set mapagent.@controller_select[0].autostart=0
			uci -q set mapagent.@controller_select[0].local=0
		elif [ "$multiap_mode" == "auto" ]; then
			uci -q set mapagent.@controller_select[0].autostart=1
			uci -q set mapagent.@controller_select[0].local=0
		elif [ "$multiap_mode" == "none" ]; then
			uci set mapagent.agent.enabled="0"
		else # default to full
			uci -q set mapagent.@controller_select[0].autostart=1
			uci -q set mapagent.@controller_select[0].local=1
		fi
		uci -q commit mapagent

		if [ "$multiap_mode" == "agent" -o "$multiap_mode" == "none" ]; then
			uci set mapcontroller.controller.enabled="0"
		else
			uci set mapcontroller.controller.enabled="1"
			[ "$disable_mlo" == "1" ] && {
				mapcontroller_remove_mld() {
					uci delete mapcontroller.$1
				}
				mapcontroller_remove_mld_id() {
					uci delete mapcontroller.$1.mld_id
				}

				config_load mapcontroller
				config_foreach mapcontroller_remove_mld mld
				config_foreach mapcontroller_remove_mld_id ap
			}
		fi
		ubus -t 5 call uci commit '{"config":"mapcontroller"}'
	fi
}
