#!/bin/sh /etc/rc.common

START=80

USE_PROCD=1
PROG=/usr/bin/ratelimit

add_rate() {
	local cfg="$1"
	config_get ssid "$cfg" ssid
	config_get ingress "$cfg" ingress
	config_get egress "$cfg" egress
	ubus call ratelimit defaults_set '{"name": "'$ssid'", "rate_ingress": "'$ingress'mbit", "rate_egress": "'$egress'mbit" }'
}

reload_service() {
	logger ratelimit reload
	config_load ratelimit
	config_foreach add_rate rate
}

service_triggers() {
	procd_add_reload_trigger ratelimit
}

start_service() {
	procd_open_instance
	procd_set_param command "$PROG"
	procd_set_param respawn
	procd_close_instance
}

service_started() {
	ubus -t 10 wait_for ratelimit
	[ $? = 0 ] && reload_service
}

