#!/bin/sh
#
# start/stop the submit job metrics daemon.
#
### BEGIN INIT INFO
# Provides:          job_metrics
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the submit job metrics daemon.
# Description:       Starts and stops the submit job metrics daemon
### END INIT INFO

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

MONITORPATH=/opt/submit/jobMetrics.py

if ! [ -x ${MONITORPATH} ]; then
   exit 1
fi

case "$1" in
   start)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Starting submit job metrics:"
         echo -n " jobmetrics" ; pgrep -fl 'python ${MONITORPATH}' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         echo -n " jobmetrics" ; daemon --check jobMetrics --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping submit job metrics:"
      fi
      echo -n " jobmetrics" ; pkill -TERM -u gridman -f "${MONITORPATH}"
      echo "."
      ;;
    restart|force-reload)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting submit job metrics:"
      fi
      echo -n " jobmetrics"
      pkill -TERM -u gridman -f "${MONITORPATH}"
      if [ -f /sbin/start-stop-daemon ]; then
         /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec ${MONITORPATH}
      else
         daemon --check jobMetrics --user=gridman ${MONITORPATH}
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/jobmetrics {start|stop|restart|force-reload}"
      exit 1
      ;;
esac

exit 0
