April_The_Sergal

CIMS Install Script Revised

Sep 29th, 2025 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.28 KB | None | 0 0
  1. -- NETStore Storage System Dynamic Installer
  2. local GITHUB_REPO   = "Temperdox/KSSH_Storage_Revised"
  3. local GITHUB_BRANCH = "master"
  4. local GITHUB_PATH   = "src"
  5. local VERSION       = "1.0.0"
  6.  
  7. -- Ignore patterns
  8. local IGNORE_PATTERNS = {
  9.     "^%.git", "^%.gitignore$", "README%.md$", "%.backup$", "install%.lua$", "^docs",
  10. }
  11.  
  12. -- Color scheme
  13. local COLORS = {
  14.     background = colors.black, text = colors.white,
  15.     highlight = colors.gray,   highlightText = colors.black,
  16.     success = colors.lime,     error = colors.red,
  17.     warning = colors.orange,   info = colors.purple,
  18.     skip = colors.yellow
  19. }
  20.  
  21. local INSTALL_MODE = { CLEAN = "clean", UPDATE = "update" }
  22.  
  23. -- ========= UI helpers =========
  24. local function clamp(x,a,b) return math.max(a, math.min(b, x)) end
  25. local function clearScreen()
  26.     term.setBackgroundColor(COLORS.background)
  27.     term.setTextColor(COLORS.text)
  28.     term.clear(); term.setCursorPos(1,1)
  29. end
  30. local function drawHeader(title)
  31.     local w = select(1, term.getSize())
  32.     term.setCursorPos(1,1)
  33.     term.setBackgroundColor(COLORS.info)
  34.     term.setTextColor(COLORS.background)
  35.     term.clearLine()
  36.     local s = " "..title.." "
  37.     term.setCursorPos(math.floor((w-#s)/2)+1,1)
  38.     term.write(s)
  39.     term.setBackgroundColor(COLORS.background)
  40.     term.setTextColor(COLORS.text)
  41. end
  42.  
  43. local function drawList(x,y,w,h,title,items,sel,scroll)
  44.     local function line(s)
  45.         term.setCursorPos(x,y); term.write((" "):rep(w))
  46.         if s then term.setCursorPos(x+1,y); term.write(s:sub(1, math.max(0, w-2))) end
  47.         y=y+1
  48.     end
  49.     if title then
  50.         term.setCursorPos(x,y); term.write((" "):rep(w))
  51.         term.setCursorPos(x+1,y); term.write(title:sub(1, math.max(0,w-2)))
  52.         y=y+1
  53.     end
  54.     local visible = h - (title and 1 or 0)
  55.     sel   = clamp(sel, 1, math.max(1, #items))
  56.     scroll= clamp(scroll, 0, math.max(0, #items - visible))
  57.     for i=1,visible do
  58.         local idx = scroll + i
  59.         local s = items[idx] or ""
  60.         if idx == sel then
  61.             term.setBackgroundColor(COLORS.highlight)
  62.             term.setTextColor(COLORS.highlightText)
  63.             line(s)
  64.             term.setBackgroundColor(COLORS.background)
  65.             term.setTextColor(COLORS.text)
  66.         else line(s) end
  67.     end
  68.     return sel, scroll
  69. end
  70.  
  71. local function move(sel, scroll, total, maxRows, delta)
  72.     sel = clamp(sel + delta, 1, math.max(1,total))
  73.     local top = sel-1; local bottom = sel
  74.     if top < scroll then scroll = top
  75.     elseif bottom > scroll + maxRows then scroll = bottom - maxRows end
  76.     scroll = clamp(scroll, 0, math.max(0, total - maxRows))
  77.     return sel, scroll
  78. end
  79.  
  80. local function pickFromList(title, items, descriptions)
  81.     if #items == 0 then return nil end
  82.     if #items == 1 then return 1 end
  83.  
  84.     clearScreen(); drawHeader(title)
  85.     local w,h = term.getSize()
  86.     local listH = h - 6
  87.     local sel, scr = 1, 0
  88.  
  89.     while true do
  90.         -- clear body
  91.         for i=3,h do term.setCursorPos(1,i); term.clearLine() end
  92.         sel, scr = drawList(1,3,w,listH,nil,items,sel,scr)
  93.  
  94.         if descriptions and descriptions[sel] then
  95.             term.setCursorPos(1,h-2); term.setTextColor(COLORS.info); term.write(string.rep("-", w))
  96.             term.setCursorPos(2,h-1); term.write(descriptions[sel]:sub(1, w-2))
  97.             term.setCursorPos(2,h);   local line2 = descriptions[sel]:sub(w-1, (w-2)*2); if #line2 > 0 then term.write(line2) end
  98.             term.setTextColor(COLORS.text)
  99.         end
  100.  
  101.         local e = { os.pullEvent() }
  102.         local ev = e[1]
  103.         if ev == "key" then
  104.             local k = e[2]
  105.             if k == keys.enter then return sel
  106.             elseif k == keys.q then return nil
  107.             elseif k == keys.up then sel, scr = move(sel,scr,#items,listH,-1)
  108.             elseif k == keys.down then sel, scr = move(sel,scr,#items,listH, 1)
  109.             elseif k == keys.pageUp then sel, scr = move(sel,scr,#items,listH, -(listH-1))
  110.             elseif k == keys.pageDown then sel, scr = move(sel,scr,#items,listH,  (listH-1))
  111.             elseif k == keys.home then sel, scr = move(1,0,#items,listH,0)
  112.             elseif k == keys["end"] then sel, scr = move(#items, math.max(0, #items-listH), #items, listH, 0) end
  113.         elseif ev == "mouse_scroll" then
  114.             sel, scr = move(sel,scr,#items,listH, e[2])
  115.         elseif ev == "mouse_click" then
  116.             -- allow click-to-select
  117.             local _, _, mx, my = table.unpack(e)
  118.             local listTop = 3
  119.             if my >= listTop and my < listTop + listH then
  120.                 local clicked = scr + (my - listTop) + 1
  121.                 if clicked >= 1 and clicked <= #items then sel = clicked; return sel end
  122.             end
  123.         elseif ev == "term_resize" then
  124.             w,h = term.getSize(); listH = h - 6
  125.             scr = clamp(scr, 0, math.max(0, #items - listH)); sel = clamp(sel, 1, math.max(1, #items))
  126.         end
  127.     end
  128. end
  129.  
  130. local function showProgress(title, message, percent, color)
  131.     clearScreen(); drawHeader(title)
  132.     local w,h = term.getSize()
  133.     local barWidth = math.min(40, w-4)
  134.     local barX = math.floor((w - barWidth)/2)
  135.     local barY = math.floor(h/2)
  136.  
  137.     term.setCursorPos(math.floor((w - #message)/2), barY-2)
  138.     if color then term.setTextColor(color) end
  139.     term.write(message); term.setTextColor(COLORS.text)
  140.  
  141.     term.setCursorPos(barX, barY); term.write("[")
  142.     local filled = math.floor(barWidth * (percent/100))
  143.     term.setTextColor(COLORS.success); term.write(string.rep("=", filled))
  144.     term.setTextColor(COLORS.text);    term.write(string.rep(" ", barWidth - filled)); term.write("]")
  145.  
  146.     local p = math.floor(percent).."%"
  147.     term.setCursorPos(math.floor((w - #p)/2), barY+2); term.write(p)
  148. end
  149.  
  150. -- Graphic decoder functions
  151. local function decodegraphic(jsonData)
  152.     -- If jsonData is already a table (embedded), use it directly
  153.     if type(jsonData) == "table" then
  154.         for k, v in pairs(jsonData) do
  155.             return {
  156.                 width = v.width,
  157.                 image = v.image,
  158.             }
  159.         end
  160.     end
  161.     -- Otherwise try to load from file
  162.     local file = fs.open(jsonData..".json", "r")
  163.     if not file then return nil end
  164.     local contents = file.readAll()
  165.     file.close()
  166.     for k, v in pairs(textutils.unserialiseJSON(contents)) do
  167.         return {
  168.             width = v.width,
  169.             image = v.image,
  170.         }
  171.     end
  172. end
  173.  
  174. local function drawgraphic(screen, image, x, y, fg, bg)
  175.     local img = decodegraphic(image)
  176.     if not img then return end
  177.  
  178.     screen.setCursorPos(x, y)
  179.     for i = 1, #img.image do
  180.         local ch, fgc, bgc = 0x80, fg, bg
  181.         for i2 = 1, 5 do
  182.             local c = img.image[i]:sub(i2, i2)
  183.             if c == "1" then
  184.                 ch = ch + 2^(i2-1)
  185.             end
  186.         end
  187.         if img.image[i]:sub(6, 6) == "1" then
  188.             ch = bit32.band(bit32.bnot(ch), 0x1F) + 0x80
  189.             fgc, bgc = bgc, fgc
  190.         end
  191.         if math.fmod(i - 1, img.width) == 0 and i > 1 then
  192.             y = y + 1
  193.             screen.setCursorPos(x, y)
  194.         end
  195.         screen.blit(string.char(ch), colors.toBlit(fgc), colors.toBlit(bgc))
  196.     end
  197. end
  198.  
  199. -- Embedded sergal graphic data
  200. local sergalGraphicData = {
  201.     ["sergal"] = {
  202.         width = 12,
  203.         image = {
  204.             "110100",
  205.             "101111",
  206.             "001111",
  207.             "000011",
  208.             "000010",
  209.             "000000",
  210.             "000000",
  211.             "000000",
  212.             "000000",
  213.             "000000",
  214.             "000000",
  215.             "000000",
  216.             "000000",
  217.             "010000",
  218.             "111101",
  219.             "111111",
  220.             "111111",
  221.             "111111",
  222.             "001111",
  223.             "001111",
  224.             "000010",
  225.             "000000",
  226.             "000000",
  227.             "000000",
  228.             "000000",
  229.             "000011",
  230.             "001111",
  231.             "111111",
  232.             "111111",
  233.             "111111",
  234.             "111110",
  235.             "111101",
  236.             "111111",
  237.             "001010",
  238.             "000000",
  239.             "000000",
  240.             "011100",
  241.             "111101",
  242.             "111111",
  243.             "111111",
  244.             "111111",
  245.             "111111",
  246.             "111111",
  247.             "011111",
  248.             "111111",
  249.             "111111",
  250.             "101111",
  251.             "001011",
  252.             "000001",
  253.             "111111",
  254.             "111111",
  255.             "111111",
  256.             "111111",
  257.             "111111",
  258.             "111111",
  259.             "110011",
  260.             "111100",
  261.             "111100",
  262.             "111110",
  263.             "111000",
  264.             "011100",
  265.             "111001",
  266.             "111111",
  267.             "111111",
  268.             "111111",
  269.             "111111",
  270.             "111111",
  271.             "111100",
  272.             "111100",
  273.             "111000",
  274.             "000000",
  275.             "000000",
  276.             "000000",
  277.             "010101",
  278.             "111000",
  279.             "111111",
  280.             "111111",
  281.             "111111",
  282.             "111111",
  283.             "001011",
  284.             "000000",
  285.             "000000",
  286.             "000000",
  287.             "000000",
  288.             "000000",
  289.             "000000",
  290.             "000000",
  291.             "010000",
  292.             "110100",
  293.             "111101",
  294.             "111111",
  295.             "111010",
  296.             "101000",
  297.             "000000",
  298.             "000000",
  299.             "000000",
  300.             "000000",
  301.             "000000",
  302.             "000000",
  303.             "000000",
  304.             "000000",
  305.             "000000",
  306.             "010000",
  307.             "101000",
  308.             "000000",
  309.             "000000",
  310.             "000000",
  311.             "000000"
  312.         }
  313.     }
  314. }
  315.  
  316. -- Simple animated splash screen
  317. local function showAnimatedSplash()
  318.     clearScreen()
  319.     local w, h = term.getSize()
  320.  
  321.     local rainbowColors = {
  322.         colors.red, colors.orange, colors.yellow, colors.lime,
  323.         colors.green, colors.cyan, colors.lightBlue, colors.blue,
  324.         colors.purple, colors.magenta, colors.pink
  325.     }
  326.  
  327.     -- Calculate centering for the sergal (12 chars wide, ~9 rows tall based on the image data)
  328.     local sergalWidth = 12
  329.     local sergalHeight = math.ceil(#sergalGraphicData.sergal.image / sergalWidth)
  330.     local startX = math.floor((w - sergalWidth) / 2) + 1
  331.     local startY = math.floor((h - sergalHeight) / 2) - 2
  332.  
  333.     -- Ensure it fits on screen
  334.     if startY < 1 then startY = 1 end
  335.  
  336.     -- Animation loop
  337.     for cycle = 1, #rainbowColors do
  338.         local currentColor = rainbowColors[cycle]
  339.  
  340.         -- Clear screen
  341.         term.setBackgroundColor(colors.black)
  342.         term.clear()
  343.  
  344.         -- Draw sergal with current rainbow color
  345.         drawgraphic(term, sergalGraphicData, startX, startY, currentColor, colors.black)
  346.  
  347.         -- Draw text (stays white) - positioned below the sergal
  348.         local textY = math.min(h - 2, startY + sergalHeight + 2)
  349.  
  350.         local title = "CIMS Storage System"
  351.         term.setCursorPos(math.floor((w - #title) / 2) + 1, textY)
  352.         term.setTextColor(colors.white)
  353.         term.write(title)
  354.  
  355.         local version = "Installer v" .. VERSION
  356.         term.setCursorPos(math.floor((w - #version) / 2) + 1, textY + 1)
  357.         term.write(version)
  358.  
  359.         sleep(0.08)
  360.     end
  361.  
  362.     -- Brief hold
  363.     sleep(1.5)
  364.     clearScreen()
  365. end
  366.  
  367. -- ========= GitHub helpers =========
  368. local function getGitHubAPIUrl(path)
  369.     return string.format("https://api.github.com/repos/%s/contents/%s/%s?ref=%s", GITHUB_REPO, GITHUB_PATH, path or "", GITHUB_BRANCH)
  370. end
  371. local function getRawFileUrl(path)
  372.     return string.format("https://raw.githubusercontent.com/%s/%s/%s/%s", GITHUB_REPO, GITHUB_BRANCH, GITHUB_PATH, path)
  373. end
  374. local function shouldIgnore(path)
  375.     for _,pat in ipairs(IGNORE_PATTERNS) do if string.match(path, pat) then return true end end
  376.     return false
  377. end
  378.  
  379. local function fetchDirectoryContents(path, fileList, errorList)
  380.     fileList = fileList or {}; errorList = errorList or {}
  381.     local apiUrl = getGitHubAPIUrl(path)
  382.     local headers = { ["User-Agent"]="CC-Installer/"..VERSION, ["Accept"]="application/vnd.github.v3+json" }
  383.     local response = http.get(apiUrl, headers)
  384.     if not response then table.insert(errorList, "Failed to fetch directory: "..(path or "root")); return fileList, errorList end
  385.     local content = response.readAll(); response.close()
  386.  
  387.     local ok, data = pcall(textutils.unserializeJSON, content)
  388.     if not ok or not data then table.insert(errorList, "Failed to parse GitHub API response for: "..(path or "root")); return fileList, errorList end
  389.  
  390.     if data.type == "file" then data = { data }
  391.     elseif not data[1] then table.insert(errorList, "No contents found in: "..(path or "root")); return fileList, errorList end
  392.  
  393.     for _,item in ipairs(data) do
  394.         local fullPath = path and (path.."/"..item.name) or item.name
  395.         if not shouldIgnore(fullPath) and not shouldIgnore(item.name) then
  396.             if item.type == "file" then
  397.                 table.insert(fileList, { path=fullPath, size=item.size, sha=item.sha, download_url=item.download_url or getRawFileUrl(fullPath) })
  398.             elseif item.type == "dir" then
  399.                 fetchDirectoryContents(fullPath, fileList, errorList)
  400.             end
  401.         end
  402.     end
  403.     return fileList, errorList
  404. end
  405.  
  406. local function getSystemFiles(systemType)
  407.     local basePath
  408.     if systemType == "storage" then
  409.         basePath = "storage"
  410.     elseif systemType == "turtle" then
  411.         basePath = "turtle"
  412.     elseif systemType == "recipe" then
  413.         basePath = "recipe"
  414.     else
  415.         basePath = "storage"  -- default
  416.     end
  417.     return fetchDirectoryContents(basePath)
  418. end
  419.  
  420. -- ========= File management =========
  421. local function downloadFile(fileInfo, destination, mode)
  422.     local url = fileInfo.download_url or getRawFileUrl(fileInfo.path)
  423.     local response = http.get(url)
  424.     if not response then return false, "Failed to download: "..fileInfo.path end
  425.     local content = response.readAll(); response.close()
  426.  
  427.     if mode == INSTALL_MODE.UPDATE and fs.exists(destination) then
  428.         local fh = fs.open(destination, "r")
  429.         if fh then
  430.             local existing = fh.readAll(); fh.close()
  431.             if existing == content then return true, nil, true end
  432.         end
  433.     end
  434.  
  435.     local dir = fs.getDir(destination)
  436.     if dir ~= "" and not fs.exists(dir) then fs.makeDir(dir) end
  437.  
  438.     if mode == INSTALL_MODE.UPDATE and fs.exists(destination) then
  439.         local backup = destination..".backup"
  440.         if fs.exists(backup) then fs.delete(backup) end
  441.         fs.copy(destination, backup)
  442.     end
  443.  
  444.     local out = fs.open(destination, "w")
  445.     if not out then return false, "Failed to write: "..destination end
  446.     out.write(content); out.close()
  447.     return true, nil, false
  448. end
  449.  
  450. -- ========= Install routines =========
  451. local function cleanInstall()
  452.     clearScreen(); drawHeader("Clean Install - Complete System Wipe")
  453.     local w = select(1, term.getSize())
  454.     term.setCursorPos(2,3); term.setTextColor(COLORS.warning); term.write("DELETING ALL FILES..."); term.setTextColor(COLORS.text)
  455.  
  456.     local currentProgram = shell.getRunningProgram()
  457.     local function deleteRecursive(path)
  458.         if fs.isDir(path) then
  459.             for _,file in ipairs(fs.list(path)) do deleteRecursive(fs.combine(path,file)) end
  460.         end
  461.         if not fs.isReadOnly(path) and path ~= currentProgram then fs.delete(path) end
  462.     end
  463.  
  464.     local all = fs.list("/"); local total = #all; local deleted = 0
  465.     for i,file in ipairs(all) do
  466.         local p = fs.combine("/", file)
  467.         if not fs.isReadOnly(p) and p ~= currentProgram and file ~= currentProgram then
  468.             term.setCursorPos(2,4); term.clearLine()
  469.             term.write("Deleting: "..file.." ("..math.floor(i*100/total).."%)")
  470.             local ok = pcall(deleteRecursive, p); if ok then deleted = deleted + 1 end
  471.             sleep(0.03)
  472.         end
  473.     end
  474.     term.setCursorPos(2,6); term.setTextColor(COLORS.success); term.write("Deleted "..deleted.." files/directories"); term.setTextColor(COLORS.text)
  475.     term.setCursorPos(2,8); term.write("System cleaned. Installing fresh...")
  476.     sleep(0.6)
  477. end
  478.  
  479. local function installFiles(fileList, systemType, mode)
  480.     local total, completed, skipped, updated = #fileList, 0, 0, 0
  481.     local errors = {}
  482.  
  483.     if mode == INSTALL_MODE.CLEAN then cleanInstall() end
  484.  
  485.     for _,fi in ipairs(fileList) do
  486.         completed = completed + 1
  487.         local percent = (completed/total) * 100
  488.         local displayName = fi.path:match("([^/]+)$") or fi.path
  489.  
  490.         local localPath
  491.         if systemType == "storage" then
  492.             localPath = fi.path:gsub("^storage/", "")
  493.         elseif systemType == "turtle" then
  494.             localPath = fi.path:gsub("^turtle/", "")
  495.         elseif systemType == "recipe" then
  496.             localPath = fi.path:gsub("^recipe/", "")
  497.         else
  498.             localPath = fi.path:gsub("^storage/", "")
  499.         end
  500.  
  501.         local ok, err, wasSkipped = downloadFile(fi, localPath, mode)
  502.         if not ok then
  503.             table.insert(errors, err)
  504.             showProgress("Installing "..systemType, "ERROR: "..displayName, percent, COLORS.error)
  505.         elseif wasSkipped then
  506.             skipped = skipped + 1
  507.             showProgress("Installing "..systemType, "Skipped (unchanged): "..displayName, percent, COLORS.skip)
  508.         else
  509.             updated = updated + 1
  510.             showProgress("Installing "..systemType, "Updated: "..displayName, percent, COLORS.success)
  511.         end
  512.         sleep(0.03)
  513.     end
  514.  
  515.     return #errors == 0, errors, { total=total, updated=updated, skipped=skipped, errors=#errors }
  516. end
  517.  
  518. -- ========= Detection =========
  519. local function detectSystemType() return turtle and "turtle" or "computer" end
  520.  
  521. local function detectExistingInstallation()
  522.     -- Check if there are any files/directories other than rom and the installer
  523.     local currentProgram = shell.getRunningProgram()
  524.     local hasFiles = false
  525.  
  526.     for _, file in ipairs(fs.list("/")) do
  527.         local fullPath = fs.combine("/", file)
  528.         -- Skip rom directory and the installer itself
  529.         if file ~= "rom" and fullPath ~= currentProgram and file ~= currentProgram then
  530.             hasFiles = true
  531.             break
  532.         end
  533.     end
  534.  
  535.     -- If there are any files (other than rom and installer), consider it an existing installation
  536.     if hasFiles then
  537.         return "storage"  -- Default to storage type for existing installations
  538.     else
  539.         return nil  -- Clean system
  540.     end
  541. end
  542.  
  543. -- ========= Prompts (list-based / auto-continue) =========
  544. local function promptInstallMode()
  545.     local options = { "Update (preserve settings & logs)", "Clean Install (remove everything)", "Cancel" }
  546.     local descriptions = {
  547.         "Updates code files only, skips unchanged files, preserves logs, configs, and data",
  548.         "Removes ALL files except ROM and this installer - complete fresh start",
  549.         "Exit installer without making changes"
  550.     }
  551.     local sel = pickFromList("Existing Installation Detected", options, descriptions)
  552.     if not sel or sel == 3 then return nil
  553.     elseif sel == 1 then return INSTALL_MODE.UPDATE
  554.     else return INSTALL_MODE.CLEAN end
  555. end
  556.  
  557. local function confirmInstall(systemType, selection, mode)
  558.     local options = { "Proceed with Install", "Cancel" }
  559.     local desc = {
  560.         (mode == INSTALL_MODE.UPDATE)
  561.                 and "Update: preserves logs/config, skips unchanged, backs up modified files"
  562.                 or  "CLEAN INSTALL: deletes EVERYTHING except ROM & installer, then fresh install",
  563.         "Return to previous menu"
  564.     }
  565.  
  566.     -- render summary once behind the picker
  567.     clearScreen(); drawHeader("Confirm Installation")
  568.     local y = 4
  569.     term.setCursorPos(2,y); term.write("Installation Summary:"); y=y+2
  570.     term.setCursorPos(2,y); term.setTextColor(COLORS.info); term.write("System Type: "..selection); term.setTextColor(COLORS.text); y=y+1
  571.     term.setCursorPos(2,y); term.write("Device Type: "..(systemType=="turtle" and "Turtle" or "Computer")); y=y+1
  572.     term.setCursorPos(2,y); term.write("Install Mode: "..(mode==INSTALL_MODE.UPDATE and "Update" or "Clean Install")); y=y+2
  573.  
  574.     -- list picker at the bottom
  575.     local picked = pickFromList("Confirm Installation", options, desc)
  576.     return picked == 1
  577. end
  578.  
  579. local function preflightCheck(systemType)
  580.     clearScreen(); drawHeader("Checking GitHub Repository...")
  581.     term.setCursorPos(2,4); term.write("Fetching file list from GitHub...")
  582.  
  583.     local fileList, errors = getSystemFiles(systemType)
  584.     if #errors > 0 then
  585.         term.setCursorPos(2,6); term.setTextColor(COLORS.error); term.write("Failed to fetch file list:"); term.setTextColor(COLORS.text)
  586.         local y = 8
  587.         for _,err in ipairs(errors) do term.setCursorPos(2,y); term.write("- "..err); y=y+1 end
  588.         term.setCursorPos(2,y+1); term.write("Press any key to exit"); os.pullEvent("key")
  589.         return nil
  590.     end
  591.  
  592.     term.setCursorPos(2,6); term.setTextColor(COLORS.success); term.write("Found "..#fileList.." files to process"); term.setTextColor(COLORS.text)
  593.     term.setCursorPos(2,8); term.write("File structure:")
  594.  
  595.     local dirs = {}; for _,fi in ipairs(fileList) do local d=fs.getDir(fi.path); if d~="" and not dirs[d] then dirs[d]=true end end
  596.     local y=9; local w,h = term.getSize()
  597.     for d,_ in pairs(dirs) do
  598.         if y < h-3 then term.setCursorPos(4,y); term.write("- "..d.."/"); y=y+1 end
  599.     end
  600.     if y >= h-3 then term.setCursorPos(4,y); term.write("... and more") end
  601.  
  602.     -- Auto-continue after a short pause (no extra keypress)
  603.     sleep(0.7)
  604.     return fileList
  605. end
  606.  
  607. local function showComplete(selection, hasStartup, stats, mode)
  608.     clearScreen(); drawHeader("Installation Complete!")
  609.     local y=4
  610.     term.setCursorPos(2,y); term.setTextColor(COLORS.success); term.write("Successfully installed: "..selection); term.setTextColor(COLORS.text); y=y+2
  611.     if mode == INSTALL_MODE.UPDATE and stats then
  612.         term.setCursorPos(2,y); term.write("Update Statistics:"); y=y+1
  613.         term.setCursorPos(2,y); term.write(string.format("- Files checked: %d", stats.total)); y=y+1
  614.         term.setCursorPos(2,y); term.setTextColor(COLORS.success); term.write(string.format("- Files updated: %d", stats.updated)); term.setTextColor(COLORS.text); y=y+1
  615.         term.setCursorPos(2,y); term.setTextColor(COLORS.skip); term.write(string.format("- Files skipped (unchanged): %d", stats.skipped)); term.setTextColor(COLORS.text); y=y+2
  616.     end
  617.  
  618.     term.setCursorPos(2,y); term.write("To start the system:"); y=y+1
  619.     if selection == "Recipe Server" then
  620.         term.setCursorPos(2,y); term.setTextColor(COLORS.info); term.write("  Run: main"); term.setTextColor(COLORS.text); y=y+1
  621.         term.setCursorPos(2,y); term.write("  or reboot the computer"); y=y+2
  622.     else
  623.         term.setCursorPos(2,y); term.setTextColor(COLORS.info); term.write("  Run: main"); term.setTextColor(COLORS.text); y=y+1
  624.         term.setCursorPos(2,y); term.write("  or reboot the "..(selection=="Storage Computer" and "computer" or "turtle")); y=y+2
  625.     end
  626.  
  627.     term.setCursorPos(2,y); term.write("Configuration:"); y=y+1
  628.     if selection == "Storage Computer" then
  629.         term.setCursorPos(2,y); term.write("- Connect chests via wired modem"); y=y+1
  630.         term.setCursorPos(2,y); term.write("- Regular chest = input"); y=y+1
  631.         term.setCursorPos(2,y); term.write("- Trapped chest = output"); y=y+1
  632.         term.setCursorPos(2,y); term.write("- All others = storage")
  633.     elseif selection == "Recipe Server" then
  634.         term.setCursorPos(2,y); term.write("- Connect wired modem for network"); y=y+1
  635.         term.setCursorPos(2,y); term.write("- Connect disk drive with disk"); y=y+1
  636.         term.setCursorPos(2,y); term.write("- Recipes saved to disk"); y=y+1
  637.         term.setCursorPos(2,y); term.write("- Storage system will auto-discover")
  638.     else
  639.         term.setCursorPos(2,y); term.write("- Connect wireless modem"); y=y+1
  640.         term.setCursorPos(2,y); term.write("- Place chest in front for buffer"); y=y+1
  641.         term.setCursorPos(2,y); term.write("- Use recipe learning system")
  642.     end
  643.  
  644.     -- brief pause so user can read
  645.     sleep(0.8)
  646. end
  647.  
  648. -- ========= Main =========
  649. local function main()
  650.     showAnimatedSplash()
  651.  
  652.     local systemType = detectSystemType()
  653.  
  654.     -- Existing install?
  655.     local existing = detectExistingInstallation()
  656.     local installMode = INSTALL_MODE.CLEAN
  657.     if existing then
  658.         installMode = promptInstallMode()
  659.         if not installMode then clearScreen(); print("Installation cancelled."); return end
  660.     end
  661.  
  662.     -- System choice
  663.     local options = { "Storage Computer", "Recipe Server", "Crafting Turtle", "Cancel" }
  664.     local descriptions = {
  665.         "Main storage management system with inventory, sorting, and order processing",
  666.         "CIMS Recipe Server - Centralized recipe storage and turtle coordination",
  667.         "Turtle-based crafting system with recipe learning and remote control",
  668.         "Exit installer without making changes"
  669.     }
  670.     local selection = pickFromList("NETStore Installer - Select System Type", options, descriptions)
  671.     if not selection or selection == 4 then clearScreen(); print("Installation cancelled."); return end
  672.  
  673.     local selectedType = options[selection]
  674.     local selectedSystem
  675.     if selectedType == "Storage Computer" then
  676.         selectedSystem = "storage"
  677.     elseif selectedType == "Recipe Server" then
  678.         selectedSystem = "recipe"
  679.     elseif selectedType == "Crafting Turtle" then
  680.         selectedSystem = "turtle"
  681.     else
  682.         selectedSystem = "storage"
  683.     end
  684.  
  685.     -- Confirm (list UI)
  686.     if not confirmInstall(systemType, selectedType, installMode) then
  687.         clearScreen(); print("Installation cancelled."); return
  688.     end
  689.  
  690.     -- Fetch list + auto-continue
  691.     local fileList = preflightCheck(selectedSystem)
  692.     if not fileList then clearScreen(); print("Installation cancelled."); return end
  693.  
  694.     -- Install
  695.     local ok, errs, stats = installFiles(fileList, selectedSystem, installMode)
  696.     if not ok then
  697.         clearScreen(); term.setTextColor(COLORS.error); print("Installation failed with errors:"); term.setTextColor(COLORS.text)
  698.         for _,e in ipairs(errs) do print("  - "..e) end
  699.         print("\nPress any key to exit"); os.pullEvent("key"); return
  700.     end
  701.  
  702.     local hasStartup = fs.exists("startup.lua")
  703.     showComplete(selectedType, hasStartup, stats, installMode)
  704.  
  705.     -- Set label if needed
  706.     if not os.getComputerLabel() then
  707.         if selectedType == "Storage Computer" then
  708.             os.setComputerLabel("KSSH_Storage_"..os.getComputerID())
  709.         elseif selectedType == "Recipe Server" then
  710.             os.setComputerLabel("CIMS_Recipe_"..os.getComputerID())
  711.         else
  712.             os.setComputerLabel("NETStore_Turtle_"..os.getComputerID())
  713.         end
  714.     end
  715.  
  716.     -- Final action (list-based, no Y/N keys)
  717.     clearScreen(); drawHeader("Installation Complete!")
  718.  
  719.     local finalOptions
  720.     if hasStartup then
  721.         finalOptions = { "Start now (run startup)", "Exit to shell", "Reboot computer" }
  722.     else
  723.         finalOptions = { "Start now (run main)", "Exit to shell", "Reboot computer" }
  724.     end
  725.     local pick = pickFromList("What would you like to do?", finalOptions, {
  726.         hasStartup and "Runs your startup script immediately" or "Runs 'main' to start the system",
  727.         "Return to CraftOS shell",
  728.         "Reboots this computer"
  729.     })
  730.  
  731.     if pick == 1 then
  732.         clearScreen(); term.setCursorPos(1,1)
  733.         if selectedType == "Recipe Server" then
  734.             print("Starting CIMS Recipe Server...")
  735.         elseif selectedType == "Storage Computer" then
  736.             print("Starting NETStore Storage System...")
  737.         else
  738.             print("Starting Crafting Turtle...")
  739.         end
  740.         sleep(0.4)
  741.         if hasStartup then shell.run("startup") else shell.run("main") end
  742.     elseif pick == 2 then
  743.         clearScreen(); term.setCursorPos(1,1); print("Installation complete!"); if not hasStartup then print("Run 'main' to start the system.") end
  744.     elseif pick == 3 then
  745.         clearScreen(); term.setCursorPos(1,1); print("Rebooting..."); sleep(0.4); os.reboot()
  746.     else
  747.         clearScreen(); term.setCursorPos(1,1); print("Done.")
  748.     end
  749. end
  750.  
  751. main()
Advertisement
Add Comment
Please, Sign In to add comment