#!/bin/bash
#
# Init file for MDSD
#
# chkconfig: 2345 70 70
# description: Azure MDS daemon
#

### BEGIN INIT INFO
# Provides:             mdsd
# Required-Start:       $remote_fs
# Required-Stop:        $remote_fs
# Default-Start:        2 3 4 5
# Default-Stop:
# Short-Description:    Azure MDS daemon
### END INIT INFO


# source function library
if [ -f /etc/init.d/functions ]; then
    INIT_STYLE=R      # INIT_STYLE uses R/S/D for its representative platform RedHat/SuSE/Debian/Microsoft
    . /etc/init.d/functions
elif [ -f /etc/rc.status ]; then
    INIT_STYLE=S
    . /etc/rc.status && rc_reset
elif [ -f /lib/lsb/init-functions ]; then
    INIT_STYLE=D
    . /lib/lsb/init-functions
else
    grep -i mariner /etc/os-release > /dev/null
    if [ $? = 0 ]; then
       # This is Microsoft Mariner distro
       INIT_STYLE=M
    else
       echo -n "Could not source init functions."
       exit 1
    fi
fi

RETVAL=0
DESC="Azure MDS Daemon"
NAME=mdsd

MDSD_BIN=/usr/sbin/mdsd
MDSD_OPTIONS="-d"

# Exit if the package is not installed
[ -x "$MDSD_BIN" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

MDSD_PIDFILE=${MDSD_ROLE_PREFIX}.pid

start()
{
  case $INIT_STYLE in
    D)
      log_begin_msg "Starting $DESC: "
      start-stop-daemon --start --pidfile $MDSD_PIDFILE --exec $MDSD_BIN -- $MDSD_OPTIONS
      RETVAL=$?
      log_end_msg $RETVAL
      ;;
    R)
      echo -n "Starting $DESC: "
      daemon $MDSD_BIN $MDSD_OPTIONS
      RETVAL=$?
      echo
      ;;
    S)
      echo -n "Starting $DESC: "
      startproc -p $MDSD_PIDFILE $MDSD_BIN $MDSD_OPTIONS
      rc_status -v
      ;;
    M)
      echo -n "Starting $DESC: "
      $MDSD_BIN $MDSD_OPTIONS
      RETVAL=$?
      echo
      ;;
    *)
      exit 1
      ;;
  esac
}

cleanup_mdsd()
{
  if [ -e $MDSD_PIDFILE ]; then
    mdsd_pid=$(cat $MDSD_PIDFILE)
    [ -z $mdsd_pid ] && echo "Pidfile was empty: $MDSD_PIDFILE" && return
    [ -z "$(ps -p $mdsd_pid -o pid=)" ] && echo "Process with pid $mdsd_pid does not exist" && return
    [ "$(ps -p $mdsd_pid -o comm=)" != "mdsd" ] && echo "Process with pid $mdsd_pid is not mdsd" && return

    echo "Force killing mdsd process: $mdsd_pid ..."
    kill -9 $mdsd_pid
    [ $? != 0 ] && echo "Could not send SIGTERM to process $mdsd_pid" >&2
  fi
}

stop()
{
  case $INIT_STYLE in
    D)
      log_begin_msg "Stopping $DESC: "
      start-stop-daemon --stop --retry=TERM/30/KILL/15 --remove-pidfile --pidfile $MDSD_PIDFILE --name $NAME
      RETVAL=$?
      log_end_msg $RETVAL
      [ $RETVAL != 0 ] && cleanup_mdsd
      ;;
    R)
      echo -n "Stopping $DESC: "
      killproc -p $MDSD_PIDFILE $MDSD_BIN
      RETVAL=$?
      [ $RETVAL != 0 ] && cleanup_mdsd
      echo
      ;;
    S)
      echo -n "Stopping $DESC: "
      killproc -p $MDSD_PIDFILE $MDSD_BIN
      rc_status -v
      ;;
    M)
      echo -n "Stopping $DESC: "
      kill -9 $(cat /var/run/mdsd/default.pid)
      RETVAL=$?
      [ $RETVAL != 0 ] && cleanup_mdsd
      echo
      ;;
    *)
      exit 1
      ;;
  esac
}

status()
{
  echo -n "Checking $DESC: "
  case $INIT_STYLE in
    D)
      status_of_proc $MDSD_BIN
      RETVAL=$?
      ;;
    R)
      if [ -f $MDSD_PIDFILE ]; then
        PID=`cat $MDSD_PIDFILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
          echo "Process dead but pidfile exists"
          RETVAL=3
        else
          echo "running"
          RETVAL=0
        fi
      else
        echo "stopped"
        RETVAL=3
      fi
      ;;
    S)
      checkproc -p $MDSD_PIDFILE $MDSD_BIN
      rc_status -v
      ;;
    M)
      systemctl status $NAME
      RETVAL=$?
      ;;
    *)
      RETVAL=1
      ;;
  esac
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    reload)
        ;;
    report)
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
esac
exit $RETVAL
