Moving e*() functions grml/lsb-functions to grml/script-functions
[grml-etc-core.git] / etc / grml / lsb-functions
1 # vim:ft=sh:tw=80:ts=4
2 # lsb init-functions
3 #
4 # based on:
5 # /lib/lsb/init-functions for Debian -*- shell-script -*-
6 #
7 # Copyright (c) 2002-03 Chris Lawrence
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 #    notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 #    notice, this list of conditions and the following disclaimer in the
17 #    documentation and/or other materials provided with the distribution.
18 # 3. Neither the name of the author nor the names of other contributors
19 #    may be used to endorse or promote products derived from this software
20 #    without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # SUCH DAMAGE.
33
34 TPUT="${TPUT:-"/usr/bin/tput"}"
35
36 _have_tput() {
37     [ -x "$TPUT" ] && "$TPUT" hpa 60 >/dev/null 2>&1 \
38         && return 0 \
39         || return 1
40 }
41
42 log_success_msg() {
43     printf " * $@\n"
44 }
45
46 log_failure_msg() {
47     if _have_tput ; then
48         RED="$("$TPUT" setaf 1)"
49         #NORMAL="$("$TPUT" op)"
50         printf " ${RED}*${NORMAL} $@\n"
51     else
52         printf " * $@\n"
53     fi
54 }
55
56 log_warning_msg() {
57     if _have_tput ; then
58         YELLOW="$("$TPUT" setaf 3)"
59         #NORMAL="$("$TPUT" op")"
60         # printf " *${NORMAL} $@\n"
61         printf " ${BLUE}*${NORMAL} $@\n"
62     else
63         printf " * $@\n"
64     fi
65 }
66
67 log_warning_msg_nn() {
68     if _have_tput ; then
69         YELLOW="$("$TPUT" setaf 3)"
70         printf " ${BLUE}*${NORMAL} $@"
71     else
72         printf " * $@"
73     fi
74 }
75
76 # int log_begin_message (char *message)
77 log_begin_msg() {
78         if [ "$#" -eq 0 ]; then
79                 return 1
80         fi
81         printf " ${GREEN}*${NORMAL} $@\n"
82 }
83
84 log_begin_msg_nn() {
85         if [ "$#" -eq 0 ]; then
86                 return 1
87         fi
88         printf " ${GREEN}*${NORMAL} $@"
89 }
90
91
92 SUBMSG="   ${GREEN}-${NORMAL} "
93
94 # int log_end_message (int exitstatus)
95 log_end_msg() {
96
97     # If no arguments were passed, return
98     [ "$#" -eq 0 ] && return 1
99
100     # Only do the fancy stuff if we have an appropriate terminal
101     # and if /usr is already mounted
102     if _have_tput ; then
103         COLS="$("$TPUT" cols)"
104         if [ -n "$COLS" ]; then
105             COL=$(( $COLS - 7 ))
106         else
107             COL=73
108         fi
109         UP="$("$TPUT" cuu1)"
110         END="$("$TPUT" hpa "$COL")"
111         START="$("$TPUT" hpa 0)"
112         #RED="$("$TPUT" setaf 1)"
113         #NORMAL="$("$TPUT" op)"
114         if [ "$1" -eq 0 ]; then
115             printf "${UP}${END}${BLUE}[ ${GREEN}ok ${BLUE}]${NORMAL}\n"
116         else
117             printf "${UP}${START} ${RED}*${NORMAL}${END}[${RED}fail${NORMAL}]\n"
118         fi
119     else
120         if [ "$1" -eq 0 ]; then
121             printf "   ...done.\n"
122         else
123             printf "   ...fail!\n"
124         fi
125     fi
126     return "$1"
127 }