/etc/grml/lsb-functions: another few bugfixes by ft (thanks)
[grml-etc-core.git] / etc / grml / lsb-functions
1 # lsb init-functions
2 # vim:ft=sh:tw=80
3 # /lib/lsb/init-functions for Debian -*- shell-script -*-
4 #
5 # Copyright (c) 2002-03 Chris Lawrence
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. Neither the name of the author nor the names of other contributors
17 #    may be used to endorse or promote products derived from this software
18 #    without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
31
32 TPUT="${TPUT:-"/usr/bin/tput"}"
33
34 _have_tput() {
35     [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
36         && return 0 \
37         || return 1
38 }
39
40 log_success_msg() {
41     printf " * $@\n"
42 }
43
44 log_failure_msg() {
45     if _have_tput ; then
46         RED="$("$TPUT" setaf 1)"
47         #NORMAL="$("$TPUT" op)"
48         printf " ${RED}*${NORMAL} $@\n"
49     else
50         printf " * $@\n"
51     fi
52 }
53
54 log_warning_msg() {
55     if _have_tput ; then
56         YELLOW="$("$TPUT" setaf 3)"
57         #NORMAL="$("$TPUT" op")"
58         # printf " *${NORMAL} $@\n"
59         printf " ${BLUE}*${NORMAL} $@\n"
60     else
61         printf " * $@\n"
62     fi
63 }
64
65 log_warning_msg_nn() {
66     if _have_tput ; then
67         YELLOW="$("$TPUT" setaf 3)"
68         printf " ${BLUE}*${NORMAL} $@"
69     else
70         printf " * $@"
71     fi
72 }
73
74 # int log_begin_message (char *message)
75 log_begin_msg() {
76         if [ "$#" -eq 0 ]; then
77                 return 1
78         fi
79         printf " ${GREEN}*${NORMAL} $@\n"
80 }
81
82 log_begin_msg_nn() {
83         if [ "$#" -eq 0 ]; then
84                 return 1
85         fi
86         printf " ${GREEN}*${NORMAL} $@"
87 }
88
89
90 SUBMSG="   ${GREEN}-${NORMAL} "
91
92 # int log_end_message (int exitstatus)
93 log_end_msg() {
94
95     # If no arguments were passed, return
96     [ "$#" -eq 0 ] && return 1
97
98     # Only do the fancy stuff if we have an appropriate terminal
99     # and if /usr is already mounted
100     if _have_tput ; then
101         COLS="$("$TPUT" cols)"
102         if [ -n "$COLS" ]; then
103             COL=$(( $COLS - 7 ))
104         else
105             COL=73
106         fi
107         UP="$("$TPUT" cuu1)"
108         END="$("$TPUT" hpa "$COL")"
109         START="$("$TPUT" hpa 0)"
110         #RED="$("$TPUT" setaf 1)"
111         #NORMAL="$("$TPUT" op)"
112         if [ "$1" -eq 0 ]; then
113             printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
114         else
115             printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
116         fi
117     else
118         if [ "$1" -eq 0 ]; then
119             printf "   ...done.\n"
120         else
121             printf "   ...fail!\n"
122         fi
123     fi
124     return "$1"
125 }
126
127 # Copyright 1999-2005 Gentoo Foundation
128 # Distributed under the terms of the GNU General Public License v2
129 # $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
130
131 # Internal variables
132
133 # Dont output to stdout?
134 RC_QUIET_STDOUT="no"
135
136 # Default values for e-message indentation and dots
137 RC_INDENTATION=''
138 RC_DEFAULT_INDENT=2
139 #RC_DOT_PATTERN=' .'
140 RC_DOT_PATTERN=''
141
142 # Should we use color?
143 if [ -r /proc/cmdline ] ; then
144     grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
145 fi
146 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
147 RC_NOCOLOR="${RC_NOCOLOR:-no}"
148
149 # Can the terminal handle endcols?
150 RC_ENDCOL="yes"
151
152 # Setup COLS and ENDCOL so eend can line up the [ ok ]
153 # width of [ ok ] == 7
154 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
155 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
156     COLS=80
157 fi
158
159 if [ "${RC_ENDCOL}" = "yes" ]; then
160     ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
161 else
162     ENDCOL=''
163 fi
164
165 # Setup the colors so our messages all look pretty
166 if [ "${RC_NOCOLOR}" = "yes" ]; then
167     unset GOOD WARN BAD NORMAL HILITE BRACKET
168 else
169     GOOD='\e[32;01m'
170     WARN='\e[33;01m'
171     BAD='\e[31;01m'
172     NORMAL='\e[0m'
173     HILITE='\e[36;01m'
174     BRACKET='\e[34;01m'
175 fi
176
177 # void esyslog(char* priority, char* tag, char* message)
178 #
179 #    use the system logger to log a message
180 #
181 esyslog() {
182     local pri
183     local tag
184
185     [ "$#" -le 2 ] && return 0
186     if [ -x /usr/bin/logger ] ; then
187         pri="$1"
188         tag="$2"
189         shift 2
190
191         /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
192     fi
193
194     return 0
195 }
196
197 # void eindent(int num)
198 #
199 #    increase the indent used for e-commands.
200 #
201 eindent() {
202     local i="${1:-0}"
203     [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
204     esetdent $(( ${#RC_INDENTATION} + $i ))
205 }
206
207 # void eoutdent(int num)
208 #
209 #    decrease the indent used for e-commands.
210 #
211 eoutdent() {
212     local i="${1:-0}"
213     [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
214     esetdent $(( ${#RC_INDENTATION} - $i ))
215 }
216
217 # void esetdent(int num)
218 #
219 #    hard set the indent used for e-commands.
220 #    num defaults to 0
221 #
222 esetdent() {
223     local i="${1:-0}"
224     [ "$i" -lt 0 ] && i=0
225     RC_INDENTATION="$(printf "%${i}s" '')"
226 }
227
228 # void einfo(char* message)
229 #
230 #    show an informative message (with a newline)
231 #
232 einfo() {
233     einfon "$*\n"
234     LAST_E_CMD=einfo
235     return 0
236 }
237
238 # void einfon(char* message)
239 #
240 #    show an informative message (without a newline)
241 #
242 einfon() {
243     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
244     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
245     printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
246     LAST_E_CMD=einfon
247     return 0
248 }
249
250 # void ewarn(char* message)
251 #
252 #    show a warning message + log it
253 #
254 ewarn() {
255     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
256         printf " $*\n"
257     else
258         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
259         printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
260     fi
261
262     # Log warnings to system log
263     esyslog "daemon.warning" "rc-scripts" "$@"
264
265     LAST_E_CMD=ewarn
266     return 0
267 }
268
269 # void eerror(char* message)
270 #
271 #    show an error message + log it
272 #
273 eerror() {
274     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
275         printf " $*\n" >/dev/stderr
276     else
277         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
278         printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
279     fi
280
281     # Log errors to system log
282     esyslog "daemon.err" "rc-scripts" "$@"
283
284     LAST_E_CMD=eerror
285     return 0
286 }
287
288 # void ebegin(char* message)
289 #
290 #    show a message indicating the start of a process
291 #
292 ebegin() {
293     local msg="$@" dots spaces
294     spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
295     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
296
297     if [ -n "${RC_DOT_PATTERN}" ]; then
298         dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
299         while [ "${dots#${spaces}}" != "${dots}" ] ; do
300             dots="${dots#${spaces}}${RC_DOT_PATTERN}"
301         done
302         msg="${msg}${dots}"
303     else
304         msg="${msg} ..."
305     fi
306     einfon "${msg}"
307     [ "${RC_ENDCOL}" = "yes" ] && echo
308
309     LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
310     LAST_E_CMD=ebegin
311     return 0
312 }
313
314 # void _eend(int error, char *efunc, char* errstr)
315 #
316 #    indicate the completion of process, called from eend/ewend
317 #    if error, show errstr via efunc
318 #
319 #    This function is private to functions.sh.  Do not call it from a
320 #    script.
321 #
322 _eend() {
323     local retval="${1:-0}" efunc="${2:-eerror}" msg
324     shift 2
325
326     if [ "${retval}" -eq 0 ]; then
327         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
328         msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
329     else
330         if [ "$#" -gt 0 ] ; then
331             "${efunc}" "$@"
332         fi
333         msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
334     fi
335
336     if [ "${RC_ENDCOL}" = "yes" ]; then
337         printf "${ENDCOL}  ${msg}\n"
338     else
339         [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
340         printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
341     fi
342
343     return "${retval}"
344 }
345
346 # void eend(int error, char* errstr)
347 #
348 #    indicate the completion of process
349 #    if error, show errstr via eerror
350 #
351 eend() {
352     local retval="${1:-0}"
353     shift
354
355     _eend "${retval}" eerror "$@"
356
357     LAST_E_CMD=eend
358     return "$retval"
359 }
360
361 # void ewend(int error, char* errstr)
362 #
363 #    indicate the completion of process
364 #    if error, show errstr via ewarn
365 #
366 ewend() {
367     local retval="${1:-0}"
368     shift
369
370     _eend "${retval}" ewarn "$@"
371
372     LAST_E_CMD=ewend
373     return "$retval"
374 }
375
376 # vim:ts=4