# /lib/lsb/init-functions - shell functions and variables for init scripts

#
# This sets some environment variables that init scripts can use
# to determine if we're running in a container
#
# Most likely just checking for the presence of $container would
# be enough
#

if [ -r /proc/1/environ ]; then
  chkcontainer=$(sed 's/\x0/\n/g' < /proc/1/environ|grep -ia '^container\|^LXC')
fi

[ -n "$chkcontainer" ] && export $chkcontainer
[ -z "$NAME" ] && NAME=${0##*/}

COL52="\\033[52G"

# killproc() [-p pidfile] pathname [signal]
# send either the specified [signal] or SIGTERM to a process

killproc() {
  local pidfile pathname pids
  local signal="-TERM"

  while true; do
    case "$1" in
      -p)
        pidfile="$2"
        shift 2
        ;;
      *)
        pathname="$1"
        [ -n "$2" ] && signal="$2"
        break
        ;;
    esac
  done

  [ -r "$pidfile" ] && pids=$(pidofproc -p "$pidfile" "$pathname") || return 0
  [ -z "$pids" ] && pids=$(pidofproc "$pathname")
  [ -z "$pids" ] && [ -n "$signal" ] && return 1
  [ -z "$pids" ] && return 0

  for pid in $pids; do
    kill -0 "$pid" 2>/dev/null || continue
    kill "$signal" "$pid" 2>/dev/null || continue

    for i in {1..10}; do
      sleep 0.1

      kill -0 "$pid" 2>/dev/null || break
    done

    if [ -z "$signal" ]; then
      kill -0 "$pid" 2>/dev/null || return 0
      kill -KILL "$pid"
    fi
  done

  return $?
}

log_init_msg() {
  [ -z "$1" ] && return 1

  echo -n "$1"
}

log_begin_msg() {
  log_init_msg "$1"
}

log_end_msg() {
  if [ "$1" = "0" ]; then
    log_success_msg
  else
    log_failure_msg
  fi
}

log_failure_msg() {
  echo "$COL52[ FAIL ]"

  return
}

log_success_msg() {
  echo "$COL52[  OK  ]"

  return
}

log_warning_msg() {
  echo "$COL52[ WARN ]"

  return
}

# pidofproc() [-p pidfile] pathname
# return one or more PIDs for the specified pathname

pidofproc() {
  local pidfile pathname pids pidlist

  while true; do
    case "$1" in
      -p)
        pidfile="$2"
        shift 2
        ;;
      *)
        pathname="$1"
        break
        ;;
    esac
  done

  if [ -r "$pidfile" ]; then
    pids=$(head -1 $pidfile|sed 's/ \+//g')
  else
    pids=$(pidof "$pathname")
  fi

  for pid in $pids; do
    kill -0 "$pid" 2>/dev/null && pidlist="$pidlist $pid "
  done

  [ -z "$pidlist" ] && return 1 || echo $pidlist && return 0
}

# start_daemon() [-f] [-n nicelevel] [-p pidfile] pathname [args...]
# start a daemon

start_daemon() {
  local force pidfile pathname args
  local nicelevel=0

  while true; do
    case "$1" in
      -f)
        force=1
        ;;
      -n)
        nicelevel="$2"
        shift 2
        ;;
      -p)
        pidfile="$2"
        shift 2
        ;;
      *)
        pathname="$1"
        shift 1
        args="$@"
        break
        ;;
    esac
  done

  if [ -n "$pidfile" ]; then
    pidofproc -p "$pidfile" "$pathname" >/dev/null && return 0

    [ -f "$pidfile" ] && rm "$pidfile"
  else
    pidofproc "$pathname" >/dev/null && return 0
  fi

  [ -n "$args" ] && nice -n "$nicelevel" "$pathname" "$args" || \
    nice -n "$nicelevel" "$pathname"
}

status_of_proc() {
  local name pidfile pathname pids pidlist

  while true; do
    case "$1" in
      -p)
        pidfile="$2"
        shift 2
        ;;
      *)
        pathname="$1"
        break
        ;;
    esac
  done

# Here we use LSB as a reference for the return values:
#
# 0		program is running or service is OK
# 1		program is dead and /var/run pid file exists
# 2		program is dead and /var/lock lock file exists
# 3		program is not running
# 4		program or service status is unknown
# 5-99		reserved for future LSB use
# 100-149	reserved for distribution use
# 150-199	reserved for application use
# 200-254	reserved
#
# As you can see however, for now we just support 0, 1, 3 and 4
# May do something for 2 at some point, but the other status
# codes should not be used here - they're only here for reference

  if [ -n "$pidfile" ]; then
    if [ -r "$pidfile" ]; then
      pid=$(pidofproc -p "$pidfile" "$pathname")

      if [ "$?" -eq 0 ]; then
        echo "$NAME running with PID: $pid" && return 0
      else
        echo "$NAME is not running" && return 1
      fi
    else
      pid=$(pidofproc "$pathname")

      if [ "$?" -eq 0 ]; then
        echo "Cannot read pid file ($pidfile)"
        echo "$NAME running with PID: $pid"
        return 4
      else
        echo "$NAME is not running"
        return 3
      fi
    fi
  else
    pid=$(pidofproc "$pathname")

    if [ "$?" -eq 0 ]; then
      echo "$NAME running with PID: $pid"
      return 0
    else
      echo "$NAME is not runing"
      return 3
    fi
  fi
  }
