added RCSID
[strongswan.git] / testing / scripts / function.sh
1 #!/bin/bash
2 # provides some general-purpose script functions
3 #
4 # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
5 # Zuercher Hochschule Winterthur
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17 # RCSID $Id$
18
19
20 ############################################
21 # print output in color
22 #
23
24 function cecho {
25 echo -e "\033[1;31m$1\033[0m"
26 }
27 function cgecho {
28 echo -e "\033[1;32m$1\033[0m"
29 }
30
31 function cecho-n {
32 echo -en "\033[1;31m$1\033[0m"
33 }
34
35
36 #############################################
37 # output all args to stderr and exit with
38 # return code 1
39 #
40
41 die() {
42 echo $* 1>&2
43 exit 1
44 }
45
46 #############################################
47 # search and replace strings throughout a
48 # whole directory
49 #
50
51 function searchandreplace {
52
53 SEARCHSTRING="$1"
54 REPLACESTRING="$2"
55 DESTDIR="$3"
56
57 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
58
59
60 #########################
61 # create a temporary file
62 #
63
64 TMPFILE="/tmp/sr.$$"
65
66
67 ###########################################
68 # search and replace in each found file the
69 # given string
70 #
71
72 for eachfoundfile in `find $DESTDIR -type f`
73 do
74 sed -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile" > "$TMPFILE"
75 cp -f "$TMPFILE" "$eachfoundfile"
76 done
77
78
79 ###########################
80 # delete the temporary file
81 #
82
83 rm -f "$TMPFILE"
84
85 }