#!/usr/bin/lua

printf = function(s,...)
	io.write(s:format(...))
	local ss = s:format(...)
end

function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end

local upload = 0
local download = 0
local file = io.open("/tmp/usage.db", "r")
if file ~= nil then
	repeat
		local line = file:read("*line")
		if line == nil then
			break
		end
		local t = Split(line, ",", 6)
		download = download + t[2]
		upload = upload + t[3]
	until 1 == 0
	file:close()
end
total = upload + download
if total < 1000 then
	tstr = string.format("%d", total)
	tfm = "KB"
else
	if total < 1000000 then
		tstr = string.format("%g", total/1000)
		tfm = "MB"
	else
		tstr = string.format("%g", total/1000000)
		tfm = "GB"
	end
end
if upload < 1000 then
	upstr = string.format("%d", upload)
	ufm = "KB"
else
	if upload < 1000000 then
		upstr = string.format("%g", upload/1000)
		ufm = "MB"
	else
		upstr = string.format("%g", upload/1000000)
		ufm = "GB"
	end
end
if download < 1000 then
	dwnstr = string.format("%d", download)
	dfm = "KB"
else
	if download < 1000000 then
		dwnstr = string.format("%g", download/1000)
		dfm = "MB"
	else
		dwnstr = string.format("%g", download/1000000)
		dfm = "GB"
	end
end

--os.execute("/etc/wrtbwmon writedl " .. dwnstr .. " " .. dfm .. " " .. upstr .. " " .. ufm .. " " .. tstr .. " " .. tfm)
os.execute("/etc/wrtbwmon writesdl " .. dwnstr .. " " .. dfm .. " " .. upstr .. " " .. ufm .. " " .. tstr .. " " .. tfm .. " " .. total)

