cd8f6b160d90c69f3bf9f18427b0b1b2e13455b9
[openwrt/svn-archive/archive.git] / package / uci / files / uci / lib / config / uci.sh
1 #!/bin/sh
2 # Shell script compatibility wrappers for /sbin/uci
3 #
4 # Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 uci_load() {
21 local PACKAGE="$1"
22 local DATA
23 local RET
24
25 _C=0
26 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
27 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
28 export ${NO_EXPORT:+-n} CONFIG_SECTION=
29
30 DATA="$(/sbin/uci ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
31 RET="$?"
32 [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
33 unset DATA
34
35 ${CONFIG_SECTION:+config_cb}
36 return "$RET"
37 }
38
39 uci_set_default() {
40 local PACKAGE="$1"
41 /sbin/uci -q show "$1" > /dev/null && return 0
42 /sbin/uci import "$1"
43 /sbin/uci commit "$1"
44 }
45
46 uci_revert_state() {
47 local PACKAGE="$1"
48 local CONFIG="$2"
49 local OPTION="$3"
50
51 /sbin/uci -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
52 }
53
54 uci_set_state() {
55 local PACKAGE="$1"
56 local CONFIG="$2"
57 local OPTION="$3"
58 local VALUE="$4"
59
60 [ -z "$VALUE" ] && return 0
61 /sbin/uci -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
62 }
63
64 uci_set() {
65 local PACKAGE="$1"
66 local CONFIG="$2"
67 local OPTION="$3"
68 local VALUE="$4"
69
70 /sbin/uci set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
71 }
72
73 uci_add() {
74 local PACKAGE="$1"
75 local TYPE="$2"
76 local CONFIG="$3"
77
78 if [ -z "$CONFIG" ]; then
79 export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
80 else
81 /sbin/uci set "$PACKAGE.$CONFIG=$TYPE"
82 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
83 fi
84 }
85
86 uci_rename() {
87 local PACKAGE="$1"
88 local CONFIG="$2"
89 local VALUE="$3"
90
91 /sbin/uci rename "$PACKAGE.$CONFIG=$VALUE"
92 }
93
94 uci_remove() {
95 local PACKAGE="$1"
96 local CONFIG="$2"
97 local OPTION="$3"
98
99 /sbin/uci del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
100 }
101
102 uci_commit() {
103 local PACKAGE="$1"
104 /sbin/uci commit $PACKAGE
105 }