April_The_Sergal

KSSH Installation Script

Sep 29th, 2025 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.97 KB | None | 0 0
  1. -- KSSH Storage System Installer with UI
  2. local GITHUB_REPO = "Temperdox/KSSH_Storage"
  3. local GITHUB_BRANCH = "master"
  4. local GITHUB_PATH = "src"
  5. local VERSION = "1.3.1"
  6.  
  7. -- Ignore list - files that should not be downloaded
  8. local IGNORE_LIST = {
  9.     ["install.lua"] = true,
  10.     [".gitignore"] = true,
  11.     ["README.md"] = true,
  12.     [".git"] = true
  13. }
  14.  
  15. -- Color scheme
  16. local COLORS = {
  17.     background = colors.black,
  18.     text = colors.white,
  19.     highlight = colors.gray,
  20.     highlightText = colors.black,
  21.     success = colors.lime,
  22.     error = colors.red,
  23.     warning = colors.orange,
  24.     info = colors.purple,
  25.     skip = colors.yellow
  26. }
  27.  
  28. -- Installation mode
  29. local INSTALL_MODE = {
  30.     CLEAN = "clean",
  31.     UPDATE = "update"
  32. }
  33.  
  34. -- UI Helper Functions
  35. local function clamp(x, a, b)
  36.     return math.max(a, math.min(b, x))
  37. end
  38.  
  39. local function clearScreen()
  40.     term.setBackgroundColor(COLORS.background)
  41.     term.setTextColor(COLORS.text)
  42.     term.clear()
  43.     term.setCursorPos(1, 1)
  44. end
  45.  
  46. local function drawHeader(title)
  47.     local w, h = term.getSize()
  48.     term.setCursorPos(1, 1)
  49.     term.setBackgroundColor(COLORS.info)
  50.     term.setTextColor(COLORS.background)
  51.     term.clearLine()
  52.     local titleText = " " .. title .. " "
  53.     local x = math.floor((w - #titleText) / 2) + 1
  54.     term.setCursorPos(x, 1)
  55.     term.write(titleText)
  56.     term.setBackgroundColor(COLORS.background)
  57.     term.setTextColor(COLORS.text)
  58. end
  59.  
  60. local function drawList(x, y, w, h, title, items, sel, scroll)
  61.     local function line(s)
  62.         term.setCursorPos(x, y)
  63.         term.write((" "):rep(w))
  64.         if s then
  65.             term.setCursorPos(x + 1, y)
  66.             term.write(s:sub(1, math.max(0, w - 2)))
  67.         end
  68.         y = y + 1
  69.     end
  70.  
  71.     -- Draw title if present
  72.     if title then
  73.         term.setCursorPos(x, y)
  74.         term.write((" "):rep(w))
  75.         term.setCursorPos(x + 1, y)
  76.         term.write(title:sub(1, math.max(0, w - 2)))
  77.         y = y + 1
  78.     end
  79.  
  80.     local visible = h - (title and 1 or 0)
  81.     sel = clamp(sel, 1, math.max(1, #items))
  82.     scroll = clamp(scroll, 0, math.max(0, #items - visible))
  83.  
  84.     for i = 1, visible do
  85.         local idx = scroll + i
  86.         local s = items[idx] or ""
  87.         if idx == sel then
  88.             term.setBackgroundColor(COLORS.highlight)
  89.             term.setTextColor(COLORS.highlightText)
  90.             line(s)
  91.             term.setBackgroundColor(COLORS.background)
  92.             term.setTextColor(COLORS.text)
  93.         else
  94.             line(s)
  95.         end
  96.     end
  97.     return sel, scroll
  98. end
  99.  
  100. local function move(sel, scroll, total, maxRows, delta)
  101.     sel = clamp(sel + delta, 1, math.max(1, total))
  102.     local top = sel - 1
  103.     local bottom = sel
  104.     if top < scroll then
  105.         scroll = top
  106.     elseif bottom > scroll + maxRows then
  107.         scroll = bottom - maxRows
  108.     end
  109.     scroll = clamp(scroll, 0, math.max(0, total - maxRows))
  110.     return sel, scroll
  111. end
  112.  
  113. local function pickFromList(title, items, descriptions)
  114.     if #items == 0 then return nil end
  115.     if #items == 1 then return 1 end
  116.  
  117.     clearScreen()
  118.     drawHeader(title)
  119.  
  120.     local w, h = term.getSize()
  121.     local listH = h - 6  -- Leave room for header and description
  122.     local sel, scr = 1, 0
  123.  
  124.     while true do
  125.         -- Clear and redraw
  126.         term.setCursorPos(1, 3)
  127.         for i = 3, h do
  128.             term.setCursorPos(1, i)
  129.             term.clearLine()
  130.         end
  131.  
  132.         -- Draw the list
  133.         sel, scr = drawList(1, 3, w, listH, nil, items, sel, scr)
  134.  
  135.         -- Draw description if available
  136.         if descriptions and descriptions[sel] then
  137.             term.setCursorPos(1, h - 2)
  138.             term.setTextColor(COLORS.info)
  139.             term.write(string.rep("-", w))
  140.             term.setCursorPos(2, h - 1)
  141.             term.write(descriptions[sel]:sub(1, w - 2))
  142.             term.setCursorPos(2, h)
  143.             local line2 = descriptions[sel]:sub(w - 1, (w - 2) * 2)
  144.             if #line2 > 0 then
  145.                 term.write(line2)
  146.             end
  147.             term.setTextColor(COLORS.text)
  148.         end
  149.  
  150.         -- Handle input
  151.         local e = {os.pullEvent()}
  152.         local ev = e[1]
  153.         if ev == "key" then
  154.             local k = e[2]
  155.             if k == keys.enter then
  156.                 return sel
  157.             elseif k == keys.q then
  158.                 return nil
  159.             elseif k == keys.up then
  160.                 sel, scr = move(sel, scr, #items, listH, -1)
  161.             elseif k == keys.down then
  162.                 sel, scr = move(sel, scr, #items, listH, 1)
  163.             elseif k == keys.pageUp then
  164.                 sel, scr = move(sel, scr, #items, listH, -(listH - 1))
  165.             elseif k == keys.pageDown then
  166.                 sel, scr = move(sel, scr, #items, listH, (listH - 1))
  167.             elseif k == keys.home then
  168.                 sel, scr = move(1, 0, #items, listH, 0)
  169.             elseif k == keys["end"] then
  170.                 sel, scr = move(#items, math.max(0, #items - listH), #items, listH, 0)
  171.             end
  172.         elseif ev == "mouse_scroll" then
  173.             local d = e[2]
  174.             sel, scr = move(sel, scr, #items, listH, d)
  175.         elseif ev == "term_resize" then
  176.             w, h = term.getSize()
  177.             listH = h - 6
  178.             scr = clamp(scr, 0, math.max(0, #items - listH))
  179.             sel = clamp(sel, 1, math.max(1, #items))
  180.         end
  181.     end
  182. end
  183.  
  184. local function showProgress(title, message, percent, color)
  185.     clearScreen()
  186.     drawHeader(title)
  187.  
  188.     local w, h = term.getSize()
  189.     local barWidth = math.min(40, w - 4)
  190.     local barX = math.floor((w - barWidth) / 2)
  191.     local barY = math.floor(h / 2)
  192.  
  193.     -- Message
  194.     term.setCursorPos(math.floor((w - #message) / 2), barY - 2)
  195.     if color then
  196.         term.setTextColor(color)
  197.     end
  198.     term.write(message)
  199.     term.setTextColor(COLORS.text)
  200.  
  201.     -- Progress bar
  202.     term.setCursorPos(barX, barY)
  203.     term.write("[")
  204.     local filled = math.floor(barWidth * (percent / 100))
  205.     term.setTextColor(COLORS.success)
  206.     term.write(string.rep("=", filled))
  207.     term.setTextColor(COLORS.text)
  208.     term.write(string.rep(" ", barWidth - filled))
  209.     term.write("]")
  210.  
  211.     -- Percentage
  212.     local percentText = math.floor(percent) .. "%"
  213.     term.setCursorPos(math.floor((w - #percentText) / 2), barY + 2)
  214.     term.write(percentText)
  215. end
  216.  
  217. -- File Management Functions
  218. local function getFileSize(path)
  219.     if not fs.exists(path) then
  220.         return 0
  221.     end
  222.     return fs.getSize(path)
  223. end
  224.  
  225. local function downloadFile(path, destination, mode)
  226.     local url = string.format(
  227.             "https://raw.githubusercontent.com/%s/%s/%s/%s",
  228.             GITHUB_REPO, GITHUB_BRANCH, GITHUB_PATH, path
  229.     )
  230.  
  231.     local response = http.get(url)
  232.     if not response then
  233.         return false, "Failed to download: " .. path
  234.     end
  235.  
  236.     local content = response.readAll()
  237.     response.close()
  238.  
  239.     -- In update mode, check if file content is identical
  240.     if mode == INSTALL_MODE.UPDATE and fs.exists(destination) then
  241.         local existingSize = getFileSize(destination)
  242.         local newSize = #content
  243.  
  244.         -- If sizes match, compare content
  245.         if existingSize == newSize then
  246.             local existingFile = fs.open(destination, "r")
  247.             if existingFile then
  248.                 local existingContent = existingFile.readAll()
  249.                 existingFile.close()
  250.  
  251.                 if existingContent == content then
  252.                     -- Files are identical, skip
  253.                     return true, nil, true  -- third return value indicates skip
  254.                 end
  255.             end
  256.         end
  257.     end
  258.  
  259.     -- Create directory if needed
  260.     local dir = fs.getDir(destination)
  261.     if dir ~= "" and not fs.exists(dir) then
  262.         fs.makeDir(dir)
  263.     end
  264.  
  265.     -- Backup existing file in update mode
  266.     if mode == INSTALL_MODE.UPDATE and fs.exists(destination) then
  267.         local backupPath = destination .. ".backup"
  268.         if fs.exists(backupPath) then
  269.             fs.delete(backupPath)
  270.         end
  271.         fs.copy(destination, backupPath)
  272.     end
  273.  
  274.     -- Write file
  275.     local file = fs.open(destination, "w")
  276.     if not file then
  277.         return false, "Failed to write: " .. destination
  278.     end
  279.  
  280.     file.write(content)
  281.     file.close()
  282.  
  283.     return true, nil, false
  284. end
  285.  
  286. -- Detection Functions
  287. local function detectExistingInstallation()
  288.     -- Check for storage system
  289.     local storageFiles = {
  290.         "main.lua",
  291.         "core/inventory.lua",
  292.         "tasks/task_pool.lua"
  293.     }
  294.  
  295.     local hasStorage = true
  296.     for _, file in ipairs(storageFiles) do
  297.         if not fs.exists(file) then
  298.             hasStorage = false
  299.             break
  300.         end
  301.     end
  302.  
  303.     -- Check for turtle system
  304.     local turtleFiles = {
  305.         "turtle/main.lua",
  306.         "turtle/service/crafter_service.lua",
  307.         "turtle/ui/ui_manager.lua"
  308.     }
  309.  
  310.     local hasTurtle = true
  311.     for _, file in ipairs(turtleFiles) do
  312.         if not fs.exists(file) then
  313.             hasTurtle = false
  314.             break
  315.         end
  316.     end
  317.  
  318.     if hasStorage then
  319.         return "storage"
  320.     elseif hasTurtle then
  321.         return "turtle"
  322.     else
  323.         return nil
  324.     end
  325. end
  326.  
  327. local function promptInstallMode()
  328.     local options = {
  329.         "Update (preserve settings & logs)",
  330.         "Clean Install (remove everything)",
  331.         "Cancel"
  332.     }
  333.  
  334.     local descriptions = {
  335.         "Updates code files only, skips unchanged files, preserves logs, configs, and data",
  336.         "Removes ALL files except ROM and this installer - complete fresh start",
  337.         "Exit installer without making changes"
  338.     }
  339.  
  340.     local selection = pickFromList("Existing Installation Detected", options, descriptions)
  341.  
  342.     if not selection or selection == 3 then
  343.         return nil
  344.     elseif selection == 1 then
  345.         return INSTALL_MODE.UPDATE
  346.     else
  347.         return INSTALL_MODE.CLEAN
  348.     end
  349. end
  350.  
  351. -- Installation Functions
  352. local function getStorageFiles()
  353.     return {
  354.         -- Core files
  355.         "storage/core/crafter.lua",
  356.         "storage/core/inventory.lua",
  357.         "storage/core/mover.lua",
  358.         "storage/core/peripherals.lua",
  359.         "storage/core/search.lua",
  360.         "storage/core/sound.lua",
  361.  
  362.         -- JSON files
  363.         "storage/json/recipes.json",
  364.  
  365.         -- Orders
  366.         "storage/orders/order_manager.lua",
  367.  
  368.         -- Tasks
  369.         "storage/tasks/deposit_task.lua",
  370.         "storage/tasks/recipe_listener.lua",
  371.         "storage/tasks/reformat_task.lua",
  372.         "storage/tasks/sort_task.lua",
  373.         "storage/tasks/task_pool.lua",
  374.  
  375.         -- UI
  376.         "storage/ui/overview.lua",
  377.  
  378.         -- Utilities
  379.         "storage/util/class.lua",
  380.         "storage/util/executor_service.lua",
  381.         "storage/util/log.lua",
  382.         "storage/util/log_print.lua",
  383.  
  384.         -- Main files
  385.         "storage/main.lua",
  386.         "storage/startup.lua"
  387.     }
  388. end
  389.  
  390. local function getTurtleFiles()
  391.     return {
  392.         -- Core files
  393.         "turtle/core/store_recipe.lua",
  394.  
  395.         -- Service files
  396.         "turtle/service/crafter_service.lua",
  397.         "turtle/service/executor_service.lua",
  398.         "turtle/service/recipe_service.lua",
  399.  
  400.         -- Tasks
  401.         "turtle/tasks/bridge.lua",
  402.  
  403.         -- UI
  404.         "turtle/ui/recipe_ui.lua",
  405.         "turtle/ui/ui_manager.lua",
  406.  
  407.         -- Utilities
  408.         "turtle/util/log_print.lua",
  409.  
  410.         -- Main files
  411.         "turtle/main.lua",
  412.         "turtle/startup.lua"
  413.     }
  414. end
  415.  
  416. local function cleanInstall(installType)
  417.     clearScreen()
  418.     drawHeader("Clean Install - Complete System Wipe")
  419.  
  420.     local w, h = term.getSize()
  421.     term.setCursorPos(2, 3)
  422.     term.setTextColor(COLORS.warning)
  423.     term.write("DELETING ALL FILES...")
  424.     term.setTextColor(COLORS.text)
  425.  
  426.     -- Get the name of this installer script
  427.     local currentProgram = shell.getRunningProgram()
  428.    
  429.     -- Helper function to recursively delete directories
  430.     local function deleteRecursive(path)
  431.         if fs.isDir(path) then
  432.             -- List all files in directory
  433.             for _, file in ipairs(fs.list(path)) do
  434.                 deleteRecursive(fs.combine(path, file))
  435.             end
  436.         end
  437.         -- Delete the file/empty directory if it's not ROM or the installer
  438.         if not fs.isReadOnly(path) and path ~= currentProgram then
  439.             fs.delete(path)
  440.         end
  441.     end
  442.    
  443.     -- Get all files and directories in root
  444.     local allFiles = fs.list("/")
  445.     local totalFiles = #allFiles
  446.     local deletedCount = 0
  447.    
  448.     for i, file in ipairs(allFiles) do
  449.         local path = fs.combine("/", file)
  450.        
  451.         -- Skip ROM directory and the installer itself
  452.         if not fs.isReadOnly(path) and path ~= currentProgram and file ~= currentProgram then
  453.             term.setCursorPos(2, 4)
  454.             term.clearLine()
  455.             term.write("Deleting: " .. file .. " (" .. math.floor(i * 100 / totalFiles) .. "%)")
  456.            
  457.             -- Delete the file or directory recursively
  458.             local ok, err = pcall(deleteRecursive, path)
  459.             if ok then
  460.                 deletedCount = deletedCount + 1
  461.             else
  462.                 term.setCursorPos(2, 5)
  463.                 term.setTextColor(COLORS.error)
  464.                 term.write("Failed to delete: " .. file)
  465.                 term.setTextColor(COLORS.text)
  466.                 sleep(0.5)
  467.             end
  468.            
  469.             -- Small visual delay
  470.             sleep(0.05)
  471.         end
  472.     end
  473.  
  474.     term.setCursorPos(2, 6)
  475.     term.setTextColor(COLORS.success)
  476.     term.write("Deleted " .. deletedCount .. " files/directories")
  477.     term.setTextColor(COLORS.text)
  478.    
  479.     term.setCursorPos(2, 8)
  480.     term.write("System cleaned. Installing fresh...")
  481.     sleep(1)
  482. end
  483.  
  484. local function installFiles(files, installType, mode)
  485.     local total = #files
  486.     local completed = 0
  487.     local skipped = 0
  488.     local updated = 0
  489.     local errors = {}
  490.  
  491.     -- Clean install removes everything first
  492.     if mode == INSTALL_MODE.CLEAN then
  493.         cleanInstall(installType)
  494.     end
  495.  
  496.     for _, file in ipairs(files) do
  497.         completed = completed + 1
  498.         local percent = (completed / total) * 100
  499.  
  500.         -- Extract filename for display
  501.         local displayName = file:match("([^/]+)$") or file
  502.  
  503.         -- Skip ignored files
  504.         if IGNORE_LIST[displayName] then
  505.             skipped = skipped + 1
  506.         else
  507.             -- Determine local path (remove "storage/" or "turtle/" prefix)
  508.             local localPath = file
  509.             if installType == "Storage Computer" then
  510.                 localPath = file:gsub("^storage/", "")
  511.             else
  512.                 localPath = file:gsub("^turtle/", "")
  513.             end
  514.  
  515.             -- Show current file being processed
  516.             local statusMsg = "Downloading: " .. displayName
  517.             if mode == INSTALL_MODE.UPDATE then
  518.                 statusMsg = "Checking: " .. displayName
  519.             end
  520.  
  521.             local success, err, wasSkipped = downloadFile(file, localPath, mode)
  522.  
  523.             if not success then
  524.                 table.insert(errors, err)
  525.                 showProgress("Installing " .. installType, "ERROR: " .. displayName, percent, COLORS.error)
  526.             elseif wasSkipped then
  527.                 skipped = skipped + 1
  528.                 showProgress("Installing " .. installType, "Skipped (unchanged): " .. displayName, percent, COLORS.skip)
  529.             else
  530.                 updated = updated + 1
  531.                 showProgress("Installing " .. installType, "Updated: " .. displayName, percent, COLORS.success)
  532.             end
  533.         end
  534.  
  535.         sleep(0.05) -- Small delay for visual feedback
  536.     end
  537.  
  538.     return #errors == 0, errors, {
  539.         total = total,
  540.         updated = updated,
  541.         skipped = skipped,
  542.         errors = #errors
  543.     }
  544. end
  545.  
  546. -- Main Installation Flow
  547. local function detectSystemType()
  548.     if turtle then
  549.         return "turtle"
  550.     else
  551.         return "computer"
  552.     end
  553. end
  554.  
  555. local function confirmInstall(systemType, selection, mode)
  556.     clearScreen()
  557.     drawHeader("Confirm Installation")
  558.  
  559.     local w, h = term.getSize()
  560.     local y = 4
  561.  
  562.     term.setCursorPos(2, y)
  563.     term.write("Installation Summary:")
  564.     y = y + 2
  565.  
  566.     term.setCursorPos(2, y)
  567.     term.setTextColor(COLORS.info)
  568.     term.write("System Type: " .. selection)
  569.     term.setTextColor(COLORS.text)
  570.     y = y + 1
  571.  
  572.     term.setCursorPos(2, y)
  573.     term.write("Device Type: " .. (systemType == "turtle" and "Turtle" or "Computer"))
  574.     y = y + 1
  575.  
  576.     term.setCursorPos(2, y)
  577.     term.write("Install Mode: " .. (mode == INSTALL_MODE.UPDATE and "Update" or "Clean Install"))
  578.     y = y + 2
  579.  
  580.     if mode == INSTALL_MODE.UPDATE then
  581.         term.setCursorPos(2, y)
  582.         term.setTextColor(COLORS.info)
  583.         term.write("Update will:")
  584.         term.setTextColor(COLORS.text)
  585.         y = y + 1
  586.         term.setCursorPos(2, y)
  587.         term.write("- Preserve logs and configurations")
  588.         y = y + 1
  589.         term.setCursorPos(2, y)
  590.         term.write("- Skip unchanged files")
  591.         y = y + 1
  592.         term.setCursorPos(2, y)
  593.         term.write("- Create backups of modified files")
  594.         y = y + 2
  595.     else
  596.         term.setCursorPos(2, y)
  597.         term.setTextColor(COLORS.warning)
  598.         term.write("CLEAN INSTALL WILL:")
  599.         term.setTextColor(COLORS.text)
  600.         y = y + 1
  601.         term.setCursorPos(2, y)
  602.         term.setTextColor(COLORS.error)
  603.         term.write("- DELETE EVERYTHING except ROM & installer")
  604.         term.setTextColor(COLORS.text)
  605.         y = y + 1
  606.         term.setCursorPos(2, y)
  607.         term.write("- Remove ALL logs, configs, and programs")
  608.         y = y + 1
  609.         term.setCursorPos(2, y)
  610.         term.write("- Perform complete fresh installation")
  611.         y = y + 2
  612.     end
  613.  
  614.     if selection == "Storage Computer" and systemType == "turtle" then
  615.         term.setCursorPos(2, y)
  616.         term.setTextColor(COLORS.warning)
  617.         term.write("WARNING: Installing storage system on a turtle!")
  618.         term.setTextColor(COLORS.text)
  619.         y = y + 1
  620.         term.setCursorPos(2, y)
  621.         term.write("This system is designed for computers.")
  622.         y = y + 2
  623.     elseif selection == "Crafting Turtle" and systemType == "computer" then
  624.         term.setCursorPos(2, y)
  625.         term.setTextColor(COLORS.warning)
  626.         term.write("WARNING: Installing turtle system on a computer!")
  627.         term.setTextColor(COLORS.text)
  628.         y = y + 1
  629.         term.setCursorPos(2, y)
  630.         term.write("This system is designed for turtles.")
  631.         y = y + 2
  632.     end
  633.  
  634.     term.setCursorPos(2, y)
  635.     term.write("Press ENTER to continue or Q to cancel")
  636.  
  637.     while true do
  638.         local event, key = os.pullEvent("key")
  639.         if key == keys.enter then
  640.             return true
  641.         elseif key == keys.q then
  642.             return false
  643.         end
  644.     end
  645. end
  646.  
  647. local function showSplash()
  648.     clearScreen()
  649.  
  650.     local w, h = term.getSize()
  651.     local logo = {
  652.         "  _  __ ____ ____ _   _ ",
  653.         " | |/ // ___) ___) |_| \\",
  654.         "|   ( \\__ \\__  \\| |=| |",
  655.         "|_|\\_\\(___/(___/|_| |_|",
  656.         "KSSH Installer v" .. VERSION
  657.     }
  658.  
  659.     local startY = math.floor((h - #logo) / 2)
  660.  
  661.     for i, line in ipairs(logo) do
  662.         local x = math.floor((w - #line) / 2) + 1
  663.         term.setCursorPos(x, startY + i)
  664.         if i <= 4 then
  665.             term.setTextColor(COLORS.info)
  666.         else
  667.             term.setTextColor(COLORS.text)
  668.         end
  669.         term.write(line)
  670.     end
  671.  
  672.     term.setTextColor(COLORS.text)
  673.     term.setCursorPos(math.floor((w - 23) / 2), h - 2)
  674.     term.write("Press any key to continue")
  675.  
  676.     os.pullEvent("key")
  677. end
  678.  
  679. local function showComplete(selection, hasStartup, stats, mode)
  680.     clearScreen()
  681.     drawHeader("Installation Complete!")
  682.  
  683.     local w, h = term.getSize()
  684.     local y = 4
  685.  
  686.     term.setCursorPos(2, y)
  687.     term.setTextColor(COLORS.success)
  688.     term.write("Successfully installed: " .. selection)
  689.     term.setTextColor(COLORS.text)
  690.     y = y + 2
  691.  
  692.     -- Show statistics for update mode
  693.     if mode == INSTALL_MODE.UPDATE and stats then
  694.         term.setCursorPos(2, y)
  695.         term.write("Update Statistics:")
  696.         y = y + 1
  697.         term.setCursorPos(2, y)
  698.         term.write(string.format("- Files checked: %d", stats.total))
  699.         y = y + 1
  700.         term.setCursorPos(2, y)
  701.         term.setTextColor(COLORS.success)
  702.         term.write(string.format("- Files updated: %d", stats.updated))
  703.         term.setTextColor(COLORS.text)
  704.         y = y + 1
  705.         term.setCursorPos(2, y)
  706.         term.setTextColor(COLORS.skip)
  707.         term.write(string.format("- Files skipped (unchanged): %d", stats.skipped))
  708.         term.setTextColor(COLORS.text)
  709.         y = y + 2
  710.     end
  711.  
  712.     term.setCursorPos(2, y)
  713.     term.write("Installation Summary:")
  714.     y = y + 2
  715.  
  716.     if hasStartup then
  717.         term.setCursorPos(2, y)
  718.         term.write("- Startup script installed")
  719.         y = y + 1
  720.         term.setCursorPos(2, y)
  721.         term.write("  The system will auto-start on reboot")
  722.         y = y + 2
  723.     end
  724.  
  725.     term.setCursorPos(2, y)
  726.     term.write("To start the system:")
  727.     y = y + 1
  728.  
  729.     if selection == "Storage Computer" then
  730.         term.setCursorPos(2, y)
  731.         term.setTextColor(COLORS.info)
  732.         term.write("  Run: main")
  733.         term.setTextColor(COLORS.text)
  734.         y = y + 1
  735.         term.setCursorPos(2, y)
  736.         term.write("  or reboot the computer")
  737.     else
  738.         term.setCursorPos(2, y)
  739.         term.setTextColor(COLORS.info)
  740.         term.write("  Run: main")
  741.         term.setTextColor(COLORS.text)
  742.         y = y + 1
  743.         term.setCursorPos(2, y)
  744.         term.write("  or reboot the turtle")
  745.     end
  746.  
  747.     y = y + 2
  748.     term.setCursorPos(2, y)
  749.     term.write("Configuration:")
  750.     y = y + 1
  751.  
  752.     if selection == "Storage Computer" then
  753.         term.setCursorPos(2, y)
  754.         term.write("- Connect chests via wired modem")
  755.         y = y + 1
  756.         term.setCursorPos(2, y)
  757.         term.write("- Regular chest = input")
  758.         y = y + 1
  759.         term.setCursorPos(2, y)
  760.         term.write("- Trapped chest = output")
  761.         y = y + 1
  762.         term.setCursorPos(2, y)
  763.         term.write("- All others = storage")
  764.     else
  765.         term.setCursorPos(2, y)
  766.         term.write("- Connect wireless modem")
  767.         y = y + 1
  768.         term.setCursorPos(2, y)
  769.         term.write("- Place chest in front for buffer")
  770.         y = y + 1
  771.         term.setCursorPos(2, y)
  772.         term.write("- Use recipe learning system")
  773.     end
  774.  
  775.     term.setCursorPos(2, h - 1)
  776.     term.write("Press any key to exit")
  777.     os.pullEvent("key")
  778. end
  779.  
  780. -- Main Program
  781. local function main()
  782.     -- Show splash screen
  783.     showSplash()
  784.  
  785.     -- Detect system type
  786.     local systemType = detectSystemType()
  787.  
  788.     -- Check for existing installation
  789.     local existingInstall = detectExistingInstallation()
  790.     local installMode = INSTALL_MODE.CLEAN
  791.  
  792.     if existingInstall then
  793.         -- Prompt for update vs clean install
  794.         installMode = promptInstallMode()
  795.         if not installMode then
  796.             clearScreen()
  797.             print("Installation cancelled.")
  798.             return
  799.         end
  800.     end
  801.  
  802.     -- Present options
  803.     local options = {
  804.         "Storage Computer",
  805.         "Crafting Turtle",
  806.         "Cancel"
  807.     }
  808.  
  809.     local descriptions = {
  810.         "Main storage management system with inventory, sorting, and order processing",
  811.         "Turtle-based crafting system with recipe learning and remote control",
  812.         "Exit installer without making changes"
  813.     }
  814.  
  815.     local selection = pickFromList("KSSH Installer - Select System Type", options, descriptions)
  816.  
  817.     if not selection or selection == 3 then
  818.         clearScreen()
  819.         print("Installation cancelled.")
  820.         return
  821.     end
  822.  
  823.     local selectedType = options[selection]
  824.  
  825.     -- Confirm installation
  826.     if not confirmInstall(systemType, selectedType, installMode) then
  827.         clearScreen()
  828.         print("Installation cancelled.")
  829.         return
  830.     end
  831.  
  832.     -- Perform installation
  833.     local files
  834.     if selectedType == "Storage Computer" then
  835.         files = getStorageFiles()
  836.     else
  837.         files = getTurtleFiles()
  838.     end
  839.  
  840.     local success, errors, stats = installFiles(files, selectedType, installMode)
  841.  
  842.     if not success then
  843.         clearScreen()
  844.         term.setTextColor(COLORS.error)
  845.         print("Installation failed with errors:")
  846.         term.setTextColor(COLORS.text)
  847.         for _, err in ipairs(errors) do
  848.             print("  - " .. err)
  849.         end
  850.         print("\nPress any key to exit")
  851.         os.pullEvent("key")
  852.         return
  853.     end
  854.  
  855.     -- Check for startup file
  856.     local hasStartup = false
  857.     if selectedType == "Storage Computer" then
  858.         hasStartup = fs.exists("startup.lua")
  859.     else
  860.         hasStartup = fs.exists("startup.lua")
  861.     end
  862.  
  863.     -- Show completion screen
  864.     showComplete(selectedType, hasStartup, stats, installMode)
  865.  
  866.     -- Set computer label if not set
  867.     if not os.getComputerLabel() then
  868.         if selectedType == "Storage Computer" then
  869.             os.setComputerLabel("KSSH_Storage_" .. os.getComputerID())
  870.         else
  871.             os.setComputerLabel("KSSH_Turtle_" .. os.getComputerID())
  872.         end
  873.     end
  874.  
  875.     -- Final prompt for auto-start
  876.     clearScreen()
  877.     drawHeader("Installation Complete!")
  878.    
  879.     local w, h = term.getSize()
  880.     term.setCursorPos(2, 4)
  881.     term.setTextColor(COLORS.success)
  882.     print("KSSH Storage System installed successfully!")
  883.     term.setTextColor(COLORS.text)
  884.    
  885.     if hasStartup then
  886.         term.setCursorPos(2, 6)
  887.         print("Would you like to start the system now?")
  888.         term.setCursorPos(2, 8)
  889.         term.setTextColor(COLORS.info)
  890.         print("[Y] Yes - Run startup script")
  891.         print("[N] No - Exit to shell")
  892.         print("[R] Reboot computer")
  893.         term.setTextColor(COLORS.text)
  894.        
  895.         while true do
  896.             local event, key = os.pullEvent("key")
  897.             if key == keys.y then
  898.                 clearScreen()
  899.                 term.setCursorPos(1, 1)
  900.                 print("Starting KSSH Storage System...")
  901.                 sleep(0.5)
  902.                 shell.run("startup")
  903.                 return
  904.             elseif key == keys.n then
  905.                 clearScreen()
  906.                 term.setCursorPos(1, 1)
  907.                 print("Installation complete!")
  908.                 print("Run 'startup' to start the system.")
  909.                 return
  910.             elseif key == keys.r then
  911.                 clearScreen()
  912.                 term.setCursorPos(1, 1)
  913.                 print("Rebooting...")
  914.                 sleep(0.5)
  915.                 os.reboot()
  916.             end
  917.         end
  918.     else
  919.         term.setCursorPos(2, 6)
  920.         print("Run 'main' to start the system")
  921.         term.setCursorPos(2, 8)
  922.         print("Press any key to exit to shell...")
  923.         os.pullEvent("key")
  924.         clearScreen()
  925.         term.setCursorPos(1, 1)
  926.     end
  927. end
  928.  
  929. -- Run the installer
  930. main()
Advertisement
Add Comment
Please, Sign In to add comment