Split a part of getconf into 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    CURRENT_PARAM=$1
35    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    ret=`printconf "$1" "$2"`
52
53    # replace * with %, so that it is not globbed.
54    ret="${ret//\\*/__star__}"
55
56    # this is weird, but single quotes are needed to
57    # allow for returned values with spaces. $ret is still expanded
58    # because it is in an 'eval' statement.
59    eval $1='$ret'
60 }