Use red color in die() function
[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 # execute command
18 # $1 - command to execute
19 # $2 - whether or not to log command exit status
20 # (0 -> disable exit status logging)
21 execute()
22 {
23 cmd=${1}
24 echo $cmd >>$LOGFILE 2>&1
25 $cmd >>$LOGFILE 2>&1
26 status=$?
27 [ "$2" != 0 ] && log_status $status
28 if [ $status != 0 ]; then
29 echo
30 echo "! Command $cmd failed, exiting (status $status)"
31 echo "! Check why here $LOGFILE"
32 exit 1
33 fi
34 }
35
36 # execute command in chroot
37 # $1 - command to execute
38 execute_chroot()
39 {
40 execute "chroot $LOOPDIR $@"
41 }
42
43 export TERM=xterm
44 RED=$(tput setaf 1)
45 GREEN=$(tput setaf 2)
46 NORMAL=$(tput op)
47
48 function cecho {
49 echo -e "\033[1;31m$1\033[0m"
50 }
51 function cgecho {
52 echo -e "\033[1;32m$1\033[0m"
53 }
54
55 function cecho-n {
56 echo -en "\033[1;31m$1\033[0m"
57 }
58
59 # log an action
60 # $1 - current action description
61 log_action()
62 {
63 /bin/echo -n "[....] $1 "
64 }
65
66 # log an action status
67 # $1 - exit status of action
68 log_status()
69 {
70 tput hpa 0
71 if [ $1 -eq 0 ]; then
72 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
73 else
74 /bin/echo -ne "[${RED}FAIL${NORMAL}"
75 fi
76 echo
77 }
78
79 # exit with given error message
80 # $1 - error message
81 die() {
82 echo -e "${RED}$1${NORMAL}"
83 exit 1
84 }
85
86 #############################################
87 # search and replace strings throughout a
88 # whole directory
89 #
90
91 function searchandreplace {
92
93 SEARCHSTRING="$1"
94 REPLACESTRING="$2"
95 DESTDIR="$3"
96
97 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
98
99
100 ###########################################
101 # search and replace in each found file the
102 # given string
103 #
104
105 for eachfoundfile in `find $DESTDIR -type f`
106 do
107 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
108 done
109
110 }
111
112 #############################################
113 # add a bridge
114 #
115
116 function umlbr_add {
117 brctl addbr "umlbr$1"
118 brctl setfd "umlbr$1" 0
119 brctl setageing "umlbr$1" 3600
120 brctl stp "umlbr$1" off
121 ifconfig "umlbr$1" "$2" netmask "$3" up
122 }
123
124 #############################################
125 # delete a bridge
126 #
127
128 function umlbr_del {
129 ifconfig "umlbr$1" down &> /dev/null 2>&1
130 brctl delbr "umlbr$1" &> /dev/null 2>&1
131 }
132
133 #############################################
134 # add a tap interface to a bridge
135 #
136
137 function umlbr_add_tap {
138 tunctl -t "tap$1_$2" &> /dev/null 2>&1
139 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
140 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
141 cecho-n "$2.."
142 }
143
144 #############################################
145 # delete a tap interface from a bridge
146 #
147
148 function umlbr_del_tap {
149 ifconfig "umlbr$2" down &> /dev/null 2>&1
150 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
151 tunctl -d "tap$1_$2" &> /dev/null 2>&1
152 cecho-n "$2.."
153 }
154