#!/bin/sh
. /lib/functions.sh
uci show network > /tmp/network_config
get_sid_from_vid() {
	local vid="$1"
	res="$(awk '/network.(.*).vlan='"'$vid'"'/ { split($1, a, ".") ; print a[2] }' /tmp/network_config)"
	[ -z "$res" ] && return 1
	echo $res
	return 0
}

replace_sid_with_vid() {
	local sid="$1"
	local opt="$2"
	local value
	config_get value "$sid" "$opt"
	[ -n "$value" ] && parsed="$(get_sid_from_vid "$value")" && uci_set dot1x "$sid" "$opt" "$parsed"
}

process_section() {
	replace_sid_with_vid "$1" "accept_vlan"
	replace_sid_with_vid "$1" "reject_vlan"
	replace_sid_with_vid "$1" "guest_vlan"
	replace_sid_with_vid "$1" "fallback_vlan"
}

config_load dot1x
config_foreach process_section port
rm /tmp/network_config
uci_commit dot1x
exit 0
