#!/bin/sh
#
# Author: Markus Bloch
#
# /etc/init.d/collectord
#
### BEGIN INIT INFO
# Provides:        presenced
# Required-Start:
# Required-Stop:
# Default-Start:   2 3 5
# Default-Stop:    0 1 6
# Short-Description: starts presenced 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/presenced
# - enable and edit in YAST system services
### END INIT INFO
 

presenced_BIN=/usr/sbin/presenced
presenced_OPTIONS="-v -l /var/log/presenced.log"
presenced_PID="/var/run/presenced.pid"

NAME="presenced"

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



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

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

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

    if [ -r "$presenced_PID" ] ;then
     pid=$(cat $presenced_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. presenced is not running"
    exit 3
    fi
     ;;

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