#!/bin/bash
# chkconfig: 2345 55 25
#
# description: start | stop | restart /usr/local/dectris/bin/rsyncd_dectris

device=rsyncd_dectris
rsyncd="/usr/local/dectris/bin/rsync_dectris --daemon"
pidFile=/var/run/rsync_dectris.pid
lockFile=/var/run/rsync_dectris.lock

start() {
    rm -rf $pidFile
    echo "Starting $rsyncd"
    nice -n 19 $rsyncd 
}


stop() {
    echo "Stopping $rsyncd"
    rm -rf $pidFile
    rm -rf $lockFile
    killall -9 rsync_dectris    
}

status() {

    echo $"Status of $device"
    if [ -e ${pidFile} ]; then
	for pid in $(cat ${pidFile}); do
	    if ps -p $pid > /dev/null; then
		echo "$device is running with the PID $pid"
	    fi
	done
    else
	echo "$device is not running"	
    fi
}





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

exit 0
