DorianH935

installBingOS.lua

Jun 7th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. print("Starting installer...")
  2. local rootPath
  3.  
  4. local args = { ... }
  5.  
  6. local function getFile(name)
  7. local file, err = http.get(rootPath .. name .. "?" .. os.epoch('utc'))
  8. local content
  9. if file then
  10. content = file.readAll()
  11. file.close()
  12. end
  13. return content, err
  14. end
  15.  
  16. local function downloadFile(name)
  17. local content = getFile(name)
  18. if not content then
  19. printError("Failed to download file: " .. name)
  20. return false
  21. end
  22. local file = fs.open(name, "w")
  23. file.write(content)
  24. file.close()
  25. return true
  26. end
  27.  
  28. local function getInstallationInformation(tag)
  29. rootPath = "https://raw.githubusercontent.com/znepb-cc/zwm/" .. (tag or "zwm")
  30. print("Fetching installation information...")
  31. local inst, err = getFile("/inst/installation.lua")
  32. if not inst then
  33. printError(("Could not fetch installation information for branch %s."):format(tag))
  34. return nil
  35. else
  36. instData = textutils.unserialize(inst)
  37. end
  38. if not instData then
  39. error("Error parsing installation information")
  40. end
  41. return instData
  42. end
  43.  
  44. local function getLatestTag()
  45. rootPath = "https://raw.githubusercontent.com/znepb-cc/zwm/zwm"
  46. print("Fetching latest tag...")
  47. local release, err = getFile("/inst/tag.txt")
  48. if err then
  49. printError("Could not fetch release tag. Please try again later.")
  50. return nil
  51. end
  52. return release
  53. end
  54.  
  55. local function install(inst, release)
  56. print(("Got version %s"):format(release))
  57. print(("Release type: %s"):format(inst.releaseType))
  58. write(("Required space: %d"):format(inst.spaceRequirement))
  59. if fs.getFreeSpace("/") < inst.spaceRequirement then
  60. printError("Unable to install zwm. Reason: Not enough space")
  61. return
  62. else
  63. print(" (OK)")
  64. end
  65.  
  66. print(("Setup will create %d directories and will install %d files."):format(#inst.directories, #inst.files))
  67. write("Confirm? Y/n ")
  68. local ready = read()
  69. if string.lower(ready) == "n" then
  70. print("Installation canceled.")
  71. return
  72. end
  73.  
  74. print("Creating directories...")
  75. for i, v in pairs(inst.directories) do
  76. print(("Creating: %s"):format(v))
  77. fs.makeDir(v)
  78. end
  79.  
  80. print("Downloading files...")
  81. for i, v in pairs(inst.files) do
  82. print(("Downloading: %s"):format(v))
  83. if not downloadFile(v) then
  84. printError("Failed to download file: " .. v)
  85. return
  86. end
  87. end
  88.  
  89. print("Installation complete")
  90. local sha256 = require("/lib/sha256")
  91.  
  92. print("Please set a username and password")
  93. write("Username: ")
  94. local username = read()
  95. write("Password: ")
  96. local password = sha256(read("*"))
  97. local data = {
  98. {
  99. name = username,
  100. passwordHash = password,
  101. homeDir = "/home/" .. username
  102. }
  103. }
  104.  
  105. local file = fs.open("/etc/accounts.cfg", "w")
  106. file.write(textutils.serialize(data))
  107. file.close()
  108.  
  109. print("Set information")
  110. write("Restart now? Y/n ")
  111. local rsn = read()
  112. if string.lower(rsn) ~= "n" then
  113. os.reboot()
  114. end
  115. end
  116.  
  117. if not args[1] then
  118. local tag = getLatestTag()
  119. if not tag then return end
  120. local inst = getInstallationInformation(tag)
  121. if not inst then return end
  122. install(inst, tag)
  123. elseif args[1] == "dev" then
  124. print("Development version selected - expect bugs")
  125. local inst = getInstallationInformation("zwm")
  126. if not inst then
  127. return
  128. end
  129. install(inst, "development")
  130. else
  131. local inst = getInstallationInformation(args[1])
  132. if not inst then
  133. return
  134. end
  135. install(inst, args[1])
  136. end
  137.  
Advertisement
Add Comment
Please, Sign In to add comment