Use local variables in getconf & printconf.
[matthijs/upstream/backupninja.git] / lib / tools.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4
5 # This file contains functions shared between ninjahelper and backupninja.
6
7 #####################################################
8 ## MISC FUNCTIONS
9
10 #
11 # create a temporary file in a secure way.
12 #
13 function maketemp() {
14    local tempfile=`mktemp /tmp/$1.XXXXXXXX`
15    echo $tempfile
16 }
17
18 #####################################################
19 ## CONFIG-FILE RELATED FUNCTIONS
20
21 function setfile() {
22    CURRENT_CONF_FILE=$1
23 }
24
25 function setsection() {
26    CURRENT_SECTION=$1
27 }
28
29 #
30 # Retrieves the configuration variable named $1 from the current config
31 # file and section and echoes its value. If it is empty or not found, $2
32 # is used.
33 function printconf() {
34    local CURRENT_PARAM=$1
35    local ret=`@AWK@ -f $libdirectory/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
36    # if nothing is returned, set the default
37    if [ "$ret" == "" -a "$2" != "" ]; then
38       ret="$2"
39    fi
40
41    echo "$ret"
42 }
43
44
45 #
46 # Retrieves the configuration variable named $1 from the current config
47 # file and section and assigns its value to the global variable with the
48 # same name. If it is empty or not found, $2 is used.
49 #
50 function getconf() {
51    local ret=`printconf "$1" "$2"`
52
53    # We use escape the $ in $ret to delay expansion of $ret, so when $1
54    # is foo, eval sees foo=$ret and properly does the assignment
55    # (without the backslash, the right part of the assignment would be
56    # whatever is in ret and be subject to all kinds of expansion.
57    eval $1=\$ret
58 }