#!/bin/sh

. /lib/functions.sh

# exit if running in an extender
enabled="$(uci -q get uspc.global.enabled)"
if [ "$enabled" -eq 0 ]; then
	return 0;
fi

get_oui_from_db()
{
	db -q get device.deviceinfo.ManufacturerOUI
}

get_serial_from_db()
{
	db -q get device.deviceinfo.SerialNumber
}

fix_agent_endpoint()
{
	local AgentEndpointID serial oui user pass

	# Get endpoint id from obuspa config first
	config_load obuspa
	config_get AgentEndpointID localagent EndpointID ""
	if [ -z "${AgentEndpointID}" ]; then
		serial=$(get_serial_from_db)
		oui=$(get_oui_from_db)
		AgentEndpointID="os::${oui}-${serial//+/%2B}"
	fi

	uci -q set uspc.agent.EndpointID="${AgentEndpointID}"
}

fix_agent_endpoint
