Magellan Linux

Contents of /mcore-src/trunk/mcore-tools/src/include/common.global.class.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2257 - (show annotations) (download)
Mon Jan 13 14:24:41 2014 UTC (10 years, 3 months ago) by niro
File size: 4273 byte(s)
-added include() function which sources files with debug information
1 # $Id$
2
3 # message only echo | disabled in quiet mode
4 mecho()
5 {
6 local COLCYAN="\033[1;36m"
7 local COLDEFAULT="\033[0m"
8 local opts
9 local webcrlf
10
11 # print nothing if quiet mode was requested
12 [[ ${QUIET} = true ]] && return
13
14 if [[ ${NOCOLORS} = true ]]
15 then
16 COLCYAN=""
17 COLDEFAULT=""
18 fi
19
20 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
21
22 # respect -n
23 case $1 in
24 -n) shift; opts="n" ;;
25 esac
26
27 echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
28 }
29
30 # prints error messages | enabled even in quiet mode
31 eecho()
32 {
33 local COLRED="\033[1;31m"
34 local COLDEFAULT="\033[0m"
35 local opts
36 local webcrlf
37
38 if [[ ${NOCOLORS} = true ]]
39 then
40 COLRED=""
41 COLDEFAULT=""
42 fi
43
44 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
45
46 # respect -n
47 case $1 in
48 -n) shift; opts="n" ;;
49 esac
50
51 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
52 }
53
54 # prints return values of get | enabled even in quiet mode
55 rvecho()
56 {
57 local COLPURPLE="\033[1;35m"
58 local COLDEFAULT="\033[0m"
59 local opts
60 local webcrlf
61
62 if [[ ${NOCOLORS} = true ]]
63 then
64 COLPURPLE=""
65 COLDEFAULT=""
66 fi
67
68 [[ ${WEBCRLF} = true ]] && webcrlf="<br>"
69
70 # respect -n
71 case $1 in
72 -n) shift; opts="n" ;;
73 esac
74
75 echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
76 }
77
78 # prints debug messages if requested | enabled even in quiet mode
79 decho()
80 {
81 # print nothing if debug mode was *not* requested
82 [[ ${DEBUG} != 1 ]] && return
83
84 eecho "DEBUG: ${@}"
85 }
86
87 # source a file with debug information
88 include()
89 {
90 local retval
91
92 if [ -f $@ ]
93 then
94 decho "including '$@'"
95 source $@
96 retval=$?
97 else
98 decho "include: '$@' not found"
99 retval=1
100 fi
101
102 return ${retval}
103 }
104
105 # adds a line to a configuration file defined by the $CONFIG variable
106 # $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"'
107 addconfig()
108 {
109 local opts
110
111 if [[ -z ${CONFIG} ]]
112 then
113 eecho "You must define \$CONFIG varibale first!"
114 return 1
115 fi
116
117 if [[ ! -d $(dirname ${CONFIG}) ]]
118 then
119 install -d $(dirname ${CONFIG})
120 fi
121
122 # check for opts
123 case $1 in
124 -n) shift; opts=" -n" ;;
125 -e) shift; opts=" -e" ;;
126 esac
127
128 echo ${opts} "$@" >> ${CONFIG}
129 }
130
131 # creates or clears a configuration file defined by the $CONFIG variable
132 # CONFIG="/etc/conf.d/mcore" clearconfig
133 clearconfig()
134 {
135 if [[ -z ${CONFIG} ]]
136 then
137 eecho "You must define \$CONFIG varibale first!"
138 return 1
139 fi
140
141 if [[ ! -d $(dirname ${CONFIG}) ]]
142 then
143 install -d $(dirname ${CONFIG})
144 fi
145 : > ${CONFIG}
146 }
147
148 # root is not allowed to run progs in a user session with newer xorg-servers
149 # this wrapper runs a command in the xsession of the unpriv_user
150 x11runas()
151 {
152 if [[ -n $(pidof X) ]]
153 then
154 su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
155 fi
156 }
157
158 # no_duplicate $list $item
159 no_duplicate()
160 {
161 local i
162 local list="$1"
163 local item="$2"
164
165 for i in ${list}
166 do
167 [[ ${i} = ${item} ]] && return 1
168 done
169
170 return 0
171 }
172
173 # checks if given path is empty
174 path_not_empty()
175 {
176 local path="$1"
177 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
178
179 # return ERR if path does not exist
180 [[ ! -d ${path} ]] && return 1
181 # return ERR if path empty
182 [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
183
184 # every thing went ok, directory not empty
185 return 0
186 }
187
188 # list all files in a given directory
189 list_files_in_directory()
190 {
191 local i
192 local retval
193 local path
194 local opts
195 local type
196
197 # basic getops
198 for i in $*
199 do
200 case $1 in
201 -mindepth) shift; opts+=" -mindepth $1" ;;
202 -maxdepth) shift; opts+=" -maxdepth $1" ;;
203 -type) shift; type="$1" ;;
204 -name) shift; opts+=" -name $1" ;;
205 '') continue ;;
206 *) path="$1" ;;
207 esac
208 shift
209 done
210
211 if [[ -z ${path} ]]
212 then
213 eecho "No path given."
214 return 1
215 fi
216
217 if [[ ! -d ${path} ]]
218 then
219 eecho "Directory '${path}' does not exist."
220 return 1
221 fi
222
223 # default to files
224 [[ -z ${type} ]] && type=f
225
226 for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort)
227 do
228 if [[ -z ${retval} ]]
229 then
230 retval="${i}"
231 else
232 retval+=" ${i}"
233 fi
234 done
235
236 rvecho "${retval}"
237 }
238
239 # runs a command in the chroot of $MROOT
240 system_chroot()
241 {
242 local cmd="$@"
243 if [[ -z ${MROOT} ]]
244 then
245 echo "system_chroot(): \$MROOT was not set, doing nothing!"
246 return 1
247 fi
248 if [ ! -d ${MROOT} ]
249 then
250 eecho "system_chroot(): MROOT='${MROOT}' does not exist."
251 return 1
252 fi
253
254 chroot ${MROOT} ${cmd}
255 }