#!/bin/sh
#
#chkconfig:35 99 1
#
#description: upsMonitor service
#
MONITOR_HOME=/opt/Viewpower
UPS_PORT=51099
pids=$(netstat -lnp | grep $UPS_PORT | awk '{print $7}')
pids=${pids%/*}
case $1 in

	start)
		if [ -n "$pids" ]; then
			echo "upsMonitor service has already started"
		else 
			cd $MONITOR_HOME
			./StartMain &
			echo "upsMonitor service is starting..."
		fi
		exit 1
	;;

	stop)
		if [ -n "$pids" ]; then
			echo "upsMonitor service is stopping..."
			cd $MONITOR_HOME
			./StopMain &
		else 
			echo "upsMonitor servcie has already stopped"
		fi
		exit 1
	;;
	
	restart)
		cd $MONITOR_HOME
		./StopMain &
		sleep 5
		cd $MONITOR_HOME
		./StartMain &
		echo "upsMonitor service is restarting..."
		exit 1
	;;

	status)
		if [ -n "$pids" ]; then
			echo "upsMonitor is running "
		else 
			echo "upsMonitor is not run "
		fi
	;;

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