# vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=8 ft=sh
# Filename:      /etc/grml/script-functions
# Purpose:       some often used functions for use in shellscripts
# 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.
################################################################################

# shellcheck disable=SC2250,SC2312

# {{{ set default PATH
# shellcheck disable=SC2086
setpath() {
  export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
}
# }}}

# {{{ check for root-permissions
check4root() {
  if [ "$(id -u 2>/dev/null)" != 0 ]; then
    echo 1>&2 "Error: please run this script with uid 0 (root)."
    return 1
  fi
}
# }}}

# {{{ check for user permissions
check4user() {
  if [ "$(id -u 2>/dev/null)" = 0 ]; then
    echo 1>&2 "Error: please do not run this script with uid 0 (root)."
    return 1
  fi
}
# }}}

# {{{ check for running zsh
iszsh() {
  if [ -n "${ZSH_VERSION}" ]; then
    return 0
  else
    return 1
  fi
}
# }}}

# # {{{ check for (X)dialog
# setdialog() {
#   if [ -n "$DISPLAY" ]; then
#     [ -x /usr/bin/Xdialog ] \
#       && DIALOG="Xdialog" \
#       && export XDIALOG_HIGH_DIALOG_COMPAT=1
#   else
#     [ -x /usr/bin/dialog ] \
#       && DIALOG='dialog' \
#       || (
#         echo 1>&2 "dialog not available"
#         return 1
#       )
#   fi
# }
# # }}}

# # {{{ check for availability of program(s)
# # 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
# }
# # }}}

# {{{ simple shell grep
stringinfile() {
  case "$(cat $2)" in
    *"$1"*) return 0 ;;
  esac
  return 1
}
# }}}

# {{{ simple shell grep for strings
stringinstring() {
  case "$2" in
    *"$1"*) return 0 ;;
  esac
  return 1
}
# }}}

# {{{ reread boot command line; echo last parameter's argument or return false.
getbootparam() {
  CMDLINE=$(cat /proc/cmdline)
  stringinstring " $1=" "$CMDLINE" || return 1
  result="${CMDLINE##*$1=}"
  result="${result%%[   ]*}"
  echo "$result"
  return 0
}
# }}}

# {{{ check boot commandline for specified option
checkbootparam() {
  stringinfile " $1" /proc/cmdline
  return "$?"
}
# }}}

# {{{ check whether $1 is yes
checkvalue() {
  if [ "$1" = "yes" -o "$1" = "YES" ]; then
    return 0
  else
    return 1
  fi
}
# }}}

# {{{ grml specific checks
isgrml() {
  [ -f /etc/grml_version ] && return 0 || return 1
}

grmlversion() {
  cat /etc/grml_version
}

isgrmlcd() {
  [ -f /etc/grml_cd ] && return 0 || return 1
}

isgrmlhd() {
  [ -f /etc/grml_cd ] && return 1 || return 0
}

checkgrmlsmall() {
  grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
}
# }}}

# {{{ filesystems (proc, pts, sys)
mount_proc() {
  check4root || return 1
  [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
}

mount_pts() {
  check4root || return 1
  stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
}

mount_sys() {
  check4root || return 1
  [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
}
# }}}

# char *reverse_list(list) {{{
#
#   Returns the reversed order of list
#
reverse_list() {
  local ret
  ret=''
  while [ "$#" -gt 0 ]; do
    if [ -z "${ret}" ]; then
      ret="$1"
    else
      ret="$1 ${ret}"
    fi
    shift
  done
  printf '%s' "${ret}"
}
#}}}

# bool is_older_than(reference, files/dirs to check) {{{
#
#   return 0 if any of the files/dirs are newer than
#   the reference file
#
#   EXAMPLE: if is_older_than a.out *.o; then ...
is_older_than() {
  local x
  local ref="$1"
  shift

  for x in "$@"; do
    [ "${x}" -nt "${ref}" ] && return 0

    if [ -d "${x}" ]; then
      is_older_than "${ref}" "${x}"/* && return 0
    fi
  done

  return 1
}
#}}}

## END OF FILE #################################################################
