Adding robbat2's patch for bug #62365.
[genkernel.git] / gen_funcs.sh
1 #!/bin/bash
2
3 isTrue() {
4         case "$1" in
5                 [Tt][Rr][Uu][Ee])
6                         return 0
7                 ;;
8                 [Tt])
9                         return 0
10                 ;;
11                 [Yy][Ee][Ss])
12                         return 0
13                 ;;
14                 [Yy])
15                         return 0
16                 ;;
17                 1)
18                         return 0
19                 ;;
20         esac
21         return 1
22 }
23
24 if isTrue ${USECOLOR}
25 then
26         GOOD=$'\e[32;01m'
27         WARN=$'\e[33;01m'
28         BAD=$'\e[31;01m'
29         NORMAL=$'\e[0m'
30         BOLD=$'\e[0;01m'
31         UNDER=$'\e[4m'
32 else
33         GOOD=''
34         WARN=''
35         BAD=''
36         NORMAL=''
37         BOLD=''
38         UNDER=''
39 fi
40
41 dump_debugcache() {
42         TODEBUGCACHE=0
43         echo "${DEBUGCACHE}" >> ${DEBUGFILE}
44 }
45
46 # print_info(debuglevel, print [, newline [, prefixline [, forcefile ] ] ])
47 print_info() {
48         local NEWLINE=1
49         local FORCEFILE=0
50         local PREFIXLINE=1
51         local SCRPRINT=0
52         local STR=''
53
54         # NOT ENOUGH ARGS
55         if [ "$#" -lt '2' ] ; then return 1; fi
56
57         # IF 3 OR MORE ARGS, CHECK IF WE WANT A NEWLINE AFTER PRINT
58         if [ "$#" -gt '2' ]
59         then
60                 if isTrue "$3"
61                 then
62                         NEWLINE='1';
63                 else
64                         NEWLINE='0';
65                 fi
66         fi
67
68         # IF 4 OR MORE ARGS, CHECK IF WE WANT TO PREFIX WITH A *
69         if [ "$#" -gt '3' ]
70         then
71                 if isTrue "$4"
72                 then
73                         PREFIXLINE='1'
74                 else
75                         PREFIXLINE='0'
76                 fi
77         fi
78
79         # IF 5 OR MORE ARGS, CHECK IF WE WANT TO FORCE OUTPUT TO DEBUG
80         # FILE EVEN IF IT DOESN'T MEET THE MINIMUM DEBUG REQS
81         if [ "$#" -gt '4' ]
82         then
83                 if isTrue "$5"
84                 then
85                         FORCEFILE='1'
86                 else
87                         FORCEFILE='0'
88                 fi
89         fi
90
91         # PRINT TO SCREEN ONLY IF PASSED DEBUGLEVEL IS HIGHER THAN
92         # OR EQUAL TO SET DEBUG LEVEL
93         if [ "$1" -lt "${DEBUGLEVEL}" -o "$1" -eq "${DEBUGLEVEL}" ]
94         then
95                 SCRPRINT='1'
96         fi
97
98         # RETURN IF NOT OUTPUTTING ANYWHERE
99         if [ "${SCRPRINT}" != '1' -a "${FORCEFILE}" != '1' ]
100         then
101                 return 0
102         fi
103
104         # STRUCTURE DATA TO BE OUTPUT TO SCREEN, AND OUTPUT IT
105         if [ "${SCRPRINT}" -eq '1' ]
106         then
107                 if [ "${PREFIXLINE}" = '1' ]
108                 then
109                         STR="${GOOD}*${NORMAL} ${2}"
110                 else
111                         STR="${2}"
112                 fi
113
114                 if [ "${NEWLINE}" = '0' ]
115                 then
116                         echo -ne "${STR}"
117                 else
118                         echo "${STR}"
119                 fi
120         fi
121
122         # STRUCTURE DATA TO BE OUTPUT TO FILE, AND OUTPUT IT
123         if [ "${SCRPRINT}" -eq '1' -o "${FORCEFILE}" -eq '1' ]
124         then
125                 STRR=${2//${WARN}/}
126                 STRR=${STRR//${BAD}/}
127                 STRR=${STRR//${BOLD}/}
128                 STRR=${STRR//${NORMAL}/}
129
130                 if [ "${PREFIXLINE}" = '1' ]
131                 then
132                         STR="* ${STRR}"
133                 else
134                         STR="${STRR}"
135                 fi
136
137                 if [ "${NEWLINE}" = '0' ]
138                 then
139                         if [ "${TODEBUGCACHE}" -eq 1 ]; then
140                                 DEBUGCACHE="${DEBUGCACHE}${STR}"
141                         else
142                                 echo -ne "${STR}" >> ${DEBUGFILE}
143                         fi      
144                 else
145                         if [ "${TODEBUGCACHE}" -eq 1 ]; then
146                                 DEBUGCACHE="${DEBUGCACHE}${STR}"$'\n'
147                         else
148                                 echo "${STR}" >> ${DEBUGFILE}
149                         fi
150                 fi
151         fi
152
153         return 0
154 }
155
156 print_error()
157 {
158         GOOD=${BAD} print_info "$@"
159 }
160
161 print_warning()
162 {
163         GOOD=${WARN} print_info "$@"
164 }
165
166 # var_replace(var_name, var_value, string)
167 # $1 = variable name
168 # $2 = variable value
169 # $3 = string
170
171 var_replace()
172 {
173   # Escape '\' and '.' in $2 to make it safe to use
174   # in the later sed expression
175   local SAFE_VAR
176   SAFE_VAR=`echo "${2}" | sed -e 's/\([\/\.]\)/\\\\\\1/g'`
177   
178   echo "${3}" | sed -e "s/%%${1}%%/${SAFE_VAR}/g" -
179 }
180
181 arch_replace() {
182   var_replace "ARCH" "${ARCH}" "${1}"
183 }
184
185 cache_replace() {
186   var_replace "CACHE" "${CACHE_DIR}" "${1}"
187 }
188
189 clear_log() {
190         [ -f "${DEBUGFILE}" ] && echo > "${DEBUGFILE}"
191 }
192
193 gen_die() {
194         dump_debugcache
195
196         if [ "$#" -gt '0' ]
197         then
198                 print_error 1 "ERROR: ${1}"
199         fi
200         echo
201         print_info 1 "-- Grepping log... --"
202         echo
203
204         if isTrue ${USECOLOR}
205         then
206                 GREP_COLOR='1' grep -B5 -E --colour=always "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
207         else
208                 grep -B5 -E "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
209         fi
210         echo
211         print_info 1 "-- End log... --"
212         echo
213         print_info 1 "Please consult ${DEBUGFILE} for more information and any"
214         print_info 1 "errors that were reported above."
215         echo
216         print_info 1 "Report any genkernel bugs to bugs.gentoo.org and"
217         print_info 1 "assign your bug to genkernel@gentoo.org. Please include"
218         print_info 1 "as much information as you can in your bug report; attaching"
219         print_info 1 "${DEBUGFILE} so that your issue can be dealt with effectively."
220         print_info 1 ''
221         print_info 1 'Please do *not* report compilation failures as genkernel bugs!'
222         print_info 1 ''
223         exit 1
224 }
225
226 has_loop() {
227         dmesg | egrep -q '^loop:'
228         if [ -e '/dev/loop0' -o -e '/dev/loop/0' -a ${PIPESTATUS[1]} ]
229         then
230                 # We found devfs or standard dev loop device, assume
231                 # loop is compiled into the kernel or the module is loaded
232                 return 0
233         else
234                 return 1
235         fi
236 }
237
238 isBootRO()
239 {
240         for mo in `grep ' /boot ' /proc/mounts | cut -d ' ' -f 4 | sed -e 's/,/ /'`
241         do
242                 if [ "x${mo}x" == "xrox" ]
243                 then
244                         return 0
245                 fi
246         done
247         return 1
248 }