lsb-functions: removed add_suffix()
[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 RC_GOT_FUNCTIONS="yes"
132
133 # Different types of dependencies
134 deptypes="need use"
135 # Different types of order deps
136 ordtypes="before after"
137
138 #
139 # Internal variables
140 #
141
142 # Dont output to stdout?
143 RC_QUIET_STDOUT="no"
144 RC_VERBOSE="${RC_VERBOSE:-no}"
145
146 # Should we use color?
147 if [ -r /proc/cmdline ] ; then
148     grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
149 fi
150 RC_NOCOLOR="${RC_NOCOLOR:-no}"
151 # Can the terminal handle endcols?
152 RC_ENDCOL="yes"
153
154 #
155 # Default values for rc system
156 #
157 RC_TTY_NUMBER=11
158 RC_NET_STRICT_CHECKING="no"
159 RC_PARALLEL_STARTUP="no"
160 RC_USE_CONFIG_PROFILE="yes"
161
162 #
163 # Default values for e-message indentation and dots
164 #
165 RC_INDENTATION=''
166 RC_DEFAULT_INDENT=2
167 #RC_DOT_PATTERN=' .'
168 RC_DOT_PATTERN=''
169
170 # void esyslog(char* priority, char* tag, char* message)
171 #
172 #    use the system logger to log a message
173 #
174 esyslog() {
175     local pri
176     local tag
177
178     if [ -x /usr/bin/logger ]
179     then
180         pri="$1"
181         tag="$2"
182
183         shift 2
184         [ -z "$*" ] && return 0
185
186         /usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
187     fi
188
189     return 0
190 }
191
192 # void eindent(int num)
193 #
194 #    increase the indent used for e-commands.
195 #
196 eindent() {
197     local i=$1
198     (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
199     esetdent $(( ${#RC_INDENTATION} + i ))
200 }
201
202 # void eoutdent(int num)
203 #
204 #    decrease the indent used for e-commands.
205 #
206 eoutdent() {
207     local i=$1
208     (( i > 0 )) || (( i = RC_DEFAULT_INDENT ))
209     esetdent $(( ${#RC_INDENTATION} - i ))
210 }
211
212 # void esetdent(int num)
213 #
214 #    hard set the indent used for e-commands.
215 #    num defaults to 0
216 #
217 esetdent() {
218     local i=$1
219     (( i < 0 )) && (( i = 0 ))
220     RC_INDENTATION=$(printf "%${i}s" '')
221 }
222
223 # void einfo(char* message)
224 #
225 #    show an informative message (with a newline)
226 #
227 einfo() {
228     einfon "$*\n"
229     LAST_E_CMD=einfo
230     return 0
231 }
232
233 # void einfon(char* message)
234 #
235 #    show an informative message (without a newline)
236 #
237 einfon() {
238     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
239     [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
240     printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
241     LAST_E_CMD=einfon
242     return 0
243 }
244
245 # void ewarn(char* message)
246 #
247 #    show a warning message + log it
248 #
249 ewarn() {
250     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
251         printf " $*\n"
252     else
253         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
254         printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
255     fi
256
257     # Log warnings to system log
258     esyslog "daemon.warning" "rc-scripts" "$*"
259
260     LAST_E_CMD=ewarn
261     return 0
262 }
263
264 # void eerror(char* message)
265 #
266 #    show an error message + log it
267 #
268 eerror() {
269     if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
270         printf " $*\n" >/dev/stderr
271     else
272         [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
273         printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
274     fi
275
276     # Log errors to system log
277     esyslog "daemon.err" "rc-scripts" "$*"
278
279     LAST_E_CMD=eerror
280     return 0
281 }
282
283 # void ebegin(char* message)
284 #
285 #    show a message indicating the start of a process
286 #
287 ebegin() {
288     local msg="$*" dots spaces
289     spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
290     [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
291
292     if [ -n "${RC_DOT_PATTERN}" ]; then
293         dots=$(printf "%$(( COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')
294         while [ "${dots#${spaces}}" != "${dots}" ] ; do
295             dots="${dots#${spaces}}${RC_DOT_PATTERN}"
296         done
297         msg="${msg}${dots}"
298     else
299         msg="${msg} ..."
300     fi
301     einfon "${msg}"
302     [ "${RC_ENDCOL}" = "yes" ] && echo
303
304     LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
305     LAST_E_CMD=ebegin
306     return 0
307 }
308
309 # void _eend(int error, char *efunc, char* errstr)
310 #
311 #    indicate the completion of process, called from eend/ewend
312 #    if error, show errstr via efunc
313 #
314 #    This function is private to functions.sh.  Do not call it from a
315 #    script.
316 #
317 _eend() {
318     local retval=${1:-0} efunc=${2:-eerror} msg
319     shift 2
320
321     if [ "${retval}" -eq 0 ]; then
322         [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
323         msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
324     else
325         if [ -n "$*" ]; then
326             "${efunc}" "$*"
327         fi
328         msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
329     fi
330
331     if [ "${RC_ENDCOL}" = "yes" ]; then
332         printf "${ENDCOL}  ${msg}\n"
333     else
334         [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
335         printf "%$(( "${COLS}" - "${LAST_E_LEN}" - 6 ))s%b\n" '' "${msg}"
336     fi
337
338     return ${retval}
339 }
340
341 # void eend(int error, char* errstr)
342 #
343 #    indicate the completion of process
344 #    if error, show errstr via eerror
345 #
346 eend() {
347     local retval=${1:-0}
348     shift
349
350     _eend ${retval} eerror "$*"
351
352     LAST_E_CMD=eend
353     return $retval
354 }
355
356 # void ewend(int error, char* errstr)
357 #
358 #    indicate the completion of process
359 #    if error, show errstr via ewarn
360 #
361 ewend() {
362     local retval=${1:-0}
363     shift
364
365     _eend ${retval} ewarn "$*"
366
367     LAST_E_CMD=ewend
368     return $retval
369 }
370
371 # Setup a basic $PATH.  Just add system default to existing.
372 # This should solve both /sbin and /usr/sbin not present when
373 # doing 'su -c foo', or for something like:  PATH= rcscript start
374 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:${PATH}"
375
376 if [ "$(/sbin/consoletype 2> /dev/null)" = "serial" ] ; then
377     # We do not want colors/endcols on serial terminals
378     RC_NOCOLOR="yes"
379     RC_ENDCOL="no"
380 fi
381
382 for arg in "$@" ; do
383     case "${arg}" in
384         # Lastly check if the user disabled it with --nocolor argument
385         --nocolor|-nc)
386             RC_NOCOLOR="yes"
387             ;;
388     esac
389 done
390
391 # Setup COLS and ENDCOL so eend can line up the [ ok ]
392 # width of [ ok ] == 7
393 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
394 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
395     COLS=80
396 fi
397
398 if [ "${RC_ENDCOL}" = "yes" ]; then
399     ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
400 else
401     ENDCOL=''
402 fi
403
404 # Setup the colors so our messages all look pretty
405 if [ "${RC_NOCOLOR}" = "yes" ]; then
406     unset GOOD WARN BAD NORMAL HILITE BRACKET
407 else
408     GOOD='\e[32;01m'
409     WARN='\e[33;01m'
410     BAD='\e[31;01m'
411     NORMAL='\e[0m'
412     HILITE='\e[36;01m'
413     BRACKET='\e[34;01m'
414 fi
415
416 # vim:ts=4