OC-misc/repo-installer/instgen.lua

100 lines
2.2 KiB
Lua
Raw Normal View History

2018-08-06 19:22:21 +10:00
local serial = require "serialization"
local tArgs = {...}
local src, dest = tArgs[1], tArgs[2].."/"
local _OSVERSION = _OSVERSION or ""
local function normalisePath(path)
local pt = {}
for seg in path:gmatch("[^/]+") do
pt[#pt+1] = seg
end
pre = ""
if path:sub(1,1) == "/" then
pre = "/"
end
return pre .. table.concat(pt, "/")
end
local function wget(src,dest)
dest=normalisePath(dest)
local fstr = "wget '%s' -qO '%s'"
local command = string.format(fstr,src,dest)
print(command)
return os.execute(command)
end
local dirs = {}
local function mkdir(path)
path=normalisePath(path)
if dirs[path] then return true end
local fstr = "mkdir -p '%s'"
if _OSVERSION:sub(1,6) == "OpenOS" then
2020-10-19 16:46:58 +11:00
fstr = "mkdir '%s'"
end
local command = string.format(fstr,path)
print(command)
dirs[path] = true
return os.execute(command)
end
local function parsecfg(path)
path=normalisePath(path)
local f = io.open(path,"rb")
if not f then error("unable to open "..tostring(path).." for parsing") end
local rt = serial.unserialize(f:read("*a"))
f:close()
if type(rt) ~= "table" then error("unable to parse "..tostring(path)) end
return rt
end
local function writecfg(t,path)
path=normalisePath(path)
local f = io.open(path, "wb")
if not f then error("unable to open "..tostring(path).." for writing") end
f:write(serial.serialize(t))
f:close()
end
2018-08-06 19:22:21 +10:00
local pathpre = src:match("(.+/).+/.+")
print(pathpre)
mkdir(dest.."/master/")
local pcfgname = os.tmpname()
wget(src,pcfgname)
local programs = parsecfg(pcfgname)
os.execute("rm '"..pcfgname.."'")
2018-08-06 19:22:21 +10:00
local dlfiles = {}
for k,v in pairs(programs) do
if v.files then
for l,m in pairs(v.files) do
dlfiles[#dlfiles+1] = l
end
end
end
for k,v in pairs(dlfiles) do
local path,fn = v:match("(.+)/(.+)")
if v:sub(1,4) ~= "http" then
mkdir(dest..path)
wget(pathpre..v,dest..v)
else
mkdir(dest.."/external")
wget(v,dest.."/external/"..fn)
end
2018-08-06 19:22:21 +10:00
end
-- merge programs.cfg with existing if applicable
local w, oprograms = pcall(parsecfg, dest.."/master/programs.cfg")
if w then
for k,v in pairs(oprograms) do
programs[k] = programs[k] or v
end
end
writecfg(programs, dest.."/master/programs.cfg")
wget("https://git.shadowkat.net/izaya/OC-misc/raw/branch/master/repo-installer/repoinstaller.lua", dest.."/.install")