Advertisement
COOLGAMETUBEorginal

AutoAPI

Aug 20th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. local Version = 1.2
  2.  
  3. local args = {...}
  4. --Copyright Lyqyd
  5. --Copyright COOLGAMETUBE
  6. local exStr = [[shell.run("/usr/bin/AutoAPI loadAPIs")]]
  7. local function loadStartup()
  8.         local lines = {}
  9.         if fs.exists("/startup") then
  10.                 local handle = io.open("/startup", "r")
  11.                 if handle then
  12.                         for line in handle:lines() do
  13.                                 table.insert(lines, line)
  14.                         end
  15.                         handle:close()
  16.                 end
  17.         end
  18.         return lines
  19. end
  20.  
  21. local function writeStartup(contents)
  22.         local handle = io.open("/startup", "w")
  23.         if handle then
  24.                 for i, line in ipairs(contents) do
  25.                         handle:write(line.."\n")
  26.                 end
  27.                 handle:close()
  28.         end
  29. end
  30.  
  31. if #args >= 1 then
  32.         if args[1] == "loadAPIs" then
  33.             for _, apiF in ipairs(fs.list("/usr/apis/")) do
  34.                 if apiF ~= "package" then
  35.                     if apiF then
  36.                         os.unloadAPI(apiF)
  37.                     end
  38.                     os.loadAPI("/usr/apis/"..apiF)
  39.                 end
  40.             end
  41.         elseif args[1] == "install" then
  42.                 local contents = loadStartup()
  43.                 local changed = false
  44.                 if fs.exists("/startup") then                  
  45.                         local exists = false
  46.                        
  47.                         for i = 1, #contents do
  48.                                 if contents[i] == exStr then
  49.                                         exists = true
  50.                                         break
  51.                                 end
  52.                         end
  53.                        
  54.                         if not exists then
  55.                                 table.insert(contents, 1, exStr)
  56.                                 changed = true
  57.                         end
  58.                 else
  59.                         contents[1] = exStr
  60.                         changed = true
  61.                 end
  62.                 if changed then
  63.                         writeStartup(contents)
  64.                 end
  65.                 shell.run("usr/bin/AutoAPI loadAPIs")
  66.         elseif args[1] == "remove" then
  67.                 local contents = loadStartup()
  68.                 for i = #contents, 1, -1 do
  69.                          if contents[i] == exStr then
  70.                                 table.remove(contents, i)
  71.                         end
  72.                 end
  73.                 writeStartup(contents)
  74.         elseif args[1] == "reload" then
  75.             if #args > 1 then
  76.                 os.unloadAPI(args[2])
  77.                 os.loadAPI(args[2])
  78.             else
  79.                 shell.run("AutoAPI loadAPIs")
  80.             end
  81.         end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement