9a49b0126f7a080fc6a550bb15af37eca224a2e5
[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   echo "${3}" | sed -e "s/%%${1}%%/${2}/g" -
174 }
175
176 arch_replace() {
177   var_replace "ARCH" "${ARCH}" "${1}"
178 }
179
180 clear_log() {
181         [ -f "${DEBUGFILE}" ] && echo > "${DEBUGFILE}"
182 }
183
184 gen_die() {
185         dump_debugcache
186
187         if [ "$#" -gt '0' ]
188         then
189                 print_error 1 "ERROR: ${1}"
190         fi
191         echo
192         print_info 1 "-- Grepping log... --"
193         echo
194
195         if isTrue ${USECOLOR}
196         then
197                 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}
198         else
199                 grep -B5 -E "([Ww][Aa][Rr][Nn][Ii][Nn][Gg]|[Ee][Rr][Rr][Oo][Rr][ :,!]|[Ff][Aa][Ii][Ll][Ee]?[Dd]?)" ${DEBUGFILE}
200         fi
201         echo
202         print_info 1 "-- End log... --"
203         echo
204         print_info 1 "Please consult ${DEBUGFILE} for more information and any"
205         print_info 1 "errors that were reported above."
206         echo
207         print_info 1 "Report any genkernel bugs to bugs.gentoo.org and"
208         print_info 1 "assign your bug to genkernel@gentoo.org. Please include"
209         print_info 1 "as much information as you can in your bug report; attaching"
210         print_info 1 "${DEBUGFILE} so that your issue can be dealt with effectively."
211         print_info 1 ''
212         print_info 1 'Please do *not* report compilation failures as genkernel bugs!'
213         print_info 1 ''
214         exit 1
215 }
216
217 has_loop() {
218         dmesg | egrep -q '^loop:'
219         if [ -e '/dev/loop0' -o -e '/dev/loop/0' -a $? ]
220         then
221                 # We found devfs or standard dev loop device, assume
222                 # loop is compiled into the kernel or the module is loaded
223                 return 0
224         else
225                 return 1
226         fi
227 }
228
229 isBootRO()
230 {
231         for mo in `grep ' /boot ' /proc/mounts | cut -d ' ' -f 4 | sed -e 's/,/ /'`
232         do
233                 if [ "x${mo}x" == "xrox" ]
234                 then
235                         return 0
236                 fi
237         done
238         return 1
239 }