#!/bin/sh

cd /usr/local/controller/connector

case "$1" in
        start)
    		if [ "$(ps x | grep -v grep | grep -c ./connector)" != "0" ]; then
    		../rc.d/010.connector.sh stop
    		fi
                ./connector > /dev/null 2>&1 &
                echo -n "connector start; "
                ;;
        stop)
            if [ "$(ps x | grep -v grep | grep -c mysqlconn.pl)" != "0" ]; then
    		    ../scripts/pmanager.sh stop
    		    echo -n "polling stop (by connector); "
    		    timeout=0
    		    while [ "$(ps x | grep -v grep | grep -c ./pol)" -gt "1" -a $timeout -lt 60 ] ;
    		     do
    		      sleep 1
    		      timeout=$(($timeout+1))
    		    done;
    		fi
            timeout=0

              killall -TERM ./pol > /dev/null 2>&1 
              killall -TERM ./snmppol > /dev/null 2>&1 

   		    while [ "$(ps x | grep -v grep | grep -c ./connector)" != "0" -a $timeout -lt 60 ] ;
   		     do
              killall connector > /dev/null 2>&1 
   		      sleep 1
   		      timeout=$(($timeout+1))
   		    done;
            echo -n "connector stop; "
         ;;
        restart)
		~/rc.d/010.connector.sh start > /dev/null
                echo " connector restart;"
                ;;
        *)
                echo ""
                echo "Usage: `basename $0` { start | stop | restart}"
                echo ""
                ;;
esac

exit 0

