sPhone/src/installer.lua

167 lines
3.8 KiB
Lua
Raw Normal View History

2016-03-06 16:01:30 +01:00
os.pullEvent = os.pullEventRaw
2015-07-31 21:21:28 +02:00
if not pocket or not term.isColor() then
print("sPhone is only for Advanced Pocket Computers!")
return
end
2015-11-11 18:38:22 +01:00
if fs.exists("/startup") then
fs.delete("/startup")
end
2015-10-27 19:02:28 +01:00
2016-03-10 17:16:08 +01:00
local files = {
2015-12-15 18:57:17 +01:00
["src/init.lua"] = "/.sPhone/init",
2015-08-21 15:45:33 +02:00
["src/sPhone.lua"] = "/.sPhone/sPhone",
2015-08-19 11:45:35 +02:00
2015-12-19 14:15:05 +01:00
["install"] = "/.sPhone/update",
["LICENSE"] = "/.sPhone/LICENSE",
2015-08-21 15:45:33 +02:00
["src/apis/sha256.lua"] = "/.sPhone/apis/sha256",
2015-11-27 22:28:35 +01:00
["src/apis/visum.lua"] = "/.sPhone/apis/visum",
2015-08-29 15:30:37 +02:00
["src/apis/graphics.lua"] = "/.sPhone/apis/graphics",
2015-08-19 11:45:35 +02:00
["src/apis/ui.lua"] = "/.sPhone/apis/ui",
2015-08-29 15:30:37 +02:00
["src/apis/base64.lua"] = "/.sPhone/apis/base64",
2015-09-25 20:18:25 +02:00
["src/apis/config.lua"] = "/.sPhone/apis/config",
2016-04-01 15:23:14 +02:00
["src/apis/shttp.lua"] = "/.sPhone/apis/shttp",
2015-08-19 11:45:35 +02:00
2016-07-01 12:19:07 +02:00
["src/bin/wget.lua"] = "/bin/wget",
2016-07-01 12:30:32 +02:00
["src/bin/halt.lua"] = "/bin/halt",
2016-05-16 19:38:58 +02:00
2015-08-19 11:45:35 +02:00
["src/apps/system/settings.lua"] = "/.sPhone/apps/system/settings",
2015-08-21 15:45:33 +02:00
["src/apps/system/info.lua"] = "/.sPhone/apps/system/info",
2015-11-01 12:13:54 +01:00
["src/apps/kstwallet.lua"] = "/.sPhone/apps/kstwallet",
2015-12-24 16:19:34 +01:00
["src/apps/store.lua"] = "/.sPhone/apps/store",
2015-12-25 22:00:13 +01:00
["src/apps/shell.lua"] = "/.sPhone/apps/shell",
2015-08-19 11:45:35 +02:00
["src/apps/gps.lua"] = "/.sPhone/apps/gps",
["src/interfaces/login"] = "/.sPhone/interfaces/login",
2015-10-10 14:44:02 +02:00
["src/interfaces/bootImage"] = "/.sPhone/interfaces/bootImage",
2015-08-19 11:45:35 +02:00
2015-08-29 15:30:37 +02:00
["src/startup"] = "/startup",
2015-11-04 17:19:16 +01:00
["src/startup"] = "/.sPhone/startup",
2015-07-31 21:21:28 +02:00
}
2016-06-04 21:32:36 +02:00
local githubUser = "BeaconNet"
2015-07-31 21:21:28 +02:00
local githubRepo = "sPhone"
local githubBranch = "master"
2016-03-06 16:01:30 +01:00
local w, h = term.getSize()
2015-07-31 21:21:28 +02:00
local function clear()
term.setBackgroundColor(colors.white)
2015-07-31 21:26:59 +02:00
term.clear()
term.setCursorPos(1, 1)
term.setTextColor(colors.black)
2015-07-31 21:21:28 +02:00
end
2016-03-06 16:01:30 +01:00
local function gui()
clear()
paintutils.drawLine(1,1,w,1,colors.blue)
term.setTextColor(colors.white)
term.setCursorPos(2,1)
print("sPhone Installer")
term.setCursorPos(1,2)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
end
2015-08-21 14:23:38 +02:00
local function center(text, y)
2015-07-31 21:21:28 +02:00
local w, h = term.getSize()
2015-08-21 14:23:38 +02:00
if not y then
local x, y = term.getCursorPos()
end
2016-03-06 16:01:30 +01:00
term.setCursorPos(math.ceil(w/2)-math.ceil(#text/2), y)
2015-07-31 21:21:28 +02:00
write(text)
end
local function httpGet(url, save)
if not url then
error("not enough arguments, expected 1 or 2", 2)
end
local remote = http.get(url)
if not remote then
return false
end
local text = remote.readAll()
remote.close()
if save then
local file = fs.open(save, "w")
file.write(text)
file.close()
return true
end
return text
end
local function get(user, repo, bran, path, save)
if not user or not repo or not bran or not path then
error("not enough arguments, expected 4 or 5", 2)
end
local url = "https://raw.github.com/"..user.."/"..repo.."/"..bran.."/"..path
local remote = http.get(url)
if not remote then
return false
end
local text = remote.readAll()
remote.close()
if save then
local file = fs.open(save, "w")
file.write(text)
file.close()
return true
end
return text
end
local function getFile(file, target)
return get(githubUser, githubRepo, githubBranch, file, target)
end
shell.setDir("")
2016-03-06 16:01:30 +01:00
gui()
2015-07-31 21:21:28 +02:00
local fileCount = 0
for _ in pairs(files) do
fileCount = fileCount + 1
end
local filesDownloaded = 0
local w, h = term.getSize()
for k, v in pairs(files) do
term.setTextColor(colors.black)
term.setBackgroundColor(colors.white)
2016-03-06 16:01:30 +01:00
gui()
center(" Downloading files",6)
term.setCursorPos(1,12)
term.clearLine()
center(" "..filesDownloaded.."/"..fileCount, 12)
2015-07-31 21:21:28 +02:00
local ok = k:sub(1, 4) == "ext:" and httpGet(k:sub(5), v) or getFile(k, v)
if not ok then
if term.isColor() then
term.setTextColor(colors.red)
end
2016-03-06 16:01:30 +01:00
term.setCursorPos(2, 16)
2015-07-31 21:21:28 +02:00
print("Error getting file:")
2016-03-06 16:01:30 +01:00
term.setCursorPos(2, 17)
2015-07-31 21:21:28 +02:00
print(k)
2016-03-06 16:01:30 +01:00
sleep(1.5)
2015-07-31 21:21:28 +02:00
end
filesDownloaded = filesDownloaded + 1
end
2016-03-06 16:01:30 +01:00
term.setCursorPos(1,12)
term.clearLine()
center(" "..filesDownloaded.."/"..fileCount, 12)
2015-10-27 15:56:03 +01:00
2015-11-11 18:45:20 +01:00
if not fs.exists("/startup") then
fs.copy("/.sPhone/startup","/startup")
end
2016-03-06 16:01:30 +01:00
center(" sPhone installed!",h-2)
center(" Rebooting...",h-1)
sleep(2)
2015-07-31 21:21:28 +02:00
os.reboot()