f9a91df063b8a30483a5f2bfefdfe9df627b7937
[librecmc/librecmc.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 }
44
45 uci_revert_state() {
46         local PACKAGE="$1"
47         local CONFIG="$2"
48         local OPTION="$3"
49
50         /sbin/uci -P /var/state revert "$PACKAGE${CONFIG:+.$CONFIG}${OPTION:+.$OPTION}"
51 }
52
53 uci_set_state() {
54         local PACKAGE="$1"
55         local CONFIG="$2"
56         local OPTION="$3"
57         local VALUE="$4"
58
59         [ -z "$VALUE" ] && return 0
60         /sbin/uci -P /var/state set "$PACKAGE.$CONFIG${OPTION:+.$OPTION}=$VALUE"
61 }
62
63 uci_set() {
64         local PACKAGE="$1"
65         local CONFIG="$2"
66         local OPTION="$3"
67         local VALUE="$4"
68
69         /sbin/uci set "$PACKAGE.$CONFIG.$OPTION=$VALUE"
70 }
71
72 uci_add() {
73         local PACKAGE="$1"
74         local TYPE="$2"
75         local CONFIG="$3"
76
77         if [ -z "$CONFIG" ]; then
78                 export ${NO_EXPORT:+-n} CONFIG_SECTION="$(/sbin/uci add "$PACKAGE" "$TYPE")"
79         else
80                 /sbin/uci set "$PACKAGE.$CONFIG=$TYPE"
81                 export ${NO_EXPORT:+-n} CONFIG_SECTION="$CONFIG"
82         fi
83 }
84
85 uci_rename() {
86         local PACKAGE="$1"
87         local CONFIG="$2"
88         local VALUE="$3"
89
90         /sbin/uci rename "$PACKAGE.$CONFIG=$VALUE"
91 }
92
93 uci_remove() {
94         local PACKAGE="$1"
95         local CONFIG="$2"
96         local OPTION="$3"
97
98         /sbin/uci del "$PACKAGE.$CONFIG${OPTION:+.$OPTION}"
99 }
100
101 uci_commit() {
102         local PACKAGE="$1"
103         /sbin/uci commit $PACKAGE
104 }