* luci/applications: olsr: add own initscript and configuration
[project/luci.git] / applications / luci-olsr / root / 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: olsr.lua 2516 2008-07-06 13:34:07Z jow $
15
16 ]]--
17
18 require("luci.util")
19 require("luci.model.uci")
20
21 luci.model.uci.load_state("network")
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 local val = ""
38
39 if sval == "Interface" then
40 val = luci.model.uci.get( "network", conf[sect][sval], "ifname" )
41 else
42 val = conf[sect][sval]
43 end
44
45 rv = string.format( '%s "%s"\n{\n', conf[sect][".type"], val )
46 pad = "\t"
47 end
48
49 for k, v in luci.util.spairs(conf[sect]) do
50 if k:sub(1,1) ~= '.' and k ~= sval then
51 if parstr then
52 rv = rv .. string.format(
53 '%s%s "%s"\t"%s"\n',
54 pad, parstr,
55 k:gsub( "_", "-" ), -- XXX: find a better solution for this
56 v
57 )
58 else
59 rv = rv .. string.format(
60 '%s%s\t%s\n',
61 pad, k, _value(v)
62 )
63 end
64 end
65 end
66
67 if sval then
68 rv = rv .. "}\n"
69 end
70
71 return rv
72 end
73
74 local function _hna(sval)
75 local rv = string.format( "%s\n{\n", sval )
76 local cnt = 0
77
78 for k, v in luci.util.spairs(conf) do
79 if conf[k][".type"] == sval and conf[k].NetAddr and conf[k].Prefix then
80 cnt = cnt + 1
81 rv = rv .. string.format(
82 "\t%s\t%s\n",
83 conf[k].NetAddr,
84 conf[k].Prefix
85 )
86 end
87 end
88
89 return ( cnt > 0 and rv .. "}\n" or "" )
90 end
91
92 local function _ipc(sval)
93 if conf[sval] and ( conf[sval].MaxConnections == nil or tonumber(conf[sval].MaxConnections) > 0 ) then
94 local rv = string.format( "%s\n{\n", sval )
95
96 for k, v in luci.util.spairs(conf[sval]) do
97 if k:sub(1,1) ~= "." then
98 local vals = luci.util.split(v, "%s+", nil, true)
99
100 if k == "Net" then
101 for i = 1,#vals,2 do
102 rv = rv .. string.format(
103 "\tNet\t%s\t%s\n",
104 vals[i], vals[i+1]
105 )
106 end
107 elseif k == "Host" then
108 for i, v in ipairs(vals) do
109 rv = rv .. string.format(
110 "\t%s\t%s\n",
111 k, vals[i]
112 )
113 end
114 else
115 rv = rv .. string.format(
116 "\t%s\t%s\n",
117 k, v
118 )
119 end
120 end
121 end
122
123 return rv .. "}\n"
124 else
125 return ""
126 end
127 end
128
129
130 -- general config section
131 print( _section("general") )
132
133 -- plugin config sections
134 for k, v in luci.util.spairs(conf) do
135 if conf[k][".type"] == "LoadPlugin" then
136 print( _section( k, "Library", "PlParam" ) )
137 end
138 end
139
140 -- interface config sections
141 for k, v in luci.util.spairs(conf) do
142 if conf[k][".type"] == "Interface" then
143 print( _section( k, "Interface" ) )
144 end
145 end
146
147 -- write Hna4, Hna6 sections
148 print( _hna("Hna4") )
149 print( _hna("Hna6") )
150
151 -- write IpcConnect section
152 print( _ipc("IpcConnect") )
153