#!/bin/sh
. /usr/share/libubox/jshn.sh

createRadioInformation() {
	local radio=$1
	local interface=$2
	local channel
	local frequency
	local ssid
	local config_data

	config_data="$(ubus call uci get {\"config\":\"wireless\",\"section\":\"$radio\"})"
	#Device does not have the radio
	if [ -z "$config_data" ]; then
		return
	fi

	#Gather information from iwinfo and uci
	json_load "$(ubus call iwinfo info {\"device\":\"$interface\"})"
	json_get_var channel "channel"
	json_get_var frequency "frequency"
	json_get_var ssid "ssid"

	json_load "$config_data"
	json_get_values config "values"
	#We will add the config via sed to the json object. There it needs the name config.
	#Furthermore, delete the first { and the last } to have the right number of brackets
	config=$(json_dump | sed 's/values/config/g' | sed 's/^{//' | sed '$s/}//' )

	#Add information to result
	json_init
	json_load "$result"
	json_add_object $radio
	json_add_int frequency $frequency
	json_add_int channel $channel
	json_add_string ssid $ssid

	#Now the Object has an empty config. Create the json string and then replace the empty config string with the filled one
	json_add_object config
	result=$(echo $(json_dump)  | sed "s/\"config\":\ *{\ *}/${config}/g" )
}

result="{}"
createRadioInformation "wifi0" "ath0"
createRadioInformation "wifi1" "ath1"
echo "$result"
