#!/bin/sh
#
# by ymc-dabe
# A simple wrapper to common start-script in /etc/init.d, that allows
# to use those scripts, even they show behavior, that is problematic to
# heartbeat (e.g. no support for "status" or a non-zero exit-code during start).


ymcCommand=$5
USAGE="usage: $0 initScriptToCall {failed-start-okay|failed-start-not-okay} {failed-status-okay|failed-status-not} {failed-stop-okay|failed-not-okay} {start|stop|status}";

initScriptToCall=$1
allowFailuresString="$2 $3 $4"

usage()
{
  echo $USAGE >&2
}

if [ $# != 5 ]; then
  usage
  exit 1
fi

call_init_script()
{
  local whatToAsk=$1
  local exit_code=1

  if [ -x "/etc/init.d/$initScriptToCall" ]; then
    /etc/init.d/$initScriptToCall $1
    exit_code=$?
    if [ $exit_code -ne 0 ] && \
       [ $(echo $allowFailuresString | grep -c -F "failed-$whatToAsk-okay") -eq 1 ]; then
      exit_code=0
    fi
    exit $exit_code
  fi

  exit $exit_code
}


case $ymcCommand in
  start)        call_init_script start;;
  stop)         call_init_script stop;;
  status)       call_init_script status;;
  *)            usage
                exit 1
                ;;
esac
