Advertisement
Ocawesome101

ulos2-kickstart.lua

Nov 24th, 2022 (edited)
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. -- Kickstart the ULOS 2 install process
  2.  
  3. local ov = rawget(_G, "_OSVERSION")
  4.  
  5. local function printf(...)
  6.   io.write(string.format(...))
  7. end
  8.  
  9. local function fail(...)
  10.   io.stderr:write("failed: ", string.format(...), "\n")
  11.   os.exit(1)
  12. end
  13.  
  14. if not (ov and ov:match("OpenOS")) then
  15.   fail("this program requires OpenOS", 0)
  16. end
  17.  
  18. local args = {...}
  19.  
  20. local url = "http://ulos.pickardayune.com/v2/ulos2-" .. args[1] .. ".lua"
  21.  
  22. local component = require("component")
  23. local internet = component.list("internet")()
  24. local computer = require("computer")
  25. local fs = require("filesystem")
  26.  
  27. local function promptYN(thing, yes, no)
  28.   printf("%s ", thing)
  29.   local p = "[" .. (yes and "Y" or "y") .. "/" .. (no and "N" or "n") .. "] "
  30.   local input
  31.   repeat
  32.     io.write(p)
  33.     input = io.read("l")
  34.   until (yes or no) or input:match("^[ynYN]$")
  35.  
  36.   if input == "" and yes then input = "y" end
  37.  
  38.   return input == "Y" or input == "y"
  39. end
  40.  
  41. local function promptText(thing, default, canBeEmpty, verify)
  42.   local input
  43.   repeat
  44.     if default then
  45.       printf("%s [%s]: ", thing, default)
  46.     else
  47.       printf("%s: ", thing)
  48.     end
  49.  
  50.     input = io.read("*l")
  51.   until (verify and verify(input)) or canBeEmpty or #input > 0
  52.  
  53.   return input
  54. end
  55.  
  56. local root = fs.get("/").address
  57. local filesystems = {}
  58. for addr in component.list("filesystem") do
  59.   if addr ~= computer.tmpAddress() and
  60.       not component.invoke(addr, "isReadOnly") then
  61.     filesystems[#filesystems+1] = addr
  62.   end
  63. end
  64.  
  65. if #filesystems == 0 then
  66.   fail("No writable filesystems are available.")
  67. end
  68.  
  69. print([[
  70. This tool will assist you in setting up the first stage of the ULOS 2
  71. installation process.]])
  72.  
  73. local addr
  74. while true do
  75.   print("\nThe following filesystems are available:")
  76.  
  77.   for i=1, #filesystems do
  78.     printf("%2d. %s\n", i, filesystems[i])
  79.   end
  80.  
  81.   local num = tonumber(promptText("Select one", nil, false, function(x)
  82.     return filesystems[tonumber(x) or 0]
  83.   end))
  84.  
  85.   print("\n\27[31mAll data on the selected device will be erased.")
  86.   print("\27[31mProceed with caution.\27[37m")
  87.   if promptYN(string.format("Continue with %s?", filesystems[num]:sub(1,8)),
  88.       true, false) then
  89.     addr = filesystems[num]
  90.     break
  91.   end
  92. end
  93.  
  94. local path = "/install"
  95. if addr == root then
  96.   -- preload wget's needed libs so we can remove everything
  97.   require("internet")
  98.   require("shell")
  99.   require("text")
  100.   path = "/"
  101. else
  102.   fs.mount(addr, path)
  103. end
  104.  
  105. local wget = assert(loadfile("/bin/wget.lua"))
  106.  
  107. os.execute("rm -r "..path.."/*")
  108.  
  109. if not wget(url, path.."/init.lua") then
  110.   print("The media creation process has failed.")
  111.   if addr == root then
  112.     print("\27[31m"..[[
  113. Your system has been rendered unrecoverable. Use an OpenOS floppy disk to
  114. reinstall and try again.]].."\27[37m")
  115.     while true do coroutine.yield() end
  116.   end
  117. end
  118.  
  119. print("Setting media label")
  120. component.invoke(addr, "setLabel", "ULOS 2 Installer")
  121.  
  122. print("The media creation process has succeded.")
  123.  
  124. if computer.setBootAddress then
  125.   computer.setBootAddress(addr)
  126. end
  127.  
  128. if addr == root then
  129.   io.write("Rebooting in ")
  130.   for i=5, 1, -1 do
  131.     io.write(i.."...")
  132.     rawget(os, "sleep")(1)
  133.   end
  134.   computer.shutdown(true)
  135. elseif promptYN("Reboot now?", true, false) then
  136.   computer.shutdown(true)
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement