* luci/contrib: olsrd-luci: only write sections for existing plugins, change default...
[project/luci.git] / contrib / package / olsrd-luci / files / lib / config / olsr.lua
1 #!/usr/bin/lua
2
3 --[[
4
5 OLSRd configuration generator
6 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15
16 ]]--
17
18 require("luci.fs")
19 require("luci.util")
20 require("luci.model.uci")
21
22 local conf = luci.model.uci.get_all("olsr")
23
24 local function _value(val)
25 if val:match("^[0-9%. \t]+$") or val == "yes" or val == "no" then
26 return val
27 else
28 return string.format( '"%s"', val )
29 end
30 end
31
32 local function _section(sect,sval,parstr)
33 local rv = ""
34 local pad = ""
35
36 if sval then
37 rv = string.format( '%s "%s"\n{\n', conf[sect][".type"], conf[sect][sval] )
38 pad = "\t"
39 end
40
41 for k, v in luci.util.spairs(conf[sect]) do
42 if k:sub(1,1) ~= '.' and k ~= sval then
43 if parstr then
44 rv = rv .. string.format(
45 '%s%s "%s"\t"%s"\n',
46 pad, parstr,
47 k:gsub( "_", "-" ), -- XXX: find a better solution for this
48 v
49 )
50 else
51 rv = rv .. string.format(
52 '%s%s\t%s\n',
53 pad, k, _value(v)
54 )
55 end
56 end
57 end
58
59 if sval then
60 rv = rv .. "}\n"
61 end
62
63 return rv
64 end
65
66 local function _hna(sval)
67 local rv = string.format( "%s\n{\n", sval )
68
69 for k, v in luci.util.spairs(conf) do
70 if conf[k][".type"] == sval and conf[k].NetAddr and conf[k].Prefix then
71 rv = rv .. string.format(
72 "\t%s\t%s\n",
73 conf[k].NetAddr,
74 conf[k].Prefix
75 )
76 end
77 end
78
79 return rv .. "}\n"
80 end
81
82 local function _ipc(sval)
83 local rv = string.format( "%s\n{\n", sval )
84
85 for k, v in luci.util.spairs(conf[sval]) do
86 if k:sub(1,1) ~= "." then
87 local vals = luci.util.split(v, "%s+", nil, true)
88
89 if k == "Net" then
90 for i = 1,#vals,2 do
91 rv = rv .. string.format(
92 "\tNet\t%s\t%s\n",
93 vals[i], vals[i+1]
94 )
95 end
96 elseif k == "Host" then
97 for i, v in ipairs(vals) do
98 rv = rv .. string.format(
99 "\t%s\t%s\n",
100 k, vals[i]
101 )
102 end
103 else
104 rv = rv .. string.format(
105 "\t%s\t%s\n",
106 k, v
107 )
108 end
109 end
110 end
111
112 return rv .. "}\n"
113 end
114
115
116 -- general config section
117 print( _section("general") )
118
119 -- plugin config sections
120 for k, v in luci.util.spairs(conf) do
121 if conf[k][".type"] == "LoadPlugin" then
122 if v.Library and luci.fs.access("/usr/lib/"..v.Library) then
123 print( _section( k, "Library", "PlParam" ) )
124 end
125 end
126 end
127
128 -- interface config sections
129 for k, v in luci.util.spairs(conf) do
130 if conf[k][".type"] == "Interface" then
131 print( _section( k, "Interface" ) )
132 end
133 end
134
135 -- write Hna4, Hna6 sections
136 print( _hna("Hna4") )
137 print( _hna("Hna6") )
138
139 -- write IpcConnect section
140 print( _ipc("IpcConnect") )