#!/bin/sh

. /lib/functions.sh

delete_overview_section() {
	local section="$1"
	local interface="$2"
	config_get id "$section" "id"
	config_get section_name "$section" "section_name"

	[ "$id" = "mobile_data_limit" ] || return
	[ "$section_name" = "$interface" ] || return
	uci_remove "overview" "$section"
	uci_commit "overview"
}

delete_network_section() {
	local interface
	local section="$1"
	interface=$(uci_get network "$section")

	[ -z "$interface" ] && {
		uci_remove "quota_limit" "$section"
		uci_commit "quota_limit"
		config_load "overview"
		config_foreach delete_overview_section "overview" "$section"
	}
}

fix_period_cb() {
	local sec="$1"
	local period

	config_get period "$sec" period 0
	[ "$period" -gt 0 ] && return

	config_get restart "$sec" restart 0
	[ "$restart" -gt 0 ] && {
		uci_set quota_limit "$sec" period "$restart"
		uci_remove quota_limit "$sec" restart
	}
}

config_load "quota_limit"
config_foreach fix_period_cb interface
config_foreach delete_network_section "interface"

exit 0