contrib/meshwizard: Fix setting of values which contain spaces & cleanups
[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 # Takes 2 arguments
16 # $1 = text to be displayed in the output for this section
17 # $2 = section (optional)
18 uci_commitverbose() {
19         echo "+ $1"
20         uci changes $2 | while read line; do
21                 echo "    $line"
22         done
23         uci commit $2
24 }
25
26 set_defaults() {
27         for def in $(env |grep "^$1" | sed 's/ /_/g'); do
28                 option="${def/$1/}"
29                 a="$(echo $option |cut -d '=' -f1)"
30                 b="$(echo $option |cut -d '=' -f2)"
31                 b="${b//_/ }"
32                 uci set $2.$a="$b"
33         done
34 }
35
36 # 1 argument: section to remove
37 section_cleanup() {
38         uci -q delete $1 && msg_cleanup $1 || msg_cleanup_error $1
39 }
40
41 # 3 arguements: 1=config name 2=oldname 3=newname
42 section_rename() {
43         uci -q rename $1.$2=$3 && msg_rename $1.$2 $1.$3 || msg_rename_error $1.2 $1.$3
44 }
45
46 msg_cleanup() {
47         echo "    Cleanup: Removed section $1."
48 }
49
50 msg_cleanup_error() {
51         echo -e "    \033[1mWarning:\033[0m Cleanup of $1 failed."
52 }
53
54 msg_missing_value() {
55         echo -e "    \033[1mWarning:\033[0m Configuration option for $2 is missing in $1."
56 }
57
58 msg_success() {
59         echo "    Finished."
60 }
61
62 msg_error() {
63         echo "    \033[1mError: \033[0mThere was a problem."
64 }
65
66 msg_rename() {
67         echo "    Renamed unnamed section $1 to $2."
68 }
69
70 msg_rename_error() {
71         echo "    \033[1mWarning:\033[0m Could not rename $1 to $2."
72 }