1
0
Fork 0
cc-stuff/turtos/startup.lua

58 lines
1.5 KiB
Lua
Raw Normal View History

2024-01-12 19:29:53 +03:00
if not turtle then
printError("not a turtle")
return
end
2024-01-12 22:04:57 +03:00
local base_path = "https://git.salushnes.solutions/hkc/cc-stuff/raw/branch/master/turtos"
2024-01-12 19:26:51 +03:00
_G._TOS_VER = "N/A"
local tos_ver_fp = io.open("/.tos-ver", "r")
if tos_ver_fp then
_G._TOS_VER = tos_ver_fp:read("l")
tos_ver_fp:close()
end
2024-01-12 22:04:57 +03:00
local function getFile(url, path)
local r, err = http.get(url)
io.write("GET " .. path .. " ... ")
if not r then
print("FAIL: " .. err)
return false, err
end
io.open(path, "w"):write(r.readAll()):close()
io.write("OK\n")
end
local req, err = http.get(base_path .. "/update.json")
if not req then
printError("Failed to get update info:", err)
else
local info = textutils.unserializeJSON(req.readAll())
req.close()
2024-01-12 19:26:51 +03:00
print("OTP version: " .. info.ver)
print("H/W version: " .. _TOS_VER)
2024-01-12 22:04:57 +03:00
2024-01-12 19:26:51 +03:00
if info.ver == _TOS_VER then
print("Running on latest firmware")
else
print("Performing an update...")
for i = 1, #info.files do
local file = info.files[i]
term.write(file.dst, "...")
if fs.exists(file.dst) then
term.write(" [DEL] ...")
fs.delete(file.dst)
end
2024-01-12 22:04:57 +03:00
getFile(base_path .. file.src, file.dst)
2024-01-12 19:26:51 +03:00
end
print("Writing new version info")
io.open("/.tos-ver", "w"):write(info.ver):close()
print("Rebooting")
return os.reboot()
end
end
shell.run("fg", "main.lua")