c226f4fe1a6ab089eb4bff06845e08e89d4b27bd
[openwrt/svn-archive/archive.git] / package / uci / files / 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 if [ -z "$CONFIG_APPEND" ]; then
27 export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
28 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
29 export ${NO_EXPORT:+-n} CONFIG_SECTION=
30 fi
31
32 DATA="$(/sbin/uci ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
33 RET="$?"
34 [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
35 unset DATA
36
37 ${CONFIG_SECTION:+config_cb}
38 return "$RET"
39 }
40
41 uci_set_default() {
42 local PACKAGE="$1"
43 /sbin/uci -q show "$1" > /dev/null && return 0
44 /sbin/uci import "$1"
45 /sbin/uci commit "$1"
46 }
47
48 uci_revert_state() {
49 local PACKAGE="$1"
50 local CONFIG="$2"
51 local OPTION="$3"
52
53 /sbin/uci -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
54 }
55
56 uci_set_state() {
57 local PACKAGE="$1"
58 local CONFIG="$2"
59 local OPTION="$3"
60 local VALUE="$4"
61
62 [ -z "$VALUE" ] && return 0
63 /sbin/uci -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
64 }
65
66 uci_set() {
67 local PACKAGE="$1"
68 local CONFIG="$2"
69 local OPTION="$3"
70 local VALUE="$4"
71
72 /sbin/uci set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
73 }
74
75 uci_add() {
76 local PACKAGE="$1"
77 local TYPE="$2"
78 local CONFIG="$3"
79
80 if [ -z "$CONFIG" ]; then
81 export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
82 else
83 /sbin/uci set "$PACKAGE.$CONFIG=$TYPE"
84 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
85 fi
86 }
87
88 uci_rename() {
89 local PACKAGE="$1"
90 local CONFIG="$2"
91 local VALUE="$3"
92
93 /sbin/uci rename "$PACKAGE.$CONFIG=$VALUE"
94 }
95
96 uci_remove() {
97 local PACKAGE="$1"
98 local CONFIG="$2"
99 local OPTION="$3"
100
101 /sbin/uci del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
102 }
103
104 uci_commit() {
105 local PACKAGE="$1"
106 /sbin/uci commit $PACKAGE
107 }