#!/usr/bin/lua

download = arg[1]
dwnunits = arg[2]
upload = arg[3]
upunits = arg[4]
total = arg[5]
totunits = arg[6]
iface = arg[7]
tdown = arg[8]
tup = arg[9]
ttot = arg[10]

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

if dwnunits == "MiB" then
	download = download * 1000
end
if upunits == "MiB" then
	upload = upload * 1000
end
if totunits == "MiB" then
	total = total * 1000
end
if dwnunits == "GiB" then
	download = download * 1000000
end
if upunits == "GiB" then
	upload = upload * 1000000
end
if totunits == "GiB" then
	total = total * 1000000
end

tdown = tdown + download
tup = tup + upload
ttot = ttot + total

printf("vnstat = %s %s %s %s\n", iface, download, upload, total)
printf("vnstat = %s %s %s %s\n", iface, tdown, tup, ttot)

local file = io.open("/tmp/busage.db", "a")
file:write(iface, ",", download, ",", upload, ",", download, ",", upload, ",", 0, ",", 0, "\n")
file:close()

file = io.open("/tmp/busage.var", "w")
file:write("DOWN=\"" .. tdown .. "\"\n")
file:write("UP=\"" .. tup .. "\"\n")
file:write("TOTAL=\"" .. ttot .. "\"\n")
file:close()
