#!/bin/sh
. /lib/functions.sh

cfg=mapagent
config_load $cfg

if uci -q get $cfg.agent.partial_wifi_reload >/dev/null; then
	uci -q set $cfg.agent.partial_wifi_reload='0'
fi

if uci -q get $cfg.agent.chan_ch_relay_mcast >/dev/null; then
	uci -q set $cfg.agent.chan_ch_relay_mcast='0'
fi

rename_dpp_uri() {
	local section="$1"
	local type device ifname band
	local ch_list=""

	config_get type "$section" type
	config_get device "$section" device
	config_get ifname "$section" ifname
	config_get band "$section" band
	config_get chirp_interval "$section" chirp_interval

	append_chan() {
		local val="$1"
		local chan_num="${val#*/}"
		ch_list="$ch_list $chan_num"
	}
	config_list_foreach "$section" chan append_chan

	new_section=$(uci add "$cfg" dpp_chirp)
	[ -n "$type" ] && uci set "$cfg.$new_section.type=$type"
	[ -n "$device" ] && uci set "$cfg.$new_section.device=$device"
	[ -n "$ifname" ] && uci set "$cfg.$new_section.ifname=$ifname"
	[ -n "$band" ] && uci set "$cfg.$new_section.band=$band"

	if [ -n "$chirp_interval" ]; then
		uci set "${cfg}.${new_section}.chirp_interval=$chirp_interval"
	fi

	for ch in $ch_list; do
		uci add_list "$cfg.$new_section.channel=$ch"
	done
	uci delete "$cfg.$section"
}

uci -q delete "$cfg.@dpp_controller[0]"

config_foreach rename_dpp_uri dpp_uri

exit 0
