#!/bin/sh /etc/rc.common
# Copyright (c) 2021 OpenWrt.org

START=50

USE_PROCD=1
PROG=/usr/sbin/atfpolicy

add_option() {
	local type="$1"
	local name="$2"

	config_get val "$cfg" "$name"

	[ -n "$val" ] && json_add_$type "$name" "$val"
}

add_defaults() {
	cfg="$1"

	json_add_boolean reset 1

	add_option int vo_queue_weight
	add_option int update_pkt_threshold
	add_option int bulk_percent_thresh
	add_option int prio_percent_thresh
	add_option int weight_normal
	add_option int weight_prio
	add_option int weight_bulk
}


reload_service() {
	json_init

	config_load atfpolicy

	config_foreach add_defaults defaults

	ubus call atfpolicy config "$(json_dump)"
}

service_triggers() {
	procd_add_reload_trigger atfpolicy
}

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

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