#!/bin/sh
#
# Author: Markus Bloch
#
# /etc/init.d/collectord
#
### BEGIN INIT INFO
# Provides:        collectord
# Required-Start:
# Required-Stop:
# Default-Start:   2 3 5
# Default-Stop:    0 1 6
# Short-Description: starts collectord for presence detection
# Description:       Collector Daemon for the Presence detection of multiple rooms (presenced)
#
# Installation:
# - copy this file to /etc/init.d
# - chmod 744 /etc/init.d/collectord
# - enable and edit in YAST system services
### END INIT INFO
 

COLLECTORD_BIN=/usr/sbin/collectord
COLLECTORD_CFG=/etc/collectord.conf
COLLECTORD_OPTIONS="-v -l /var/log/collectord.log"
COLLECTORD_PID="/var/run/collectord.pid"

NAME="collectord"

if [ ! -e "$COLLECTORD_BIN" ] ;then
	echo "binary not found or not executable"
	exit 7
fi

if [ ! -r "$COLLECTORD_CFG" ] ;then
	echo "config file not found"
	exit 6
fi


 case "$1" in
   start)
     echo -n "Start $NAME ..."
     
     $COLLECTORD_BIN -c $COLLECTORD_CFG -d $COLLECTORD_OPTIONS 2>&1
     
     if [ $? = "0" ] ;then
	echo " OK"
	exit 0

     else
	echo " FAILED"
	exit 3
     fi 
	
     ;;
   stop)
     if [ -r "$COLLECTORD_PID" ] ;then

     echo  "Stopping $NAME"
     kill $(cat $COLLECTORD_PID)
     else
 	echo "$NAME is not running"
	exit 0
     fi
     ;;
   status)

    if [ -r "$COLLECTORD_PID" ] ;then
     pid=$(cat $COLLECTORD_PID)
     cnt=`ps -ef | grep "$pid" | grep -v grep | wc -l`
     if [ "$cnt" -eq "0" ] ; then
       echo "$NAME is not running"
       exit 3
     else
       echo "$NAME is running"
       exit 0
     fi
    else
    echo "no pidfile found. collectord is not running"
    exit 3
    fi
     ;;

   *)
     echo "Usage: $NAME {start|stop|status}"
   exit 1
 esac
 exit 0
