removing svn keyword $Id$ from all files
[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
18 ############################################
19 # print output in color
20 #
21
22 function cecho {
23 echo -e "\033[1;31m$1\033[0m"
24 }
25 function cgecho {
26 echo -e "\033[1;32m$1\033[0m"
27 }
28
29 function cecho-n {
30 echo -en "\033[1;31m$1\033[0m"
31 }
32
33
34 #############################################
35 # output all args to stderr and exit with
36 # return code 1
37 #
38
39 die() {
40 echo $* 1>&2
41 exit 1
42 }
43
44 #############################################
45 # search and replace strings throughout a
46 # whole directory
47 #
48
49 function searchandreplace {
50
51 SEARCHSTRING="$1"
52 REPLACESTRING="$2"
53 DESTDIR="$3"
54
55 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
56
57
58 #########################
59 # create a temporary file
60 #
61
62 TMPFILE="/tmp/sr.$$"
63
64
65 ###########################################
66 # search and replace in each found file the
67 # given string
68 #
69
70 for eachfoundfile in `find $DESTDIR -type f`
71 do
72 sed -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile" > "$TMPFILE"
73 cp -f "$TMPFILE" "$eachfoundfile"
74 done
75
76
77 ###########################
78 # delete the temporary file
79 #
80
81 rm -f "$TMPFILE"
82
83 }
84
85 #############################################
86 # add a bridge
87 #
88
89 function umlbr_add {
90 brctl addbr "umlbr$1"
91 brctl setfd "umlbr$1" 0
92 brctl setageing "umlbr$1" 3600
93 brctl stp "umlbr$1" off
94 ifconfig "umlbr$1" "$2" netmask "$3" up
95 }
96
97 #############################################
98 # delete a bridge
99 #
100
101 function umlbr_del {
102 ifconfig "umlbr$1" down &> /dev/null 2>&1
103 brctl delbr "umlbr$1" &> /dev/null 2>&1
104 }
105
106 #############################################
107 # add a tap interface to a bridge
108 #
109
110 function umlbr_add_tap {
111 tunctl -t "tap$1_$2" &> /dev/null 2>&1
112 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
113 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
114 cecho-n "$2.."
115 }
116
117 #############################################
118 # delete a tap interface from a bridge
119 #
120
121 function umlbr_del_tap {
122 ifconfig "umlbr$2" down &> /dev/null 2>&1
123 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
124 tunctl -d "tap$1_$2" &> /dev/null 2>&1
125 cecho-n "$2.."
126 }
127