Advertisement
Navatusein

GTNH-OC-Installer

Oct 10th, 2024 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. local shell = require("shell")
  2. local term = require("term")
  3. local filesystem = require("filesystem")
  4. local internet = require("internet")
  5.  
  6. local programListUrl = "https://raw.githubusercontent.com/Navatusein/GTNH-OC-Installer/refs/heads/main/programs.lua"
  7.  
  8. ---@class ProgramDescription
  9. ---@field name string
  10. ---@field description string
  11. ---@field url string
  12. local program = {}
  13.  
  14. ---Download and install tar utility
  15. local function downloadTarUtility()
  16. if filesystem.exists("/bin/tar.lua") then
  17. return
  18. end
  19.  
  20. local tarManUrl = "https://raw.githubusercontent.com/mpmxyz/ocprograms/master/usr/man/tar.man"
  21. local tarBinUrl = "https://raw.githubusercontent.com/mpmxyz/ocprograms/master/home/bin/tar.lua"
  22.  
  23. shell.setWorkingDirectory("/usr/man")
  24. shell.execute("wget -fq "..tarManUrl)
  25. shell.setWorkingDirectory("/bin")
  26. shell.execute("wget -fq "..tarBinUrl)
  27. end
  28.  
  29. ---Download and install program
  30. ---@param program ProgramDescription
  31. local function downloadProgram(program)
  32. term.write("Installing "..program.name.."\n")
  33.  
  34. shell.execute("wget -fq "..program.url.." program.tar")
  35. shell.execute("tar -xf program.tar")
  36. shell.execute("rm program.tar")
  37.  
  38. term.write("Installation complete\n")
  39. end
  40.  
  41. ---Get program list from url
  42. ---@param programListUrl string
  43. ---@return ProgramDescription[]
  44. local function getProgramList(programListUrl)
  45. local request = internet.request(programListUrl)
  46. local result = ""
  47.  
  48. for chunk in request do
  49. result = result..chunk
  50. end
  51.  
  52. return load(result)()
  53. end
  54.  
  55. ---Choose program
  56. ---@param programList ProgramDescription[]
  57. ---@return ProgramDescription
  58. local function chooseProgram(programList)
  59. for key, value in pairs(programList) do
  60. term.write("["..key.."] "..value.name.."\n")
  61. term.write(" "..value.description.."\n\n")
  62. end
  63.  
  64. term.write("\nSelect program to install [1-"..tostring(#programList).."]\n")
  65.  
  66. local _, startRow = term.getCursor()
  67.  
  68. while true do
  69. term.write("===>")
  70.  
  71. local userInput = tonumber(io.read())
  72.  
  73. if userInput and userInput >= 1 and userInput <= #programList then
  74. return programList[userInput]
  75. end
  76.  
  77. term.setCursor(1, startRow)
  78. term.clearLine()
  79. end
  80. end
  81.  
  82. ---Make auto run
  83. local function makeAutoRun()
  84. term.write("\nCreate auto run [y/n]\n")
  85. term.write("===>")
  86.  
  87. local userInput = io.read()
  88.  
  89. term.clear()
  90.  
  91. if string.lower(userInput) == "y" then
  92. local file = assert(io.open("/home/.shrc", "w"))
  93. file:write("main")
  94. file:close()
  95.  
  96. term.write("Auto run created\n")
  97. else
  98. term.write("Auto run ignored\n")
  99. end
  100. end
  101.  
  102. ---Main
  103. local function main()
  104. term.clear()
  105. term.write("Welcome to Navatusein's programs installer\n\n")
  106.  
  107. downloadTarUtility()
  108. local programList = getProgramList(programListUrl)
  109. local programUrl = chooseProgram(programList)
  110.  
  111. shell.setWorkingDirectory("/home")
  112.  
  113. makeAutoRun()
  114. downloadProgram(programUrl)
  115. end
  116.  
  117. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement