#! /bin/bash
###
#
# chkconfig: 2345 20 80
# description:  Enables MapR hoststats
#
### BEGIN INIT INFO
# Provides:       mapr-hoststats
# Required-Start: $network
# Required-Stop:  $network
# Should-Start:
# Should-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Start MapR hoststas
### END INIT INFO


# Source function library.
if [ -r /etc/init.d/functions ]; then
    . /etc/init.d/functions
fi

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
if [ -r /lib/lsb/init-functions ]; then 
    . /lib/lsb/init-functions
fi

BASEMAPR=${MAPR_HOME:-/opt/mapr}

PATH=$PATH:/sbin:/usr/sbin:/bin:/usr/bin:$BASEMAPR/cldb/
PATH=$PATH:$BASEMAPR/lib
PATH=$PATH:$BASEMAPR/server

env=${BASEMAPR}/conf/env.sh
[ -f $env ] && . $env

DESC="hoststats daemon"
NAME=hoststats
DAEMON=${BASEMAPR}/server/hoststats
PIDFILE=${BASEMAPR}/pid/$NAME.pid
SCRIPTNAME=/etc/init.d/mapr-$NAME

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


. $BASEMAPR/server/initscripts-common.sh

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

do_start()
{
  LD_LIBRARY_PATH="$BASEMAPR/lib:$LD_LIBRARY_PATH"
  port_found=0

  file="${BASEMAPR}/conf/mfs.conf"
  HOSTSTATS_ARGS=""
  while read line;  do
    key=`echo $line | awk -F '=' '{print $1}'`
    value=`echo $line | awk -F '=' '{print $2}'`
    if [ "$key" = "mfs.server.port" ]; then
      HOSTSTATS_ARGS="$HOSTSTATS_ARGS $value"
      port_found=1
      break
    fi
  done < "$file";

  # Consider 5660 as default shm key
  if [ $port_found -eq 0 ]; then
    HOSTSTATS_ARGS="$HOSTSTATS_ARGS 5660"
  fi

  HOSTSTATS_RPC=""
  if [ "$RPC_ON"x != "x" -a "$RPC_ON" = "1" ]; then
     HOSTSTATS_RPC="-S 1"
  fi

  daemon --pidfile $PIDFILE $DAEMON $HOSTSTATS_ARGS ${BASEMAPR}/server/data/TaskTracker.stats $HOSTSTATS_RPC > /dev/null 2>&1 || return 1
  return $?
}


do_stop()
{
    #
    # REMINDER: If the parameters for killproc should
    # change in this function, then the parameters need
    # to be verified against SuSE in order to maintian
    # support.
    #
    local delay="-d 10"

    if [ -f "/etc/SuSE-release" ]; then
        delay="-t 10"
    fi

    #
    # killproc needs -p before -d !
    #
    if [ -f "${PIDFILE}" ]; then
        killproc -p ${PIDFILE} ${delay} ${NAME}
    else
        killproc ${delay} ${NAME}
    fi

    rm -f ${PIDFILE}

    return 0
}


do_exit()
{
  UnLockForSingleInstance;
  exit $1
}

if [ "$1" = "-h" ]; then
  echo "Usage: $SCRIPTNAME {start|stop|status|restart|}"
  exit 0
fi

LockForSingleInstance;

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

case "$1" in
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    status)
        status $NAME
        ;;
    restart)
        do_stop
        do_start
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|}" >&2
        do_exit 3
        ;;
esac
result=$?
do_exit $result
