Advertisement
Selim_042

Northbridge Terminal Install

Oct 9th, 2015
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.61 KB | None | 0 0
  1. local northbridgeBuildAPI = "http://builds.northbridge.dx.am/"
  2. local x, y = term.getSize()
  3.  
  4. local function putFilesFromDirArray(array, build, basePath)
  5.     for k,v in pairs(array) do
  6.         if (type(array[k]) == "string") then
  7.             if (basePath ~= nil) then
  8.                 v = basePath.."/"..v
  9.             end
  10.             fileData = http.get(northbridgeBuildAPI..build.."/"..v)
  11.             if (fileData ~= nil) then
  12.                 local file = fs.open(v, "w")
  13.                 file.write(fileData.readAll(), "w")
  14.                 file.close()
  15.             end
  16.         else
  17.             if (basePath ~= nil) then
  18.                 k = basePath.."/"..k
  19.             end
  20.             fs.makeDir(k)
  21.             putFilesFromDirArray(v, build, k)
  22.         end
  23.     end
  24. end
  25.  
  26. local function arrayToDir(dirArray, dir)
  27.     if (dir == nil) then dir = "" end
  28.     for k,v in pairs(dirArray) do
  29.         print(dir.."/"..k..": "..v.type)
  30.         if (v.type == "file") then
  31.             if (not fs.exists(dir)) then fs.makeDir(dir) end
  32.             local file = fs.open(dir.."/"..k,'w')
  33.             file.write(v.contents)
  34.             file.close()
  35.         elseif (v.type == "dir") then
  36.             fs.makeDir(dir.."/"..k)
  37.             arrayToDir(v.contents,dir.."/"..k)
  38.         end
  39.     end
  40. end
  41.  
  42. local function firstToUpper(str)
  43.     return (str:gsub("^%l", string.upper))
  44. end
  45.  
  46. local function split(self, inSplitPattern, outResults )
  47.     if not outResults then
  48.         outResults = { }
  49.     end
  50.     local theStart = 1
  51.     local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  52.     while theSplitStart do
  53.         table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
  54.         theStart = theSplitEnd + 1
  55.         theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  56.     end
  57.     table.insert( outResults, string.sub( self, theStart ) )
  58.     return outResults
  59. end
  60.  
  61. local function splitToLines(longString)
  62.     if (#longString > x) then
  63.         local chars = split(longString,"")
  64.         local lines = (#chars+3)/x
  65.         for i=1,#lines do
  66.             chars:insert(i*x,"\n   ")
  67.         end
  68.         local outString = ""
  69.         for i=1,#chars do
  70.             outString = outString..chars[i]
  71.         end
  72.         return outString
  73.     else
  74.         return longString
  75.     end
  76. end
  77.  
  78. local function pagedPrint(input)
  79.     local linedinput = split(input,"\n")
  80.     local x,y = term.getSize()
  81.     term.setCursorPos(1,2)
  82.     for i=1,y-2 do
  83.         print(linedInput[i])
  84.     end
  85.     print("Press a key to continue")
  86.     for i=y+1,#linedInput-y do
  87.         term.setCursorPos(1,1)
  88.         term.setTextColor(colors.white)
  89.         if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
  90.         for i=1, x do
  91.             write(" ")
  92.         end
  93.         term.setCursorPos(1, 1)
  94.         print("Northbridge Terminal OS Build Info:")
  95.         if (term.isColor()) then term.setBackgroundColor(colors.black) end
  96.         os.pullEvent("key")
  97.         term.setCursorPos(1,i)
  98.         print(linedInput[i])
  99.     end
  100. end
  101.  
  102. write("Getting build info... ")
  103. builds = http.get(northbridgeBuildAPI.."build.php?cmd=getBuilds&versions=true&changelogs=true")
  104. if (builds ~= nil) then
  105.     builds = textutils.unserialize(builds.readAll())
  106. else
  107.     error("can't access the Northbridge Network Build API")
  108. end
  109. local outer = true
  110. while (outer) do
  111.     term.clear()
  112.     term.setCursorPos(1, 1)
  113.     term.setTextColor(colors.white)
  114.     --[=[if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
  115.     for i=1, x do
  116.         write(" ")
  117.     end
  118.     term.setCursorPos(1, 1)
  119.     print("Northbridge Terminal OS Build Info:")
  120.     if (term.isColor()) then term.setBackgroundColor(colors.black) end
  121.     local info = ""
  122.     for k, v in pairs(builds.msg) do
  123.         if (type(k) ~= "number") then
  124.             info = info..firstToUpper(k)..":".."\n"
  125.             if (v.version ~= nil) then
  126.                 info = info.." Version: "..v.version.."\n"
  127.             end
  128.             if (v.changelog ~= nil) then
  129.                 info = info.." Changelog:".."\n"
  130.                 for i=1, #v.changelog do
  131.                     --print(" "..v.changelog[i])
  132.                     info = info.." "..splitToLines(v.changelog[i]).."\n"
  133.                 end
  134.             end
  135.         end
  136.     end
  137.     pagedPrint(info)
  138.     --[[term.setCursorPos(1, y)
  139.     write("Press a key to continue.")
  140.     os.pullEvent("key")]]
  141.     sleep(0)]=]
  142.     local inner = true
  143.     while (inner) do
  144.         term.clear()
  145.         term.setCursorPos(1, 1)
  146.         term.setTextColor(colors.white)
  147.         if (term.isColor()) then term.setBackgroundColor(colors.lightGray) end
  148.         for i=1, x do
  149.             write(" ")
  150.         end
  151.         term.setCursorPos(1, 1)
  152.         print("Northbridge Terminal OS Build Selection:")
  153.         term.setBackgroundColor(colors.black)
  154.         print("Which build would you like to install?")
  155.         local cursorPos = {term.getCursorPos(1,1)}
  156.         for i = 1, #builds.msg do
  157.             print(i.."> "..firstToUpper(builds.msg[i]))
  158.         end
  159.         --print(1+#builds.msg.."> ".."Show previous page")
  160.         print(2+#builds.msg.."> ".."Cancel")
  161.         reply = tonumber(read())
  162.         if (reply == nil) or (reply < 0) or (reply > #builds.msg+2) then
  163.             print("Invalid reply")
  164.             sleep(2)
  165.             term.setCursorPos(cursorPos[1], cursorPos[2])
  166.         elseif (reply == #builds.msg+1) then
  167.             inner = false
  168.         elseif (reply == #builds.msg+2) then
  169.             term.clear()
  170.             term.setCursorPos(1,1)
  171.             print("Nevermind then...")
  172.             inner = false
  173.             outer = false
  174.         else
  175.             print("Getting build files...")
  176.             buildData = http.get(northbridgeBuildAPI.."build.php?cmd=getBuildFiles&build="..builds.msg[reply])
  177.             if (buildData == nil) then
  178.                 error("can't access the Northbridge Network Build API")
  179.             end
  180.             buildData = textutils.unserialize(buildData.readAll())
  181.             if (fs.exists("/startup")) then
  182.                 fs.move("/startup","/autorun")
  183.             end
  184.             putFilesFromDirArray(buildData.msg, builds.msg[reply])
  185.            
  186.             if (fs.exists("files")) then
  187.                 local files = fs.open("files",'r')
  188.                 arrayToDir(textutils.unserialise(files.readAll()))
  189.                 files.close()
  190.                 fs.delete("files")
  191.             end
  192.            
  193.             local file = fs.open("/northbridge/build", "w")
  194.             file.write(builds.msg[reply])
  195.             file.close()
  196.            
  197.             fs.delete("json")
  198.             fs.delete("version")
  199.             print("Rebooting...")
  200.             sleep(2)
  201.             os.reboot()
  202.         end
  203.     end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement