2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
4 # This file contains functions shared between ninjahelper and backupninja.
6 #####################################################
10 # create a temporary file in a secure way.
13 local tempfile=`mktemp /tmp/$1.XXXXXXXX`
17 #####################################################
18 ## CONFIG-FILE RELATED FUNCTIONS
24 function setsection() {
29 # sets a global var with name equal to $1
30 # to the value of the configuration parameter $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
41 # replace * with %, so that it is not globbed.
42 ret="${ret//\\*/__star__}"
44 # this is weird, but single quotes are needed to
45 # allow for returned values with spaces. $ret is still expanded
46 # because it is in an 'eval' statement.
51 # Replaces escape sequences in $1 with the proper values for the vserver $2.
52 # The result is put on stdout. If $2 is empty, values for the host are
53 # replaced. The following values are replaced:
56 # %h The short hostname (as returned by hostname -s)
57 # %H The full hostname (as returned by hostname --fqdn)
58 # %n The vserver name, or empty for the host
59 # %N The vserver name, or "host" for the host
60 # %v The vserver root directory, or empty for the host
62 # Note that the given vserver must be running!
64 function interpolate() {
67 vexec=${vsname:+$VSERVER $vsname exec }
68 # Find the values to replace
69 h=`$vexec hostname -s`
70 H=`$vexec hostname --fqdn`
73 v=${vsname:+$VROOTDIR/$vsname}
76 for var in h H n N v; do
77 # Do indirect lookup of the value to replace
79 # Escape slashes (twice, bash seems to eat one somewhere)
80 val=`echo "$val" | sed 's#/#\\\\/#g'`
81 # Add replacement pattern. The first part checks that there is
82 # an odd number of percent signs before the variable, so
83 # double % is properly left alone.
84 expr="${expr}s/\(\(^\|[^%]\)\(%%\)*\)%$var/\1$val/g;"
86 # Finally replace literal % signs
87 expr="${expr}s/%%/%/g"
89 # Do the actual interpolation
90 echo $path | sed "$expr"