lsb-functions: include support for grml_colors handling
[grml-etc-core.git] / etc / grml / lsb-functions
index 895e578..4dc8441 100644 (file)
@@ -1,5 +1,6 @@
 # lsb init-functions
-# vim:ft=sh:tw=80
+#
+# based on:
 # /lib/lsb/init-functions for Debian -*- shell-script -*-
 #
 # Copyright (c) 2002-03 Chris Lawrence
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+if [ "$(cat /proc/1/comm 2>/dev/null)" = "systemd" ] ; then
+  SYSTEMD=true
+else
+  SYSTEMD=false
+fi
+
+# log_*() functions {{{
 TPUT="${TPUT:-"/usr/bin/tput"}"
 
 _have_tput() {
@@ -123,12 +131,31 @@ log_end_msg() {
     fi
     return "$1"
 }
+# }}}
 
-# Copyright 1999-2005 Gentoo Foundation
-# Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier Exp $
+# e*() output functions {{{
+# heavily based on gentoo's functions.sh; stripped down and modified
+# to match our needs.
+#
+# defined functions:
+#   ebegin()
+#   eend()
+#   eerror()
+#   eindent()
+#   einfo()
+#   einfon()
+#   eoutdent()
+#   esetdent()
+#   esyslog()
+#   ewarn()
+#   ewend()
+#
+# copyright 1999-2005 gentoo foundation
+# distributed under the terms of the gnu general public license v2
+# $header: /var/cvsroot/gentoo-src/rc-scripts/sbin/functions.sh,v 1.81.2.6 2005/05/15 20:00:31 vapier exp $
 
-# Internal variables
+# initialisation {{{
+# internal variables
 
 # Dont output to stdout?
 RC_QUIET_STDOUT="no"
@@ -138,60 +165,78 @@ RC_INDENTATION=''
 RC_DEFAULT_INDENT=2
 #RC_DOT_PATTERN=' .'
 RC_DOT_PATTERN=''
+# dont output to stdout?
+rc_quiet_stdout="no"
+
+# default values for e-message indentation and dots
+rc_indentation=''
+rc_default_indent=2
+#rc_dot_pattern=' .'
+rc_dot_pattern=''
 
-# Should we use color?
+# should we use color?
 if [ -r /proc/cmdline ] ; then
-    grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
+  grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
 fi
 [ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
 RC_NOCOLOR="${RC_NOCOLOR:-no}"
+if [ "$RC_NOCOLOR" = "no" ] ; then
+  if [ -r /etc/grml_colors ] ; then
+    . /etc/grml_colors
+  fi
+fi
 
 # Can the terminal handle endcols?
-RC_ENDCOL="yes"
+if [ "${RC_NOCOLOR}" = "yes" ]; then
+  RC_ENDCOL="no"
+else
+  RC_ENDCOL="yes"
+fi
 
 # Setup COLS and ENDCOL so eend can line up the [ ok ]
 # width of [ ok ] == 7
 COLS="$(stty size 2>/dev/null | cut -d' ' -f2)"
 if [ -z "${COLS}" ] || [ "${COLS}" -le 0 ] ; then
-    COLS=80
+  COLS=80
 fi
 
 if [ "${RC_ENDCOL}" = "yes" ]; then
-       ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
+  ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
 else
-    ENDCOL=''
+  ENDCOL=''
 fi
 
 # Setup the colors so our messages all look pretty
 if [ "${RC_NOCOLOR}" = "yes" ]; then
-    unset GOOD WARN BAD NORMAL HILITE BRACKET
+  unset GOOD WARN BAD NORMAL HILITE BRACKET
 else
-    GOOD='\e[32;01m'
-    WARN='\e[33;01m'
-    BAD='\e[31;01m'
-    NORMAL='\e[0m'
-    HILITE='\e[36;01m'
-    BRACKET='\e[34;01m'
+  GOOD='\e[32;01m'
+  WARN='\e[33;01m'
+  BAD='\e[31;01m'
+  NORMAL='\e[0m'
+  HILITE='\e[36;01m'
+  BRACKET='\e[34;01m'
 fi
+#}}}
 
 # void esyslog(char* priority, char* tag, char* message)
 #
 #    use the system logger to log a message
 #
 esyslog() {
-    local pri
-    local tag
+  local pri
+  local tag
 
-    [ "$#" -le 2 ] && return 0
-    if [ -x /usr/bin/logger ] ; then
-        pri="$1"
-        tag="$2"
-        shift 2
+  [ "$#" -le 2 ] && return 0
+  if [ -x /usr/bin/logger ] ; then
+    pri="$1"
+    tag="$2"
+    shift 2
 
-        /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
-    fi
+    /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
+  fi
 
-    return 0
+  return 0
 }
 
 # void eindent(int num)
@@ -199,9 +244,9 @@ esyslog() {
 #    increase the indent used for e-commands.
 #
 eindent() {
-    local i="${1:-0}"
-    [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
-    esetdent $(( ${#RC_INDENTATION} + $i ))
+  local i="${1:-0}"
+  [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
+  esetdent $(( ${#RC_INDENTATION} + $i ))
 }
 
 # void eoutdent(int num)
@@ -209,9 +254,9 @@ eindent() {
 #    decrease the indent used for e-commands.
 #
 eoutdent() {
-    local i="${1:-0}"
-    [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
-    esetdent $(( ${#RC_INDENTATION} - $i ))
+  local i="${1:-0}"
+  [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
+  esetdent $(( ${#RC_INDENTATION} - $i ))
 }
 
 # void esetdent(int num)
@@ -220,9 +265,9 @@ eoutdent() {
 #    num defaults to 0
 #
 esetdent() {
-    local i="${1:-0}"
-    [ "$i" -lt 0 ] && i=0
-    RC_INDENTATION="$(printf "%${i}s" '')"
+  local i="${1:-0}"
+  [ "$i" -lt 0 ] && i=0
+  RC_INDENTATION="$(printf "%${i}s" '')"
 }
 
 # void einfo(char* message)
@@ -230,9 +275,9 @@ esetdent() {
 #    show an informative message (with a newline)
 #
 einfo() {
-    einfon "$*\n"
-    LAST_E_CMD=einfo
-    return 0
+  einfon "$*\n"
+  LAST_E_CMD=einfo
+  return 0
 }
 
 # void einfon(char* message)
@@ -240,11 +285,11 @@ einfo() {
 #    show an informative message (without a newline)
 #
 einfon() {
-    [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
-    [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
-    printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
-    LAST_E_CMD=einfon
-    return 0
+  [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
+  [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+  printf " ${GOOD}*${NORMAL} ${RC_INDENTATION}$*"
+  LAST_E_CMD=einfon
+  return 0
 }
 
 # void ewarn(char* message)
@@ -252,18 +297,18 @@ einfon() {
 #    show a warning message + log it
 #
 ewarn() {
-    if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
-        printf " $*\n"
-    else
-        [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
-        printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
-    fi
+  if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
+      printf " $*\n"
+  else
+    [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+    printf " ${WARN}*${NORMAL} ${RC_INDENTATION}$*\n"
+  fi
 
-    # Log warnings to system log
-    esyslog "daemon.warning" "rc-scripts" "$@"
+  # Log warnings to system log
+  esyslog "daemon.warning" "rc-scripts" "$@"
 
-    LAST_E_CMD=ewarn
-    return 0
+  LAST_E_CMD=ewarn
+  return 0
 }
 
 # void eerror(char* message)
@@ -271,18 +316,18 @@ ewarn() {
 #    show an error message + log it
 #
 eerror() {
-    if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
-        printf " $*\n" >/dev/stderr
-    else
-        [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
-        printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
-    fi
+  if [ "${RC_QUIET_STDOUT}" = "yes" ]; then
+    printf " $*\n" >&2
+  else
+    [ "${RC_ENDCOL}" != "yes" ] && [ "${LAST_E_CMD}" = "ebegin" ] && echo
+    printf " ${BAD}*${NORMAL} ${RC_INDENTATION}$*\n"
+  fi
 
-    # Log errors to system log
-    esyslog "daemon.err" "rc-scripts" "$@"
+  # Log errors to system log
+  esyslog "daemon.err" "rc-scripts" "$@"
 
-    LAST_E_CMD=eerror
-    return 0
+  LAST_E_CMD=eerror
+  return 0
 }
 
 # void ebegin(char* message)
@@ -290,25 +335,25 @@ eerror() {
 #    show a message indicating the start of a process
 #
 ebegin() {
-    local msg="$@" dots spaces
-    spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
-    [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
-
-    if [ -n "${RC_DOT_PATTERN}" ]; then
-        dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
-        while [ "${dots#${spaces}}" != "${dots}" ] ; do
-            dots="${dots#${spaces}}${RC_DOT_PATTERN}"
-        done
-        msg="${msg}${dots}"
-    else
-        msg="${msg} ..."
-    fi
-    einfon "${msg}"
-    [ "${RC_ENDCOL}" = "yes" ] && echo
-
-    LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
-    LAST_E_CMD=ebegin
-    return 0
+  local msg="$@" dots spaces
+  spaces="$(printf '%'"${#RC_DOT_PATTERN}"'s' '')"
+  [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
+
+  if [ -n "${RC_DOT_PATTERN}" ]; then
+    dots="$(printf "%$(( $COLS - 3 - ${#RC_INDENTATION} - ${#msg} - 7 ))s" '')"
+    while [ "${dots#${spaces}}" != "${dots}" ] ; do
+        dots="${dots#${spaces}}${RC_DOT_PATTERN}"
+    done
+    msg="${msg}${dots}"
+  else
+    msg="${msg} ..."
+  fi
+  einfon "${msg}"
+  [ "${RC_ENDCOL}" = "yes" ] && echo
+
+  LAST_E_LEN=$(( 3 + ${#RC_INDENTATION} + ${#msg} ))
+  LAST_E_CMD=ebegin
+  return 0
 }
 
 # void _eend(int error, char *efunc, char* errstr)
@@ -320,27 +365,27 @@ ebegin() {
 #    script.
 #
 _eend() {
-    local retval="${1:-0}" efunc="${2:-eerror}" msg
-    shift 2
+  local retval="${1:-0}" efunc="${2:-eerror}" msg
+  shift 2
 
-    if [ "${retval}" -eq 0 ]; then
-        [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
-        msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
-    else
-        if [ "$#" -gt 0 ] ; then
-            "${efunc}" "$@"
-        fi
-        msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
+  if [ "${retval}" -eq 0 ]; then
+    [ "${RC_QUIET_STDOUT}" = "yes" ] && return 0
+    msg="${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
+  else
+    if [ "$#" -gt 0 ] ; then
+        "${efunc}" "$@"
     fi
+    msg="${BRACKET}[ ${BAD}!!${BRACKET} ]${NORMAL}"
+  fi
 
-    if [ "${RC_ENDCOL}" = "yes" ]; then
-        printf "${ENDCOL}  ${msg}\n"
-    else
-        [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
-        printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
-    fi
+  if [ "${RC_ENDCOL}" = "yes" ]; then
+    printf "${ENDCOL}  ${msg}\n"
+  else
+    [ "${LAST_E_CMD}" = "ebegin" ] || LAST_E_LEN=0
+    printf "%$(( ${COLS} - ${LAST_E_LEN} - 6 ))s%b\n" '' "${msg}"
+  fi
 
-    return "${retval}"
+  return "${retval}"
 }
 
 # void eend(int error, char* errstr)
@@ -349,13 +394,13 @@ _eend() {
 #    if error, show errstr via eerror
 #
 eend() {
-    local retval="${1:-0}"
-    shift
+  local retval="${1:-0}"
+  shift
 
-    _eend "${retval}" eerror "$@"
+  _eend "${retval}" eerror "$@"
 
-    LAST_E_CMD=eend
-    return "$retval"
+  LAST_E_CMD=eend
+  return "$retval"
 }
 
 # void ewend(int error, char* errstr)
@@ -364,13 +409,37 @@ eend() {
 #    if error, show errstr via ewarn
 #
 ewend() {
-    local retval="${1:-0}"
-    shift
+  local retval="${1:-0}"
+  shift
 
-    _eend "${retval}" ewarn "$@"
+  _eend "${retval}" ewarn "$@"
 
-    LAST_E_CMD=ewend
-    return "$retval"
+  LAST_E_CMD=ewend
+  return "$retval"
 }
+#}}}
+
+# if we're using systemd then redfine functions for
+# output in systemd style
+if $SYSTEMD ; then
+  einfo() {
+    printf "[  ${GREEN}OK${NORMAL}  ] %s\n" "$*"
+  }
+
+  ewarn() {
+    printf "[ ${YELLOW}WARN${NORMAL} ] %s\n" "$*"
+  }
+
+  eerror() {
+    printf "[ ${RED}FAIL${NORMAL} ] %s\n" "$*"
+  }
+
+  eend() {
+    :
+  }
+fi
+
+# don't expose unneeded local variables
+unset SYSTEMD
 
-# vim:ts=4
+# vim: ft=sh tw=80 ts=4 foldmethod=marker