From 9b818711ab0bfd09eb2102ffe6e9485c3c6a62af Mon Sep 17 00:00:00 2001 From: Ale2610 Date: Fri, 31 Jul 2015 21:21:28 +0200 Subject: [PATCH] Create installer.lua --- src/installer.lua | 118 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/installer.lua diff --git a/src/installer.lua b/src/installer.lua new file mode 100644 index 0000000..a0eea18 --- /dev/null +++ b/src/installer.lua @@ -0,0 +1,118 @@ +if not pocket or not term.isColor() then + print("sPhone is only for Advanced Pocket Computers!") + return +end + +local files = { + ["src/sPhone.lua"] = "/.sPhone/sPhone", + ["src/apis/sha256.lua"] = "/.sPhone/apis/sha256", + ["src/apis/sertextext.lua"] = "/.sPhone/apis/sertextext", + ["src/apis/graphics.lua"] = "/.sPhone/apis/graphics", +} + +local githubUser = "Sertex-Team" +local githubRepo = "sPhone" +local githubBranch = "master" + + +local function clear() + term.setBackgroundColor(colors.white) + term.clear() + term.setCursorPos(1, 1) + term.setTextColor(colors.white) +end + +local function center(text) + local w, h = term.getSize() + local x, y = term.getCursorPos() + term.setCursorPos(math.ceil(w/2) - #text, y) + 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("") + +clear() + +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) + clear() + term.setCursorPos(2, 2) + center("sPhone") + print("") + center("Getting files") + term.setCursorPos(2, h - 1) + 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 + term.setCursorPos(2, 6) + print("Error getting file:") + term.setCursorPos(2, 7) + print(k) + sleep(1) + end + filesDownloaded = filesDownloaded + 1 +end +clear() +term.setCursorPos(2, 2) +center("sPhone") +print("") +center("sPhone installed!") +for i = 1, 5 do + write(".") + sleep(1) +end +os.reboot()