lpenap

testing file

Sep 14th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. -- ComputerCraft BigReactors Control installer. Bootstrapped by https://pastebin.com/3W2G3Vc9
  2.  
  3. local tree = select(1,...)
  4. if not tree then
  5.     tree = 'master'
  6. end
  7. local repo = 'lpenap/computercraft-bifreactor-control'
  8.  
  9. local REPO_BASE = ('https://raw.githubusercontent.com/%s/%s/'):format(repo, tree)
  10.  
  11. local FILES = {
  12.     'lolmer_bigreactor_monitor_prog.lua',
  13.     'lolmer_bigreactor_startup.lua'
  14. }
  15.  
  16. local function request(url_path)
  17.     local request = http.get(REPO_BASE..url_path)
  18.     local status = request.getResponseCode()
  19.     local response = request.readAll()
  20.     request.close()
  21.     return status, response
  22. end
  23.  
  24. local function makeFile(file_path, data)
  25.     local file = fs.open('bigreactor-control.rom/'..file_path,'w')
  26.     file.write(data)
  27.     file.close()
  28. end
  29.  
  30. local function rewriteDofiles()
  31.     for _, file in pairs(FILES) do
  32.         local filename = ('bigreactor-control.rom/%s'):format(file)
  33.         local r = fs.open(filename, 'r')
  34.         local data = r.readAll()
  35.         r.close()
  36.         local w = fs.open(filename, 'w')
  37.         data = data:gsub('dofile%("', 'dofile("bigreactor-control.rom/')
  38.         w.write(data)
  39.         w.close()
  40.     end
  41. end
  42.  
  43. local function moveFiles()
  44.     fs.delete('reactorcontrol')
  45.     fs.delete('startup')
  46.     fs.move('bigreactor-control.rom/lolmer_bigreactor_monitor_prog.lua', 'reactorcontrol')
  47.     fs.move('bigreactor-control.rom/lolmer_bigreactor_startup.lua', 'startup')
  48. end
  49.  
  50. -- install the FILES for control program and startup
  51.  
  52. local function doInstall()
  53.     print ("Fetching BigReactor control program...")
  54.     print ("Using repo: " .. REPO_BASE)
  55.     local isDownloadOk = true
  56.     for key, path in pairs(FILES) do
  57.         local try = 0
  58.         local status, response = request(path)
  59.         print ("  Fetching " .. path)
  60.         while status ~= 200 and try <= 3 do
  61.             status, response = request(path)
  62.             try = try + 1
  63.         end
  64.         if status then
  65.             print ("    OK")
  66.             makeFile(path, response)
  67.         else
  68.             isDownloadOk = false
  69.             print (('Unable to download %s'):format(path))
  70.             fs.delete('bigreactor-control.rom')
  71.             fs.delete('reactorcontrol')
  72.             fs.delete('startup')
  73.             break
  74.         end
  75.     end
  76.     rewriteDofiles()
  77.     if isDownloadOk then
  78.         moveFiles()
  79.         print("BigReactors Control Program installed!")
  80.         print("")
  81.         print("Going to reboot the computer in 5 seconds...")
  82.         os.sleep(5)
  83.         os.reboot()
  84.     else
  85.         print("There was a problem installing the files.")
  86.     end
  87. end
  88.  
  89.  
  90. doInstall()
Advertisement
Add Comment
Please, Sign In to add comment