Merge remote-tracking branch 'origin/github/pr/104'
[grml-etc-core.git] / etc / grml / script-functions
index f3f6e20..4d6bcea 100644 (file)
@@ -3,7 +3,6 @@
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Tue, 23 Oct 2007 16:53:48 +0200 [ft]
 ################################################################################
 
 # {{{ set default PATH
@@ -49,15 +48,60 @@ setdialog(){
 # }}}
 
 # {{{ check for availability of program(s)
-check4progs(){
-  local RC=''
-  for arg in $* ; do
-    type -p $arg >/dev/null 2>&1 || RC="$arg"
-  done
-  if [ -n "$RC" ] ; then
-     echo "$RC not installed"
-     return 1
-  fi
+# usage example:
+#       check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]
+#
+# with option given either of:
+#       -s,-q,--quiet,--silent
+#
+# check for available progs but produce no output
+check4progs() {
+    [ -n "${ZSH_VERSION}" ] && emulate -L sh
+    local RTN=0
+    local oldifs="${IFS}"
+    local ARG d found
+    local VERBOSE=1
+
+    case ${1} in
+        -q | -s | --quiet | --silent)
+            VERBOSE=0
+            shift 1
+            ;;
+
+        *)
+            ;;
+    esac
+
+    while [ $# -gt 0 ]
+    do
+        ARG="$1"
+        shift
+
+        found=0
+        IFS=:
+        for d in $PATH
+        do
+            if [ -x "${d}/${ARG}" ]
+            then
+                found=1
+                break
+            fi
+        done
+        IFS="${oldifs}"
+
+        # check for availability
+        if [ ${found} -eq 0 ]
+        then
+            if [ ${VERBOSE} -eq 1 ]
+            then
+                printf "%s: binary not found\n" "${ARG}" >&2
+            fi
+            RTN=1
+        fi
+    done
+
+    # return non zero, if at least one prog is missing!
+    return $RTN
 }
 # }}}
 
@@ -77,8 +121,9 @@ stringinstring(){
 
 # {{{ reread boot command line; echo last parameter's argument or return false.
 getbootparam(){
-  stringinstring " $1=" /proc/cmdline || return 1
-  result="${/proc/cmdline##*$1=}"
+  CMDLINE=$(cat /proc/cmdline)
+  stringinstring " $1=" "$CMDLINE" || return 1
+  result="${CMDLINE##*$1=}"
   result="${result%%[   ]*}"
   echo "$result"
   return 0
@@ -87,7 +132,7 @@ getbootparam(){
 
 # {{{ check boot commandline for specified option
 checkbootparam(){
-  stringinstring " $1" /proc/cmdline
+  stringinfile " $1" /proc/cmdline
   return "$?"
 }
 # }}}
@@ -183,274 +228,5 @@ is_older_than() {
 }
 #}}}
 
-# 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 $
-
-# initialisation {{{
-# internal variables
-
-# 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?
-if [ -r /proc/cmdline ] ; then
-  grep -q ' nocolor' /proc/cmdline && RC_NOCOLOR='yes'
-fi
-[ -n "$NOCOLORS" ] && RC_NOCOLOR='yes'
-RC_NOCOLOR="${RC_NOCOLOR:-no}"
-
-# Can the terminal handle endcols?
-RC_ENDCOL="yes"
-
-# 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
-fi
-
-if [ "${RC_ENDCOL}" = "yes" ]; then
-  ENDCOL="\e[A\e[$(( ${COLS} - 8 ))G"
-else
-  ENDCOL=''
-fi
-
-# Setup the colors so our messages all look pretty
-if [ "${RC_NOCOLOR}" = "yes" ]; then
-  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'
-fi
-#}}}
-
-# void esyslog(char* priority, char* tag, char* message)
-#
-#    use the system logger to log a message
-#
-esyslog() {
-  local pri
-  local tag
-
-  [ "$#" -le 2 ] && return 0
-  if [ -x /usr/bin/logger ] ; then
-    pri="$1"
-    tag="$2"
-    shift 2
-
-    /usr/bin/logger -p "${pri}" -t "${tag}" -- "$@"
-  fi
-
-  return 0
-}
-
-# void eindent(int num)
-#
-#    increase the indent used for e-commands.
-#
-eindent() {
-  local i="${1:-0}"
-  [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
-  esetdent $(( ${#RC_INDENTATION} + $i ))
-}
-
-# void eoutdent(int num)
-#
-#    decrease the indent used for e-commands.
-#
-eoutdent() {
-  local i="${1:-0}"
-  [ "$i" -gt 0 ] || i="${RC_DEFAULT_INDENT}"
-  esetdent $(( ${#RC_INDENTATION} - $i ))
-}
-
-# void esetdent(int num)
-#
-#    hard set the indent used for e-commands.
-#    num defaults to 0
-#
-esetdent() {
-  local i="${1:-0}"
-  [ "$i" -lt 0 ] && i=0
-  RC_INDENTATION="$(printf "%${i}s" '')"
-}
-
-# void einfo(char* message)
-#
-#    show an informative message (with a newline)
-#
-einfo() {
-  einfon "$*\n"
-  LAST_E_CMD=einfo
-  return 0
-}
-
-# void einfon(char* message)
-#
-#    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
-}
-
-# void ewarn(char* message)
-#
-#    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
-
-  # Log warnings to system log
-  esyslog "daemon.warning" "rc-scripts" "$@"
-
-  LAST_E_CMD=ewarn
-  return 0
-}
-
-# void eerror(char* message)
-#
-#    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
-
-  # Log errors to system log
-  esyslog "daemon.err" "rc-scripts" "$@"
-
-  LAST_E_CMD=eerror
-  return 0
-}
-
-# void ebegin(char* message)
-#
-#    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
-}
-
-# void _eend(int error, char *efunc, char* errstr)
-#
-#    indicate the completion of process, called from eend/ewend
-#    if error, show errstr via efunc
-#
-#    This function is private to functions.sh.  Do not call it from a
-#    script.
-#
-_eend() {
-  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}"
-  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}"
-}
-
-# void eend(int error, char* errstr)
-#
-#    indicate the completion of process
-#    if error, show errstr via eerror
-#
-eend() {
-  local retval="${1:-0}"
-  shift
-
-  _eend "${retval}" eerror "$@"
-
-  LAST_E_CMD=eend
-  return "$retval"
-}
-
-# void ewend(int error, char* errstr)
-#
-#    indicate the completion of process
-#    if error, show errstr via ewarn
-#
-ewend() {
-  local retval="${1:-0}"
-  shift
-
-  _eend "${retval}" ewarn "$@"
-
-  LAST_E_CMD=ewend
-  return "$retval"
-}
-#}}}
-
 ## END OF FILE #################################################################
-# vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=2
+# vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=8 ft=sh