* luci/contrib: add fork of openwrt olsrd
[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.util")
19 require("luci.model.uci")
20
21 local conf = luci.model.uci.get_all("olsr")
22
23 local function _value(val)
24         if val:match("^[0-9%. \t]+$") or val == "yes" or val == "no" then
25                 return val
26         else
27                 return string.format( '"%s"', val )
28         end
29 end
30
31 local function _section(sect,sval,parstr)
32         local rv  = ""
33         local pad = ""
34
35         if sval then
36                 rv  = string.format( '%s "%s"\n{\n', conf[sect][".type"], conf[sect][sval] )
37                 pad = "\t"
38         end
39
40         for k, v in luci.util.spairs(conf[sect]) do
41                 if k:sub(1,1) ~= '.' and k ~= sval then
42                         if parstr then
43                                 rv = rv .. string.format(
44                                         '%s%s "%s"\t"%s"\n',
45                                         pad, parstr,
46                                         k:gsub( "_", "-" ),     -- XXX: find a better solution for this
47                                         v
48                                 )
49                         else
50                                 rv = rv .. string.format(
51                                         '%s%s\t%s\n',
52                                         pad, k, _value(v)
53                                 )
54                         end
55                 end
56         end
57
58         if sval then
59                 rv = rv .. "}\n"
60         end     
61
62         return rv
63 end
64
65 local function _hna(sval)
66         local rv = string.format( "%s\n{\n", sval )
67
68         for k, v in luci.util.spairs(conf) do
69                 if conf[k][".type"] == sval and conf[k].NetAddr and conf[k].Prefix then
70                         rv = rv .. string.format(
71                                 "\t%s\t%s\n",
72                                 conf[k].NetAddr,
73                                 conf[k].Prefix
74                         )
75                 end
76         end
77
78         return rv .. "}\n"
79 end
80
81 local function _ipc(sval)
82         local rv = string.format( "%s\n{\n", sval )
83
84         for k, v in luci.util.spairs(conf[sval]) do
85                 if k:sub(1,1) ~= "." then
86                         local vals = luci.util.split(v, "%s+", nil, true)
87
88                         if k == "Net" then
89                                 for i = 1,#vals,2 do
90                                         rv = rv .. string.format(
91                                                 "\tNet\t%s\t%s\n",
92                                                 vals[i], vals[i+1]
93                                         )
94                                 end
95                         elseif k == "Host" then
96                                 for i, v in ipairs(vals) do
97                                         rv = rv .. string.format(
98                                                 "\t%s\t%s\n",
99                                                 k, vals[i]
100                                         )
101                                 end
102                         else
103                                 rv = rv .. string.format(
104                                         "\t%s\t%s\n",
105                                         k, v
106                                 )
107                         end
108                 end
109         end
110
111         return rv .. "}\n"
112 end
113
114
115 -- general config section
116 print( _section("general") )
117
118 -- plugin config sections
119 for k, v in luci.util.spairs(conf) do
120         if conf[k][".type"] == "LoadPlugin" then
121                 print( _section( k, "Library", "PlParam" ) )
122         end
123 end
124
125 -- interface config sections
126 for k, v in luci.util.spairs(conf) do
127         if conf[k][".type"] == "Interface" then
128                 print( _section( k, "Interface" ) )
129         end
130 end
131
132 -- write Hna4, Hna6 sections
133 print( _hna("Hna4") )
134 print( _hna("Hna6") )
135
136 -- write IpcConnect section
137 print( _ipc("IpcConnect") )