luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / modules / luci-mod-system / luasrc / model / cbi / admin_system / startup.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2010 Manuel Munz <freifunk at somakoma dot de>
4 -- Licensed to the public under the Apache License 2.0.
5
6 local fs  = require "nixio.fs"
7 local sys = require "luci.sys"
8
9 local inits = { }
10 local handled = false
11
12 for _, name in ipairs(sys.init.names()) do
13         local index   = sys.init.index(name)
14         local enabled = sys.init.enabled(name)
15
16         if index < 255 then
17                 inits["%02i.%s" % { index, name }] = {
18                         name    = name,
19                         index   = tostring(index),
20                         enabled = enabled
21                 }
22         end
23 end
24
25
26 m = SimpleForm("initmgr", translate("Initscripts"), translate("You can enable or disable installed init scripts here. Changes will applied after a device reboot.<br /><strong>Warning: If you disable essential init scripts like \"network\", your device might become inaccessible!</strong>"))
27 m.reset = false
28 m.submit = false
29
30
31 s = m:section(Table, inits)
32
33 i = s:option(DummyValue, "index", translate("Start priority"))
34 n = s:option(DummyValue, "name", translate("Initscript"))
35
36
37 e = s:option(Button, "endisable", translate("Enable/Disable"))
38
39 e.render = function(self, section, scope)
40         if inits[section].enabled then
41                 self.title = translate("Enabled")
42                 self.inputstyle = "save"
43         else
44                 self.title = translate("Disabled")
45                 self.inputstyle = "reset"
46         end
47
48         Button.render(self, section, scope)
49 end
50
51 e.write = function(self, section)
52         if inits[section].enabled then
53                 handled = true
54                 inits[section].enabled = false
55                 return sys.init.disable(inits[section].name)
56         else
57                 handled = true
58                 inits[section].enabled = true
59                 return sys.init.enable(inits[section].name)
60         end
61 end
62
63
64 start = s:option(Button, "start", translate("Start"))
65 start.inputstyle = "apply"
66 start.write = function(self, section)
67         handled = true
68         sys.call("/etc/init.d/%s %s >/dev/null" %{ inits[section].name, self.option })
69 end
70
71 restart = s:option(Button, "restart", translate("Restart"))
72 restart.inputstyle = "reload"
73 restart.write = start.write
74
75 stop = s:option(Button, "stop", translate("Stop"))
76 stop.inputstyle = "remove"
77 stop.write = start.write
78
79
80
81 f = SimpleForm("rc", translate("Local Startup"),
82         translate("This is the content of /etc/rc.local. Insert your own commands here (in front of 'exit 0') to execute them at the end of the boot process."))
83
84 t = f:field(TextValue, "rcs")
85 t.forcewrite = true
86 t.rmempty = true
87 t.rows = 20
88
89 function t.cfgvalue()
90         return fs.readfile("/etc/rc.local") or ""
91 end
92
93 function f.handle(self, state, data)
94         if not handled and state == FORM_VALID then
95                 if data.rcs then
96                         fs.writefile("/etc/rc.local", data.rcs:gsub("\r\n", "\n"))
97                 else
98                         fs.writefile("/etc/rc.local", "")
99                 end
100         end
101         return true
102 end
103
104 return m, f