#/bin/sh

sta_ipv6_addr=""
sta_list_file="/tmp/ETHERNET_CLIENT_LIST"
tmp_sta="/tmp/client_info"
ethernet_client_lock="/var/run/ethernet_client_checking.lock"
timeout=3

[ -e "$ethernet_client_lock" ] && return

touch $ethernet_client_lock

for d in $(foreach wireless wifi-iface | grep ssid); do
	if [ $(uci get wireless.$d.disabled) == "0" -a $(uci get wireless.$d.tc_enabled) == "1" ]; then
		for sta in $(wlanconfig `uci get wireless.$d.ifname` list sta | grep -v ADDR | grep -E ^[0-9A-Fa-f] | awk {'print $1'}); do
			[ -e "$tmp_sta" ] && rm -f "$tmp_sta"
			sta_ipv6_addr=`/sbin/mesh.sh mac_to_ipv6_unique_local $sta`
#			echo "sta $sta $sta_ipv6_addr"
			curl --connect-timeout $timeout -o $tmp_sta -s -k -X GET "https://[$sta_ipv6_addr]:4430/api/net/ethernet/client_info" -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer"
			if [ -e "$tmp_sta" ]; then
				[ "$(cat $tmp_sta | jq .status_code)" == "200" ] && cat $tmp_sta | jq -r .data.client[].mac_addr >> $sta_list_file"2"
			fi
		done
	fi
done

if [ -e "${sta_list_file}2" ]; then
	mv ${sta_list_file}2 $sta_list_file
fi

rm $ethernet_client_lock
