#!/bin/bash
#description: start and stop guradserver
#chkconfig:35 99 99

. /etc/rc.d/init.d/functions

program_path=`cat /var/log/version_control`
program_mainserver=`cat $program_path/command_control | grep guard_name | grep -v grep | awk '{print $2}'`
program_script=`cat $program_path/command_control | grep script_guard | grep -v grep | awk '{print $2}'`

export LD_LIBRARY_PATH=$program_path/libs

start()
{
	local PID
	local RES
	
	cd $program_path
	
	#жϷǷѾ
	PID=`pidof -x ${program_mainserver}`
	
	if [[ -z ${PID} ]];then
		echo -n "Starting $program_script: "
		./${program_mainserver} 
		RES=$?
		if [ $RES == 0 ];then
			success
		else
			failure
		fi
		echo
	else
		echo "$program_script has been start"
	fi
}

stop()
{
	local PID
	local RES

	PID=`pidof -x ${program_mainserver}`

	if [[ -n ${PID} ]];then
		echo -n "Stopping $program_script: "
		kill -9 ${PID}
		RES=$?
		if [ $RES == 0 ];then
			success
		else
			failure
		fi
		echo
	else
		echo "$program_script didn't run"
	fi
}


#main
case "$1" in
 start)
  start
  ;;
 stop)
  stop
  ;;
 restart)
  stop
  sleep 1
  start
  ;;
 status)
  status $program_mainserver
  ;;
 *)

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

