local component = require "component"
local sides = require "sides"
local tA = {...}
if #tA < 3 then
io.stderr:write("usage: addalias
")
return
end
local alines = {}
local f = io.open("/etc/talias","rb")
if f then
for line in f:lines() do
local lt = {}
for word in line:gmatch("%S+") do
lt[#lt+1] = word
end
alines[string.format("%s\t%s",table.remove(lt,1), table.remove(lt,1))] = lt
end
f:close()
end
local addr = component.get(tA[1])
local side = tonumber(tA[2]) or sides[tA[2]]
local aname = string.format("%s\t%s",addr,side)
alines[aname] = alines[aname] or {}
alines[aname][#alines[aname]+1] = tA[3]
local f = io.open("/etc/talias","wb")
for k,v in pairs(alines) do
f:write(k)
for _,w in ipairs(v) do
f:write("\t"..w)
end
f:write("\n")
end
f:close()