#!/bin/sh
cd /micro
CONSOLE_LOG_ENABLE=${CONSOLE_LOG_ENABLE:-false}
DAILY_LOG_ENABLE=${DAILY_LOG_ENABLE:-true}
S_ACTIVE=${S_ACTIVE:-dev}
JAVA_OPS=${JAVA_OPS:-'-Xms512M -Xmx512M'}
CURR_PATH=$(pwd)
SERVICE_NAME=""
JAR_NAME=$(find ./ -maxdepth 1 -name \*1.0.0-SNAPSHOT-exec.jar -type f -exec basename {} \;)
if [ -z $JAR_NAME ];then
 JAR_NAME=$(find ./ -maxdepth 1 -name \*1.0.0-SNAPSHOT.jar -type f -exec basename {} \;)
 SERVICE_NAME=${JAR_NAME%%-1.0.0-SNAPSHOT.jar}
else
 SERVICE_NAME=${JAR_NAME%%-1.0.0-SNAPSHOT-exec.jar}
fi

TMP_DIR=${CURR_PATH}"/tmp"
mkdir -p $TMP_DIR

sed -i 's/AIOPS_COLLECTOR_URL/'"$AIOPS_COLLECTOR_URL"'/' /root/APM/aiopsagent.config
CURRENT=`date "+%Y-%m-%d %H:%M:%S"`
TIMESTAMP=`date -d "$CURRENT" +%s`
CONTAINER_IP=`hostname -i|sed "s/\./ /g"|xargs printf "%03d%03d%03d%03d"`_$TIMESTAMP

JAVA_OPS="-javaagent:/root/APM/aiopsagent-1.8.0.jar -Dpinpoint.agentId=$CONTAINER_IP -Dpinpoint.licence=$AIOPS_LICENCE -Dpinpoint.applicationName=$AIOPS_APP_NAME "${JAVA_OPS}

JAVA_OPS="-Ddaily.log.enable=$DAILY_LOG_ENABLE -Dconsole.log.enable=$CONSOLE_LOG_ENABLE -Dspring.profiles.active=$S_ACTIVE -Dtmp.dir=${TMP_DIR} "${JAVA_OPS}

JAR_LIB=${CURR_PATH}"/lib"
if [ -d $JAR_LIB ];then
JAVA_OPS=${JAVA_OPS}" -Dloader.path=file://${JAR_LIB} "
fi

PATH_TO_JAR="$CURR_PATH/$JAR_NAME"
PID_PATH_NAME="$CURR_PATH/$SERVICE_NAME-pid"

if [ -f $PID_PATH_NAME ]; then
	if test -z $(cat $PID_PATH_NAME) ;then
		rm $PID_PATH_NAME
	fi
fi

start()
{
	echo "Starting $SERVICE_NAME ..."
    if [ ! -f $PID_PATH_NAME ]; then
		if [ $1 -eq 0 ]; then
			java $JAVA_OPS -jar $PATH_TO_JAR
			echo "$SERVICE_NAME stoped ..."
		else
		  	nohup java $JAVA_OPS -jar $PATH_TO_JAR >/dev/null 2>&1 &
		    echo $! > $PID_PATH_NAME
	        echo "$SERVICE_NAME started ..."
    	fi
    else
        PID=$(cat $PID_PATH_NAME);
        PID_EXISTS=$(ps -p $PID);
        if [ $? -eq 0 ]; then
			echo "$SERVICE_NAME is already running ..."
	    else
			if [ $1 -eq 0 ]; then
				java $JAVA_OPS -jar $PATH_TO_JAR
				echo "$SERVICE_NAME stoped ..."
			else
				nohup java $JAVA_OPS -jar $PATH_TO_JAR >/dev/null 2>&1 &
				echo $! > $PID_PATH_NAME
				echo "$SERVICE_NAME started ..."
			fi
		fi
    fi
}

stop()
{
    if [ -f $PID_PATH_NAME ]; then
        PID=$(cat $PID_PATH_NAME);
        echo "$SERVICE_NAME stoping ..."
		i=0
        kill $PID
        PID_EXISTS=$(ps -p $PID);
		while ( [ $? -eq 0 ] && [ "$i" -lt 15 ] )
		do
			sleep 1;
			let "i += 1";
			echo "$i sec"
			PID_EXISTS=$(ps -p $PID);
		done
		PID_EXISTS=$(ps -p $PID);
        if [ $? -eq 0 ]; then
			CPID=$(ps -ef|grep java|grep $PATH_TO_JAR|grep -v grep|awk '{print $2}')
        	kill -9 $CPID;
			echo "$SERVICE_NAME stop failure so kill process ..."
		fi
        echo "$SERVICE_NAME stopped ..."
        rm $PID_PATH_NAME
    else
        echo "$SERVICE_NAME is not running ..."
    fi
}

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