From 8e18e26e47817569ed67e841bd8bba7e550e68b4 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 15 May 2024 01:12:03 +0200 Subject: [PATCH] add program updating script --- cc-tweaked/scripts/update.lua | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cc-tweaked/scripts/update.lua diff --git a/cc-tweaked/scripts/update.lua b/cc-tweaked/scripts/update.lua new file mode 100644 index 0000000..a4f87f3 --- /dev/null +++ b/cc-tweaked/scripts/update.lua @@ -0,0 +1,56 @@ +-- usage : update +-- +-- downloads + +local program = ... + +local function new_config(name) + local path = "/etc/" .. name + + local function read_key_in_line(line, key) + local prefix = key .. ":" + local prefix_len = string.len(prefix) + local actual = string.sub(line, 0, prefix_len) + if not (prefix == actual) then return nil end + local value = string.sub(line, prefix_len + 1) + return value + end + + local function get(key, default) + local file = fs.open(path, "r") + if file == nil then return default end + while true do + local line = file.readLine() + if line == nil then return default end + local value = read_key_in_line(line, key) + if not (value == nil) then return value end + end + end + + return { get = get } +end + +local function main() + if program == nil then + print("Usage: update ") + return + end + + local url = new_config("update").get("url", "http://cc.epitls.fr") + local url_ = url .. "/" .. program + local response = http.get(url_) + if (response == nil) or (type(response) == "string") then + print("HTTP Request to", url_, " failed :", response) + return + end + + local content = response.readAll() + local path = "./" .. program + if fs.exists(path) then fs.delete(path) end + local file = fs.open(path, "w") + file.write(content) + + print("Updated program", program) +end + +main()