#/bin/bash

# Copyright 2012-2016 Intel Corporation All Rights Reserved.
#
# The source code contained or described herein and all documents
# related to the source code ("Material") are owned by Intel
# Corporation or its suppliers or licensors. Title to the Material
# remains with Intel Corporation or its suppliers and licensors. The
# Material may contain trade secrets and proprietary and confidential
# information of Intel Corporation and its suppliers and licensors,
# and is protected by worldwide copyright and trade secret laws and
# treaty provisions. No part of the Material may be used, copied,
# reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel's prior express
# written permission.
#
# No license under any patent, copyright, trade secret or other
# intellectual property right is granted to or conferred upon you by
# disclosure or delivery of the Materials, either expressly, by
# implication, inducement, estoppel or otherwise. Any license under
# such intellectual property rights must be express and approved by
# Intel in writing.
#
# Unless otherwise agreed by Intel in writing, you may not remove or
# alter this notice or any other notice embedded in Materials by Intel
# or Intel's suppliers or licensors in any way.

#
# clckd   This shell script takes care of starting and stopping the \
#         Intel(R) Cluster Checker daemon
#
# chkconfig: 2345 99 01
# config: /opt/ohpc/admin/clck/3.1.2/etc/clckd.xml
# description: This shell script takes care of starting and stopping the \
#              Intel(R) Cluster Checker daemon
# processname: clckd

### BEGIN INIT INFO
# Provides: clckd
# Short-Description: start and stop the Intel(R) Cluster Checker daemon
# Description: Intel(R) Cluster Checker daemon runs various providers \
#              periodically to gather data on the health of the node
### END INIT INFO

bin=/opt/ohpc/admin/clck/3.1.2/sbin/mic/clckd
prog=$(basename $bin)
config=/opt/ohpc/admin/clck/3.1.2/etc/clckd.xml
user=clck

# Allow anyone to run status
if [ "$1" = "status" ]  ; then
    if [ `pidof ${prog}` ]; then
        echo "${prog} (pid `pidof ${prog}`) is running ..."
        RETVAL=$?
        exit $RETVAL
    else
        echo "${prog} is stopped"
        RETVAL=$?
        exit $RETVAL
    fi
fi

# Check that we are root to do anything else.
if [ `/usr/bin/id -u` -ne 0 ]; then
  echo "[ ERROR ] Only root can start, stop or send signals to ${prog}"
  exit 4
fi

start()
{
  # Check the daemon is not already running and start the daemon.
  if [ `pidof ${prog}` ]; then
      echo "${prog} (pid `pidof ${prog}`) is running ..."
      RETVAL=$?
      return ${RETVAL}
  else
    echo -n "Starting ${prog}"
    echo
    start-stop-daemon -S -u ${user} -x ${bin} -- -c ${config} -n 
    echo
  fi
  RETVAL=${?}
  return ${RETVAL}
}

stop()
{
  # Check the daemon is running adn stop the daemon.
  echo -n "Stopping ${prog} ... "
  if [ `pidof ${prog}` ] ; then
    start-stop-daemon -K -x ${bin}
  else
    echo -n "No running instance of ${prog} found."
fi
  echo

}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  sleep)
    start-stop-daemon -K -s USR1 -x ${bin} 
    ;;
  wakeup)
    start-stop-daemon -K -s USR2 -x ${bin}
    ;;
  status)
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|sleep|wakeup}"
    exit 1
    ;;
esac
