Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Starting installer...")
- local rootPath
- local args = { ... }
- local function getFile(name)
- local file, err = http.get(rootPath .. name .. "?" .. os.epoch('utc'))
- local content
- if file then
- content = file.readAll()
- file.close()
- end
- return content, err
- end
- local function downloadFile(name)
- local content = getFile(name)
- if not content then
- printError("Failed to download file: " .. name)
- return false
- end
- local file = fs.open(name, "w")
- file.write(content)
- file.close()
- return true
- end
- local function getInstallationInformation(tag)
- rootPath = "https://raw.githubusercontent.com/znepb-cc/zwm/" .. (tag or "zwm")
- print("Fetching installation information...")
- local inst, err = getFile("/inst/installation.lua")
- if not inst then
- printError(("Could not fetch installation information for branch %s."):format(tag))
- return nil
- else
- instData = textutils.unserialize(inst)
- end
- if not instData then
- error("Error parsing installation information")
- end
- return instData
- end
- local function getLatestTag()
- rootPath = "https://raw.githubusercontent.com/znepb-cc/zwm/zwm"
- print("Fetching latest tag...")
- local release, err = getFile("/inst/tag.txt")
- if err then
- printError("Could not fetch release tag. Please try again later.")
- return nil
- end
- return release
- end
- local function install(inst, release)
- print(("Got version %s"):format(release))
- print(("Release type: %s"):format(inst.releaseType))
- write(("Required space: %d"):format(inst.spaceRequirement))
- if fs.getFreeSpace("/") < inst.spaceRequirement then
- printError("Unable to install zwm. Reason: Not enough space")
- return
- else
- print(" (OK)")
- end
- print(("Setup will create %d directories and will install %d files."):format(#inst.directories, #inst.files))
- write("Confirm? Y/n ")
- local ready = read()
- if string.lower(ready) == "n" then
- print("Installation canceled.")
- return
- end
- print("Creating directories...")
- for i, v in pairs(inst.directories) do
- print(("Creating: %s"):format(v))
- fs.makeDir(v)
- end
- print("Downloading files...")
- for i, v in pairs(inst.files) do
- print(("Downloading: %s"):format(v))
- if not downloadFile(v) then
- printError("Failed to download file: " .. v)
- return
- end
- end
- print("Installation complete")
- local sha256 = require("/lib/sha256")
- print("Please set a username and password")
- write("Username: ")
- local username = read()
- write("Password: ")
- local password = sha256(read("*"))
- local data = {
- {
- name = username,
- passwordHash = password,
- homeDir = "/home/" .. username
- }
- }
- local file = fs.open("/etc/accounts.cfg", "w")
- file.write(textutils.serialize(data))
- file.close()
- print("Set information")
- write("Restart now? Y/n ")
- local rsn = read()
- if string.lower(rsn) ~= "n" then
- os.reboot()
- end
- end
- if not args[1] then
- local tag = getLatestTag()
- if not tag then return end
- local inst = getInstallationInformation(tag)
- if not inst then return end
- install(inst, tag)
- elseif args[1] == "dev" then
- print("Development version selected - expect bugs")
- local inst = getInstallationInformation("zwm")
- if not inst then
- return
- end
- install(inst, "development")
- else
- local inst = getInstallationInformation(args[1])
- if not inst then
- return
- end
- install(inst, args[1])
- end
Advertisement
Add Comment
Please, Sign In to add comment