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