OC-PsychOS/exec/pkg.lua

74 lines
1.6 KiB
Lua
Raw Permalink Normal View History

local tA = {...}
local rf = tA[2]
local wget = wget or loadfile("/boot/exec/wget.lua")
if not wget then
print("wget is required to run this program")
elseif rf and pcall(fs.exists,rf) then
local pref = tA[3] or "/boot/"
local mode = "install"
if tA[1]:sub(1,1) == "r" then
mode = "remove"
end
local files = {}
local lrf = "/tmp/rf.txt"
local function precipe(s)
local t = {}
for line in s:gmatch("[^\n\r]+") do
t[#t+1] = {line:match("(.+)[ \t](.+)")}
local S = t[#t][1]
t[#t][1] = ""
for c in S:gmatch(".") do
if (string.byte(c) > 31 and string.byte(c) < 127) then
t[#t][1] = t[#t][1]..c
end
local S = t[#t][2]
t[#t][2] = ""
for c in S:gmatch(".") do
if (string.byte(c) > 31 and string.byte(c) < 127) then
t[#t][2] = t[#t][2]..c
end
end
end
end
return t
end
local function dl(p,u)
if u:sub(1,8) == "https://" or u:sub(1,7) == "http://" then
wget(u,pref..p)
elseif u == "dir" then
fs.mkdir(pref..p)
else
fs.cp(u,pref..p)
end
end
if rf:sub(1,8) == "https://" or rf:sub(1,7) == "http://" then
wget(rf,"/tmp/rf.txt")
else
lrf = rf
end
local f=io.open(lrf)
if not f then return false, "no recipe" end
files=precipe(f:read("*a"))
f:close()
local total = #files
print(mode.." the following files to "..pref.."?")
for k,v in ipairs(files) do io.write(v[2].." ") end
io.write("\n[yN] ")
local c = io.read():lower():sub(1,1)
if c ~= "y" then return false, "aborted" end
for k,v in ipairs(files) do
print(tostring(k).."/"..tostring(total)..": "..pref..v[2])
if mode == "install" then
dl(v[2],v[1])
elseif mode == "remove" then
fs.rm(pref..v[2])
end
end
end