#!/bin/bash
#chkconfig: 2345 52 48
#description: Chelsio Uwire Manager Station 

### BEGIN INIT INFO
# Provides:          Chelsio_Uwire_Manager_Station
# Required-Start:    $network
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Chelsio Unified Wire Manager Station Daemon
# Description:       The Chelsio Unified Wire Manager Station daemon 
#	service.  We want it to be active in runlevels 2 3 4
#	and 5, as these are the runlevels with the network
#	available.
### END INIT INFO

RETVAL=0
prog="Chelsio Uwire Manager Management station"
EXP_INSTANCE=0
instance=0
SNMP_INSTANCE=1
BIN_PATH="/opt/chelsio/uwire_mgmt/mgmt_station"
start_mgmtst()
{

        #echo -n $"Starting $prog: "
	
	$BIN_PATH/runMgmtStation.sh 1>/dev/null 
	RETVAL=$?
	if [ $RETVAL -eq 0 ];
         then
                echo  "Unified Management Station : Started"
        else
                echo  "Unified Management Station : Already Running"
        fi

        return $RETVAL

}

stop_mgmtst() {
        # echo -n $"Stopping $prog: "
 
	instance=$(ps -eaf | grep chelsio_mgmtst| grep -v grep | wc -l )
			
		if [ $instance -gt 0 ]; then
		   pid=` ps -aef |grep chelsio_mgmt | grep chnotifier | grep -v grep | awk '{print $2}'` 
		    kill -9 $pid
		   pid=` ps -aef |grep chelsio_mgmt | grep -v chnotifier | grep -v grep | awk '{print $2}'`
		    kill -9 $pid
		    rm -f /dev/mqueue/*
		    echo "Unified Management Station : Stopped"
		else
		     echo "Unified Management Station : Not Running"
		fi
			
               
	RETVAL=$?
        return $RETVAL
}

status_mgmtst() {
	instance=$(ps -eaf | grep chelsio_mgmtst| grep -v grep | wc -l )
			
	if [ $instance -eq 2 ]; then
	    echo "Unified Management Station: Running "
	else
	    echo "Unified Management Station: Stopped"
	fi
			
               
	RETVAL=$?
        return $RETVAL
}

restart_mgmtst(){
	stop_mgmtst
	start_mgmtst
}


if [ $UID -ne 0 ]; then
    echo "chelsio-mgmtstd: Permission denied: Not a root user"
    exit 0
fi

case "$1" in
  start)
	start_mgmtst
	;;
  stop)
	stop_mgmtst
	;;
  status)
	status_mgmtst
	;;
  restart)
	restart_mgmtst
        ;;
  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	RETVAL=1
esac

exit $RETVAL
