01a93874abc06061066267ed6ab3b14f5e084df9
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / helpers / setup_olsrd.sh
1 #!/bin/sh
2 # Sets up olsrd
3
4 . /lib/functions.sh
5 . $dir/functions.sh
6
7 protocols="4"
8 if [ "$ipv6_enabled" = 1 ] && [ "$has_ipv6" == "1" ]; then
9     protocols="4 6"
10 fi
11
12 clean_config() {
13     # Clean the config, remove interface wlan
14     handle_interface() {
15             config_get interface "$1" interface
16             if [ "$interface" = "wlan" ]; then
17                     uci delete $cfg.$1
18             fi
19     }
20     config_foreach handle_interface Interface
21 }
22
23 rename_olsrd() {
24     #Rename olsrd basic settings
25     handle_olsrd() {
26         if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
27                 section_rename $cfg $1 olsrd
28         fi
29     }
30     config_foreach handle_olsrd olsrd
31 }
32
33 rename_interface_defaults() {
34     # Rename interface defaults
35     handle_interfacedefaults() {
36             if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
37                     section_rename $cfg $1 InterfaceDefaults
38             fi
39     }
40     config_foreach handle_interfacedefaults InterfaceDefaults
41 }
42
43 cleanup_plugins() {
44     # Rename nameservice, dyngw and httpinfo plugins
45     handle_plugin() {
46             config_get library "$1" library
47             if [ -z "${1/cfg[0-9a-fA-F]*/}" ]; then
48                     new="$(echo $library | cut -d '.' -f 1)"
49                     section_rename $cfg "$1" "$new"
50             fi
51     }
52     config_foreach handle_plugin LoadPlugin
53     uci -q delete $cfg.olsrd_httpinfo
54     uci -q delete $cfg.olsrd_dyn_gw
55 }
56
57 setup_nameservice() {
58     # Setup nameservice plugin
59     if [ -n "$profile_suffix" ]; then
60             suffix=".$profile_suffix"
61     else
62             suffix=".olsr"
63     fi
64     local llfile="/var/run/latlon.js"
65     local hosts="/var/etc/hosts.olsr"
66     local services="/var/run/services_olsr"
67
68     if [ "$proto" = "6" ]; then
69         local llfile="/var/run/latlon.js.ipv6"
70         local hosts="/var/etc/hosts.olsr.ipv6"
71         local services="/var/run/services_olsr.ipv6"
72     fi
73
74         uci batch <<- EOF
75                 set $cfg.olsrd_nameservice=LoadPlugin
76                 set $cfg.olsrd_nameservice.library="olsrd_nameservice.so.0.3"
77                 set $cfg.olsrd_nameservice.latlon_file="$llfile"
78                 set $cfg.olsrd_nameservice.hosts_file="$hosts"
79                 set $cfg.olsrd_nameservice.sighup_pid_file="/var/run/dnsmasq.pid"
80                 set $cfg.olsrd_nameservice.services_file="$services"
81                 set $cfg.olsrd_nameservice.suffix="$suffix"
82         EOF
83
84     uci_commitverbose "Setup olsr nameservice plugin" $cfg
85 }
86
87 setup_dyngw_plain() {
88     # Setup dyngw_plain
89     # If Sharing of Internet is enabled then enable dyngw_plain plugin
90
91     if [ "$general_sharenet" == 1 ]; then
92         uci set $cfg.dyngw_plain=LoadPlugin
93         uci set $cfg.dyngw_plain.ignore=0
94         uci set $cfg.dyngw_plain.library="olsrd_dyn_gw_plain.so.0.4"
95         uci_commitverbose "Setup olsrd_dyngw_plain plugin" $cfg
96     fi
97
98 }
99
100 setup_watchdog() {
101     # Setup watchdog
102     local watchdogfile="/var/run/olsrd.watchdog"
103     if [ "$proto" = "6" ]; then
104         watchdogfile="/var/run/olsrd.watchdog.ipv6"
105     fi
106
107         uci batch <<- EOF
108                 set $cfg.olsrd_watchdog=LoadPlugin
109                 set $cfg.olsrd_watchdog.library="olsrd_watchdog.so.0.1"
110                 set $cfg.olsrd_watchdog.file="$watchdogfile"
111                 set $cfg.olsrd_watchdog.interval=30
112         EOF
113     uci_commitverbose "Setup olsr watchdog plugin" $cfg
114
115 }
116
117 setup_jsoninfo() {
118         proto="$1"
119         uci batch <<- EOF
120                 set $cfg.olsrd_jsoninfo=LoadPlugin
121                 set $cfg.olsrd_jsoninfo.library="olsrd_jsoninfo.so.0.0"
122         EOF
123         if [ "$proto" = "6" ]; then
124                 uci set $cfg.olsrd_jsoninfo.ipv6only='1'
125         fi
126         uci_commitverbose "Setup olsr jsoninfo plugin" $cfg
127 }
128
129 setup_txtinfo() {
130         proto="$1"
131         uci batch <<- EOF
132             set $cfg.olsrd_txtinfo=LoadPlugin
133             set $cfg.olsrd_txtinfo.library="olsrd_txtinfo.so.0.1"
134         EOF
135         if [ "$proto" = "6" ]; then
136                 uci set $cfg.olsrd_txtinfo.ipv6only='1'
137         fi
138         uci_commitverbose "Setup olsr txtinfo plugin" $cfg
139
140
141
142 for proto in $protocols; do
143     cfg="olsrd"
144     [ "$proto" == "6" ] && cfg="olsrd6"
145     config_load $cfg
146     clean_config
147     rename_olsrd
148     cleanup_plugins
149
150     uci set $cfg.olsrd.IpVersion="$proto"
151     uci set $cfg.InterfaceDefaults=InterfaceDefaults
152     set_defaults "olsr_interfacedefaults_" $cfg.InterfaceDefaults
153     uci_commitverbose "Cleanup olsrd config" $cfg
154
155     setup_nameservice
156     setup_dyngw_plain
157     setup_watchdog
158     setup_jsoninfo $proto
159     setup_txtinfo $proto
160 done