7143bef3a6ec84c873cf67b9fa7d2a9be7b8c02b
[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 kv_replace() {
186   var_replace "KV" "${KV}" "${1}"
187 }
188
189 cache_replace() {
190   var_replace "CACHE" "${CACHE_DIR}" "${1}"
191 }
192
193 clear_log() {
194     if [ -f "${DEBUGFILE}" ]
195     then
196         (echo > "${DEBUGFILE}") 2>/dev/null || small_die "Genkernel: Could not write to ${DEBUGFILE}."
197     fi   
198 }
199
200 gen_die() {
201         dump_debugcache
202
203         if [ "$#" -gt '0' ]
204         then
205                 print_error 1 "ERROR: ${1}"
206         fi
207         echo
208         print_info 1 "-- Grepping log... --"
209         echo
210
211         if isTrue ${USECOLOR}
212         then
213                 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}
214         else
215                 grep -B5 -E "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
216         fi
217         echo
218         print_info 1 "-- End log... --"
219         echo
220         print_info 1 "Please consult ${DEBUGFILE} for more information and any"
221         print_info 1 "errors that were reported above."
222         echo
223         print_info 1 "Report any genkernel bugs to bugs.gentoo.org and"
224         print_info 1 "assign your bug to genkernel@gentoo.org. Please include"
225         print_info 1 "as much information as you can in your bug report; attaching"
226         print_info 1 "${DEBUGFILE} so that your issue can be dealt with effectively."
227         print_info 1 ''
228         print_info 1 'Please do *not* report compilation failures as genkernel bugs!'
229         print_info 1 ''
230
231         # Cleanup temp dirs and caches if requested
232         cleanup
233         exit 1
234 }
235
236 has_loop() {
237         if [ -e /var/log/dmesg ]
238         then
239                 cat /var/log/dmesg | egrep -q '^loop:'
240         else
241                 dmesg | egrep -q '^loop:'
242         fi
243         if [ -e '/dev/loop0' -o -e '/dev/loop/0' -a ${PIPESTATUS[1]} ]
244         then
245                 # We found devfs or standard dev loop device, assume
246                 # loop is compiled into the kernel or the module is loaded
247                 return 0
248         else
249                 return 1
250         fi
251 }
252
253 isBootRO()
254 {
255         for mo in `grep ' ${BOOTDIR} ' /proc/mounts | cut -d ' ' -f 4 | sed -e 's/,/ /'`
256         do
257                 if [ "x${mo}x" == "xrox" ]
258                 then
259                         return 0
260                 fi
261         done
262         return 1
263 }
264
265 setup_cache_dir()
266 {
267
268 [ ! -d "${CACHE_DIR}" ] && mkdir -p "${CACHE_DIR}"
269
270 if [ "${CLEAR_CACHE_DIR}" == 'yes' ]
271 then
272         print_info 1 "Clearing cache dir contents from ${CACHE_DIR}"
273         CACHE_DIR_CONTENTS=`ls ${CACHE_DIR}|grep -v CVS|grep -v cpio|grep -v README`
274         
275         for i in ${CACHE_DIR_CONTENTS}
276         do
277                 print_info 1 "   >> removing ${i}"
278                 rm ${CACHE_DIR}/${i}
279         done
280 fi
281
282 }
283
284 clear_tmpdir()
285 {
286 if ! isTrue ${CMD_NOINSTALL}
287 then
288     TMPDIR_CONTENTS=`ls ${TMPDIR}`
289     print_info 1 "Removing tmp dir contents"
290     for i in ${TMPDIR_CONTENTS}
291     do
292         print_info 1 "   >> removing ${i}"
293         rm ${TMPDIR}/${i}
294     done
295 fi
296 }