local wifi = {}

local DEFAULT_SIGNAL = "wifi::value"

local net = [[ printf "$(cat /sys/class/net/w*/operstate)~|~$(nmcli radio wifi)" ]]

--- callback: function(operstate, "up"|"down")
function wifi.get(callback)
  awful.spawn.easy_async_with_shell(net, function(out)
    local val = gears.string.split(out, "~|~")
    local w = "down"
    if val[2]:match("enabled") then
      w = "up"
    end
	callback(val[1], w)
  end)
end

function wifi.emit(name)
	wifi.get(function(...)
		awesome.emit_signal(name or DEFAULT_SIGNAL, ...)
	end)
end

function wifi.register_signal(signal_name, update_interval)
	gears.timer {
		timeout = update_interval or 5,
		call_now = true,
		autostart = true,
		callback = function() wifi.emit(signal_name) end
	}
end

return wifi
