#!/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 server 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 server daemon
# processname: clck-serverd

### BEGIN INIT INFO
# Provides: clck-serverd
# Short-Description: start and stop the Intel(R) Cluster Checker server daemon
# Description: Intel(R) Cluster Checker server daemon announces itself as the \
#              node responsible for aggregation of data, and accumulates data \
#              from the rest of the daemons and writes them to a database.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

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

# Allow anyone to run status
if [ "$1" = "status" ] ; then
  status -p ${pidfile} ${prog}
  RETVAL=$?
  exit $RETVAL
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

  if status -p ${pidfile} ${prog} >&/dev/null; then
    status -p ${pidfile} ${prog}
  else
    echo -n "Starting ${prog}"
    daemon --user=${user} --pidfile=${pidfile} ${bin} -c ${config} -P ${pidfile} -n
    echo
  fi

  RETVAL=$?

  return ${RETVAL}
}

stop()
{
  echo -n "Stopping ${prog} ... "
  if ! status -p ${pidfile} ${prog} >&/dev/null; then
    echo -n "No running instance of ${prog} found."
  fi
  killproc -p ${pidfile} "${prog}"
  echo

  rm -rf "${pidfile}"
}


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

