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