b0f6cb291ea6b844f52f8bc0dfe5085ae787e5f5
[project/luci.git] / contrib / package / meshwizard / files / usr / bin / meshwizard / functions.sh
1 uci_remove_list_element() {
2 local option="$1"
3 local value="$2"
4 local list="$(uci get $option)"
5 local elem
6
7 uci delete $option
8 for elem in $list; do
9 if [ "$elem" != "$value" ]; then
10 uci add_list $option=$elem
11 fi
12 done
13 }
14
15 # string_contains(string, substring)
16 #
17 # Returns 0 if the specified string contains the specified substring,
18 # otherwise returns 1.
19 string_contains() {
20 string="$1"
21 substring="$2"
22 if test "${string#*$substring}" != "$string"
23 then
24 return 0 # $substring is in $string
25 else
26 return 1 # $substring is not in $string
27 fi
28 }
29
30 # Takes 2 arguments
31 # $1 = text to be displayed in the output for this section
32 # $2 = section (optional)
33 uci_commitverbose() {
34 echo "+ $1"
35 uci changes $2 | while read line; do
36 echo " $line"
37 done
38 uci commit $2
39 }
40
41 set_defaults() {
42 for def in $(env |grep "^$1" | sed 's/ /_/g'); do
43 option="${def/$1/}"
44 a="$(echo $option |cut -d '=' -f1)"
45 b="$(echo $option |cut -d '=' -f2-)"
46 b="${b//_/ }"
47 string_contains "$a" "_LENGTH" && continue
48 string_contains "$a" "_ITEM" && {
49 # special threatment for lists. use add_list and remove the
50 # item index (_ITEMx).
51 uci add_list $2.${a//_ITEM[0-9]*/}="$b"
52 } || {
53 uci set $2.$a="$b"
54 }
55 done
56 }
57
58 # 3 arguments: 1=config name 2=oldname 3=newname
59 section_rename() {
60 uci -q rename $1.$2=$3 && msg_rename $1.$2 $1.$3 || msg_rename_error $1.$2 $1.$3
61 }
62
63 msg_missing_value() {
64 echo -e " \033[1mWarning:\033[0m Configuration option for $2 is missing in $1."
65 }
66
67 msg_success() {
68 echo " Finished."
69 }
70
71 msg_error() {
72 echo " \033[1mError: \033[0mThere was a problem."
73 }
74
75 msg_rename() {
76 echo " Renamed unnamed section $1 to $2."
77 }
78
79 msg_rename_error() {
80 echo " \033[1mWarning:\033[0m Could not rename $1 to $2."
81 }
82
83
84 restore_factory_defaults() {
85 echo "+ Restore default config as requested with cleanup=1"
86 cp -f /rom/etc/config/* /etc/config/
87 rm /etc/config/wireless
88 wifi detect > /etc/config/wireless
89 rm /etc/config/network
90 if [ -f /etc/init.d/defconfig ]; then
91 # legacy (AA)
92 /etc/init.d/defconfig start
93 [ -f /rom/etc/uci-defaults/network ] && sh /rom/etc/uci-defaults/network
94 else
95 sh /rom/etc/uci-defaults/02_network
96 fi
97 }
98
99 is_in_list() {
100 # checks if an item is in a list
101 local list="$1"
102 local item="$2"
103 for word in $list; do
104 [ $word = "$item" ] && return 0
105 done
106 return 1
107 }
108
109 add_to_list() {
110 local list="$1"
111 local item="$2"
112 is_in_list "$list" "$item" && echo $list
113 if [ -z "$list" ]; then
114 echo "$item"
115 else
116 echo "$list $item"
117 fi
118 }