#!/bin/bash

# Copyright 2012-2015 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.0.1/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
# Required-Start: 
# Should-Start
# Required-Stop: 
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# 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

# Source function library.
. /etc/rc.status
rc_reset

bin=/opt/ohpc/admin/clck/3.0.1/sbin/intel64/clckd
prog=$(basename $bin)
pidfile=/var/run/clck/clckd.pid
config=/opt/ohpc/admin/clck/3.0.1/etc/clckd.xml
user=clck

# Allow anyone to run status
if [ "$1" = "status" ] ; then
  echo -n "Checking status of ${prog}"
  checkproc -p ${pidfile} ${prog}
  rc_status -v
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

create_pidfile_location()
{
  if [ ! -d "$(dirname $pidfile)" ]; then
    mkdir -p "$(dirname $pidfile)"
  fi

  chmod 755 "$(dirname $pidfile)"

  chown $user "$(dirname $pidfile)" >/dev/null 2>/dev/null
  chgrp $user "$(dirname $pidfile)" >/dev/null 2>/dev/null
}

start()
{
  create_pidfile_location

  checkproc -p ${pidfile} ${bin}
  RETVAL=$?

  if [ $RETVAL -eq 0 ]; then
    echo "${bin} is already running"
  else 
    echo -n "Starting ${prog}"
    startproc -u ${user} -p ${pidfile} ${bin} -c ${config} -P ${pidfile} -n
    rc_status -v
  fi


  RETVAL=$?

  return ${RETVAL}
}

stop()
{
  echo -n "Stopping ${prog} ... "

  checkproc -p ${pidfile} ${bin}
  RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    killproc -p ${pidfile} "${prog}"
    rc_status -v
  else
    echo -n "No running instance of ${prog} found."
  fi
  echo

  rm -rf "${pidfile}"
}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  sleep)
    killproc -p ${pidfile} ${prog} -USR1
    ;;
  wakeup)
    killproc -p ${pidfile} ${prog} -USR2
    ;;
  status)
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|sleep|wakeup}"
    exit 1
    ;;
esac

