# /etc/rc.status
# This file contains a functions to record, display, and return the actual
# rc status information in init scripts.
# See https://en.opensuse.org/openSUSE:Packaging_init_scripts#Status_Functions
# for more info.
#
# The following functions are provided:
#
#   rc_active
#     This function checks whether a service is enabled (by symlinks).
#     It returns "0" if the service is enabled in a runlevel and
#     returns "1" otherwise.
#
#   rc_exit
#     This function terminates an init script with the exit status
#     appropriate to the overall rc status.
#
#   rc_failed [num]
#     This function sets the local and the overall rc status to a selected
#     value defined by the parameter num. The value "1" is used by default.
#
#   rc_check
#     This function checks the exit status of the last command ($?) and sets
#     the local rc status to this value if the value is different from "0".
#     Then it sets the overall rc status to the value of the local rc status
#     if it differs from "0". This function is used internally by other rc
#     status functions.
#
#   rc_reset
#     This function sets both the local and the overall rc status to "0".
#
#   rc_status [-r] [-s] [-u] [-v[num]]
#     This function checks, sets, and displays the rc status. By default,
#     it is quiet: it only calls rc_check. So, it must be called with an
#     option to display the status. The options have the following meaning:
#       -r calls rc_reset. This option is usable together with -v. The command
#          rc_status -v -r checks, sets, and displays the current rc status.
#          Then rc_reset is called.
#       -s displays "skipped" and sets the status to "3". It means an
#          unimplemented feature.
#       -u displays "unused" and sets the status to "3". It means an
#          unimplemented feature.
#       -v[num] displays the actual status and resets local status to "0".
#          By default, the status is displayed on the actual line. The 
#          parameter num defines that it should display num lines above the
#          actual cursor position.

if test -z "$PREVENT_LOOP" && test $PPID -ne 1 && test $# -eq 1 ; then
  unit_name=
  case "$0" in
    /etc/init.d/*|/etc/rc.d/*)
    unit_name=${0##*/} ;;
  */rc*)
    unit_name=`readlink "$0"`
    unit_name=${unit_name##*/}
    ;;
  esac
  if test -n "$unit_name"; then
    if [ "$1" =  "status" ]; then
      PREVENT_LOOP=1 "$0" "$1"
      exec /usr/bin/systemctl -l status  "${unit_name}.service"
    else
      exec /usr/bin/systemctl --ignore-dependencies $1 "${unit_name}.service"
    fi
  fi
  unset unit_name
fi

# local rc status
rc_status=0

# overall rc status
rc_status_overall=0

# echo opt to enable interpretation of backslash escapes
# eopt="-e"

# output messages
msg_done="done"
msg_failed="failed"
msg_missed="missed"
msg_skipped="skipped"
msg_unused="unused"
msg_running="running"
msg_dead="dead"
msg_unknown="unknown"

# parent cmd
rc_action=$1

rc_active ()
{
  local file
  for file in /etc/rc*.d/S[0-9][0-9]$1 ; do
    test -h $file && return 0
  done
  return 1
}

rc_exit ()
{
  exit $rc_status_overall
}

rc_failed ()
{
  if [ -z "$1" ] ; then
    rc_status=1
  else
    rc_status=$1
  fi
  rc_status_overall=$rc_status
  return $rc_status
}

rc_check ()
{
  result=$?
  test $result -ne 0 && rc_status=$result
  test $rc_status -ne 0 && rc_status_overall=$rc_status
  return $result
}

rc_reset ()
{
  rc_status=0
  rc_status_overall=0
  return 0
}

if [ "$rc_action" = "status" ] ; then
rc_status ()
{
  rc_check
  ret=$rc_status
  local op
  for op ; do
    case "$op" in
      -v) case "$rc_status" in
            0) echo $eopt $msg_running ;;
            1|2) echo $eopt $msg_dead ;;
            3) echo $eopt $msg_unused ;;
            4) echo $eopt $msg_unknown ;;
          esac
          rc_status=0 ;;
      -r) rc_reset ;;
      -s) echo $eopt msg_skipped ; rc_failed 3 ;;
      -u) echo $eopt msg_unused ; rc_failed 3 ;;
      *) echo "rc_status: Usage [-v] [-r] [-s] [-u]" ;;
    esac
  done
  return $ret
}
elif [ -n "$rc_action" ] ; then
# "start", "stop", etc...
rc_status ()
{
  rc_check
  ret=$rc_status
  if [ "$rc_action" = "stop" -a $rc_status -eq 7 ] ; then
    # we've stopped the service: 7 -> 0
    rc_failed 7
  fi
  local op
  for op ; do
    case "$op" in
      -v) case "$rc_status" in
            0) echo $eopt $msg_done ;;
            3) echo $eopt $msg_missed ;;
            5) echo $eopt $msg_skipped ;;
            6) echo $eopt $msg_unused ;;
            *) echo $eopt $msg_failed ;;
          esac
          rc_status=0 ;;
      -r) rc_reset ;;
      -s) echo $eopt msg_skipped ; rc_failed 5 ;;
      -u) echo $eopt msg_unused ; rc_failed 6 ;;
      *) echo "rc_status: Usage [-v] [-r] [-s] [-u]" ;;
    esac
  done
  return $ret
}
else
rc_status ()
{
  rc_check
  ret=$rc_status
  local op
  for op ; do
    case "$op" in
      -v) case "$rc_status" in
            0) echo $eopt $msg_done ;;
            *) echo $eopt $msg_failed ;;
          esac
          rc_status=0 ;;
      -r) rc_reset ;;
      -s) echo $eopt msg_skipped ; rc_failed 0 ;;
      -u) echo $eopt msg_unused ; rc_failed 0 ;;
      *) echo "rc_status: Usage [-v] [-r] [-s] [-u]" ;;
    esac
  done
  return $ret
}
fi