Add strip_prefix function.
[matthijs/upstream/backupninja.git] / lib / tools.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3
4 # This file contains functions shared between ninjahelper and backupninja.
5
6 #####################################################
7 ## MISC FUNCTIONS
8
9 #
10 # create a temporary file in a secure way.
11 #
12 function maketemp() {
13         local tempfile=`mktemp /tmp/$1.XXXXXXXX`
14         echo $tempfile
15 }
16
17 #####################################################
18 ## CONFIG-FILE RELATED FUNCTIONS
19
20 function setfile() {
21         CURRENT_CONF_FILE=$1
22 }
23
24 function setsection() {
25         CURRENT_SECTION=$1
26 }
27
28 #
29 # sets a global var with name equal to $1
30 # to the value of the configuration parameter $1
31 # $2 is the default.
32
33 function getconf() {
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         # replace * with %, so that it is not globbed.
42         ret="${ret//\\*/__star__}"
43
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.
47         eval $1='$ret'
48 }
49
50 #
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:
54 #
55 # %%  Literal %
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
61 #
62 # Note that the given vserver must be running!
63 #
64 function interpolate() {
65         path=$1
66         vsname=$2
67         vexec=${vsname:+$VSERVER $vsname exec }
68         # Find the values to replace
69         h=`$vexec hostname -s`
70         H=`$vexec hostname --fqdn`
71         n=$vsname
72         N=${vsname:-host}
73         v=${vsname:+$VROOTDIR/$vsname}
74
75         expr=''
76         for var in h H n N v; do
77                 # Do indirect lookup of the value to replace
78                 val=${!var}
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;"
85         done
86         # Finally replace literal % signs
87         expr="${expr}s/%%/%/g"
88
89         # Do the actual interpolation
90         echo $path | sed "$expr"
91 }
92
93 # Does the $1 start with $2 ?
94 function starts_with() {
95         # Get the head of $1 as long as $2
96         head=`echo $1 | head -c ${#2}`
97         # Is it equal to $2?
98         [ "$head" = "$2" ]
99 }
100
101 # Strip the prefix $2 from $1. Assumes that $1 actually starts with $1.
102 # The result is put on stdout.
103 function strip_prefix() {
104         # Strip the first ${#2} (the length of $2) characters from $1.
105         # tail -c +N means start at the Nth byte, 1-based, so we add 1.
106         echo $1 | tail -c +$((${#2}+1))
107 }