Advertisement
Lyqyd

easy-shell

Mar 5th, 2015
4,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local exStr = [[shell.run("/usr/bin/easy-shell execute")]]
  4.  
  5. local function loadStartup()
  6.     local lines = {}
  7.     if fs.exists("/startup") then
  8.         local handle = io.open("/startup", "r")
  9.         if handle then
  10.             for line in handle:lines() do
  11.                 table.insert(lines, line)
  12.             end
  13.             handle:close()
  14.         end
  15.     end
  16.     return lines
  17. end
  18.  
  19. local function writeStartup(contents)
  20.     local handle = io.open("/startup", "w")
  21.     if handle then
  22.         for i, line in ipairs(contents) do
  23.             handle:write(line.."\n")
  24.         end
  25.         handle:close()
  26.     else
  27.         error("Could not write startup file!")
  28.     end
  29. end
  30.  
  31. if #args >= 1 then
  32.     if args[1] == "execute" then
  33.         shell.setPath(shell.path()..":/usr/bin")
  34.  
  35.         local loadAPI = os.loadAPI
  36.  
  37.         os.loadAPI = function(path)
  38.             if fs.exists(path) then
  39.                 return loadAPI(path)
  40.             elseif fs.exists(fs.combine("/usr/apis", path)) then
  41.                 return loadAPI(fs.combine("/usr/apis", path))
  42.             end
  43.         end
  44.     elseif args[1] == "install" then
  45.         if tonumber(string.sub(os.version(), 9)) and tonumber(string.sub(os.version(), 9)) > 1.7 and fs.isDir("/startup") then
  46.             --In CraftOS 1.8+, simply add a file to the startup folder.
  47.             local handle = io.open("/startup/easy-shell.lua")
  48.             if handle then
  49.                 handle:write(exStr)
  50.                 handle:close()
  51.             else
  52.                 error("Could not write file!")
  53.             end
  54.         else
  55.             --otherwise, attempt to cleanly add a line to the startup file.
  56.             local contents = loadStartup()
  57.             local changed = false
  58.             if fs.exists("/startup") then
  59.                 local exists = false
  60.  
  61.                 for i = 1, #contents do
  62.                     if contents[i] == exStr then
  63.                         exists = true
  64.                         break
  65.                     end
  66.                 end
  67.  
  68.                 if not exists then
  69.                     table.insert(contents, 1, exStr)
  70.                     changed = true
  71.                 end
  72.             else
  73.                 contents[1] = exStr
  74.                 changed = true
  75.             end
  76.             if changed then
  77.                 writeStartup(contents)
  78.             end
  79.         end
  80.         shell.run("usr/bin/easy-shell execute")
  81.     elseif args[1] == "remove" then
  82.         if tonumber(string.sub(os.version(), 9)) and tonumber(string.sub(os.version(), 9)) > 1.7 and fs.exists("/startup/easy-shell.lua") then
  83.             --in 1.8+, we just have to delete the file we added.
  84.             fs.delete("/startup/easy-shell.lua")
  85.         else
  86.             local contents = loadStartup()
  87.             for i = #contents, 1, -1 do
  88.                  if contents[i] == exStr then
  89.                     table.remove(contents, i)
  90.                 end
  91.             end
  92.             writeStartup(contents)
  93.         end
  94.     end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement