This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy-shell
95 lines (87 loc) · 2.24 KB
/
easy-shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local args = {...}
local exStr = [[shell.run("/usr/bin/easy-shell execute")]]
local function loadStartup()
local lines = {}
if fs.exists("/startup") then
local handle = io.open("/startup", "r")
if handle then
for line in handle:lines() do
table.insert(lines, line)
end
handle:close()
end
end
return lines
end
local function writeStartup(contents)
local handle = io.open("/startup", "w")
if handle then
for i, line in ipairs(contents) do
handle:write(line.."\n")
end
handle:close()
else
error("Could not write startup file!")
end
end
if #args >= 1 then
if args[1] == "execute" then
shell.setPath(shell.path()..":/usr/bin")
local loadAPI = os.loadAPI
os.loadAPI = function(path)
if fs.exists(path) then
return loadAPI(path)
elseif fs.exists(fs.combine("/usr/apis", path)) then
return loadAPI(fs.combine("/usr/apis", path))
end
end
elseif args[1] == "install" then
if tonumber(string.sub(os.version(), 9)) and tonumber(string.sub(os.version(), 9)) > 1.7 and fs.isDir("/startup") then
--In CraftOS 1.8+, simply add a file to the startup folder.
local handle = io.open("/startup/easy-shell.lua")
if handle then
handle:write(exStr)
handle:close()
else
error("Could not write file!")
end
else
--otherwise, attempt to cleanly add a line to the startup file.
local contents = loadStartup()
local changed = false
if fs.exists("/startup") then
local exists = false
for i = 1, #contents do
if contents[i] == exStr then
exists = true
break
end
end
if not exists then
table.insert(contents, 1, exStr)
changed = true
end
else
contents[1] = exStr
changed = true
end
if changed then
writeStartup(contents)
end
end
shell.run("usr/bin/easy-shell execute")
elseif args[1] == "remove" then
if tonumber(string.sub(os.version(), 9)) and tonumber(string.sub(os.version(), 9)) > 1.7 and fs.exists("/startup/easy-shell.lua") then
--in 1.8+, we just have to delete the file we added.
fs.delete("/startup/easy-shell.lua")
else
local contents = loadStartup()
for i = #contents, 1, -1 do
if contents[i] == exStr then
table.remove(contents, i)
end
end
writeStartup(contents)
end
end
end