-- init: bootstrap launcher. -- -- Installed as startup.lua by the kernel. On boot it asks which program -- to fetch, downloads it via the kernel, rewrites startup.lua so that -- subsequent boots launch THAT program directly, then runs it. local BASE_URL = "https://git.astrocore.space/root/nova-corp/raw/branch/main/" local function fetch(path, dest) if fs.exists(dest) then shell.execute("rm", dest) end shell.execute("wget", BASE_URL .. path, dest) sleep(1) end local function ensureKernel() if not fs.exists("kernel.lua") then fetch("kernel.lua", "kernel.lua") end return require("kernel") end local function writeProgramStartup(programName) local src = ([[-- Auto-generated startup for "%s" local BASE_URL = "%s" local function fetch(path, dest) if fs.exists(dest) then shell.execute("rm", dest) end shell.execute("wget", BASE_URL .. path, dest) sleep(1) end -- Always refresh kernel + program so code updates flow through. fetch("kernel.lua", "kernel.lua") local kernel = require("kernel") local program = kernel.addProgram("%s") if type(program) ~= "table" then print("Program '%s' did not return a module table.") return end if program.start then program.start() end if program.main then program.main() else print("Program '%s' has no main() function.") end ]]):format(programName, BASE_URL, programName, programName, programName) if fs.exists("startup.lua") then shell.execute("rm", "startup.lua") end local f = fs.open("startup.lua", "w") f.write(src) f.close() end local function prompt() term.clear() term.setCursorPos(1, 1) print("=== NovaCorp init ===") print("Enter program name to install (from /programs):") write("> ") local name = read() if not name or name == "" then print("No program entered. Staying on init.") return nil end return name end local function main() local name = prompt() if not name then return end local kernel = ensureKernel() print("Fetching program '" .. name .. "'...") local ok, programOrErr = pcall(kernel.addProgram, name) if not ok then print("Failed to download program: " .. tostring(programOrErr)) print("Leaving init in place. Reboot to try again.") return end print("Replacing startup.lua...") writeProgramStartup(name) print("Done. Launching '" .. name .. "' now.") sleep(1) local program = programOrErr if type(program) == "table" then if program.start then pcall(program.start) end if program.main then pcall(program.main) end end end main()