#!/bin/sh

#EXEM_NDM_HOME="/{{ EXEM_HOME }}/ndm"

source /{{ EXEM_HOME }}/ndm/bin/reg.env


NDM_STOP_TIMEOUT=10

Usage()
{
	echo "Usage : ndm-ctl [version|start|restart|stop]"
	echo ""
	exit $1
}


CMD_ARGUMENT=$@

BASE_NAME=`basename $0`

case "${BASE_NAME}" in
	start)
		CMD_ARGUMENT="start"
		;;
	stop)
		CMD_ARGUMENT="stop"
		;;
	restart)
		CMD_ARGUMENT="restart"
		;;
  version)
    CMD_ARGUMENT="version"
    ;;
	mongomonctl)
		;;
	*)
		echo "unknown link. please check link name[$0]."
		Usage 1
		;;
esac


# option process
if [ -n "${CMD_ARGUMENT}" ];
then

	for i in ${CMD_ARGUMENT}
	do

#		echo "$i"

		case "$i" in
			start)
				ENV_RUN_MODE="start"
				RUN_LIST="start"
				;;
			restart)
				ENV_RUN_MODE="restart"
				RUN_LIST="stop start"
				;;
			stop)
				ENV_RUN_MODE="stop"
				RUN_LIST="stop"
				;;
		  version)
		    ENV_RUN_MODE="version"
		    RUN_LIST="version"
		    ;;
			help)
				Usage 0
				;;
			*)
				Usage 1
				;;

		esac

	done

fi



if [ ! -d "${EXEM_NDM_HOME}" ];then
	echo "agent home directory is not exist.[${EXEM_NDM_HOME}]"
	exit 1
fi


cd ${EXEM_NDM_HOME} || exit 1


PROCESS_NAME="exem-ndm-agent"
SVC_FILE="ndm-svc"
SVC_PID_FILE="data/svc.pid"
AGENT_PID_FILE="data/ndm-agent.pid"


echo "${PROCESS_NAME} ${ENV_RUN_MODE} --- "


for MODE in ${RUN_LIST}; do

	case "${MODE}" in
		version)
				bin/exem-ndm-agent -v
				;;
		start)

			if [ -f "${SVC_PID_FILE}" ];then

				SVC_PID=`cat ${SVC_PID_FILE}`

				PROC_COMM=`cat /proc/${SVC_PID}/comm`
				if [ $? -ne 0 ];then
					PROC_COMM="none"
				fi

				if [ "${PROC_COMM}" = "${SVC_FILE}" ];then
						echo "${PROCESS_NAME} is running."
						exit 1
				fi

				rm -rf ${SVC_PID_FILE}

			fi

			if [ ! -f "bin/${SVC_FILE}" ];then
				echo "bin/${SVC_FILE} is not exist."
				exit 2
			fi

			bin/${SVC_FILE} >> ${EXEM_NDM_HOME}/log/svc.log 2>&1 &
	#		bin/${SVC_FILE} > /dev/null 2>&1 &
			SVC_PID="$!"
			echo "SVC_PID[${SVC_PID}]"
			echo "${SVC_PID}" > ${SVC_PID_FILE}
			;;
		stop)

			SVC_PID=`cat ${SVC_PID_FILE}`
			if [ $? -ne 0 ] || [ "${SVC_PID}" = "" ];then
				echo "${PROCESS_NAME} Service is not running."
				exit 1
			fi
			
			PS_SVC_PID=`ps -ef | grep -v grep | grep ${SVC_PID} | grep ${SVC_FILE}`
			if [ ! "${PS_SVC_PID}" = "" ];then
				kill -9 ${SVC_PID}
				echo "${PROCESS_NAME} Service terminate."
			else
				echo "${PROCESS_NAME} Service pid is not match[${PS_SVC_PID}/${SVC_PID}]"
			fi
			
			rm -rf ${SVC_PID_FILE}
			

			if [ -f "${AGENT_PID_FILE}" ];then
			
				RUN_NDM_PID=`cat ${AGENT_PID_FILE} | tr '\0' '\n'`
				
				rm -rf ${AGENT_PID_FILE}

				echo -n "Waiting for ${PROCESS_NAME} process(${RUN_NDM_PID}) exit."

				NUM="0"
				while [ ${NUM} -lt ${NDM_STOP_TIMEOUT} ]
				do
					PS_NDM_PID=`ps -ef | grep -v grep | grep ${RUN_NDM_PID} | grep mongomon`
					if [ "${PS_NDM_PID}" = "" ];then
						break
					fi

					NUM=`expr ${NUM} + 1`
					sleep 1
					echo -n "."
				done

				echo ""

				if [ ${NUM} -eq ${NDM_STOP_TIMEOUT} ];then
					echo "${PROCESS_NAME} process shutdown wait timed out."
				else
					echo "${PROCESS_NAME} process terminated."
				fi

			else
				echo "${PROCESS_NAME} process is not running."
			fi

			;;
		*)
			Usage 2
			;;
	esac
done

