#!/bin/sh

. /lib/functions.sh

cfg=mapcontroller

config_load $cfg

adapt_sta_steering() {
	steer_module=$(uci -q get $cfg.@sta_steering[0].steer_module)
	bandsteer=$(uci -q get $cfg.@sta_steering[0].bandsteer)
	enabled=$(uci -q get $cfg.@sta_steering[0].enabled)
	diffsnr=$(uci -q get $cfg.@sta_steering[0].diffsnr)
	stri=$(uci -q get $cfg.@sta_steering[0].steer_retry_int)
	sti=$(uci -q get $cfg.@sta_steering[0].steer_int)
	stdi=$(uci -q get $cfg.@sta_steering[0].steer_disable_int)

	uci -q del $cfg.@sta_steering[0].enabled
	uci -q del $cfg.@sta_steering[0].steer_module
	uci -q del $cfg.@sta_steering[0].use_bcn_metrics
	uci -q del $cfg.@sta_steering[0].use_usta_metrics
	uci -q del $cfg.@sta_steering[0].bandsteer
	uci -q del $cfg.@sta_steering[0].diffsnr

	uci del_list $cfg.@sta_steering[0].plugins="rcpi"
	uci add_list $cfg.@sta_steering[0].plugins="rcpi"
	uci -q set $cfg.@sta_steering[0].plugins_enabled="1"
	uci -q set $cfg.@sta_steering[0].plugins_policy="any"

	# re-apply any custom legacy value(s) in 'sta-steer' section
	if [ -n "${enabled}" -o -n "${bandsteer}" -o -n "${diffsnr}" -o -n "${sti}" -o -n "${stri}" -o -n "${stdi}" ]; then
		# create 'rcpi' named 'sta-steer' section if there is none
		[ $(uci -q get mapcontroller.rcpi) ] || uci set $cfg.rcpi=sta-steer

		# set custom value(s)
		[ -z "${enabled}" ] || uci -q set $cfg.rcpi.enabled="${enabled}"
		[ -z "${bandsteer}" ] || uci -q set $cfg.rcpi.bandsteer="${bandsteer}"
		[ -z "${diffsnr}" ] || uci -q set $cfg.rcpi.diffsnr="${diffsnr}"
		[ -z "${sti}" ] || uci -q set $cfg.rcpi.steer_int="${sti}"
		[ -z "${stri}" ] || uci -q set $cfg.rcpi.steer_retry_int="${stri}"
		[ -z "${stdi}" ] || uci -q set $cfg.rcpi.steer_disable_int="${stdi}"
		uci reorder $cfg.rcpi=2
	fi
}

adapt_sta_steering
