#!/bin/bash
USER_NAME=`whoami`
if [ "$USER_NAME" = "root" ];then
    echo "----->Root user can not start app.<-----"
    exit 1
fi

echo 'current branch=' $DRONE_COMMIT_BRANCH

cd `dirname $0`

NOW_DIR=`pwd`

JAVA_OPTS="-Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_MEM_OPTS=" -server -Xms256m -Xmx256m"
else
    JAVA_MEM_OPTS=" -server -Xms256m -Xmx256m"
fi
#if [ -n "$BITS" ]; then
#    JAVA_MEM_OPTS=" -server -Xms2048m -Xmx2048m"
#else
#    JAVA_MEM_OPTS=" -server -Xms2048m -Xmx2048m"
#fi

echo -e "Starting the server ..."

batch=${DRONE_COMMIT_BRANCH}

ignoreArr=("monitor","sentinel-dashboard-pro","sentinel-dashboard")

#if [[ "${ignoreArr[*]}" =~ "${batch}" ]]; then
#  JAVA_AGENT="-javaagent:/home/clay/skywalking-agent/skywalking-agent.jar -Dskywalking.agent.service_name=$DRONE_COMMIT_BRANCH -Dskywalking.trace.ignore_path=/actuator/** -Dskywalking.collector.backend_service=10.7.89.101:11800 --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -Dio.netty.tryReflectionSetAccessible=true"
#  nohup java -jar $JAVA_AGENT $JAVA_OPTS $JAVA_MEM_OPTS $DRONE_COMMIT_BRANCH.jar --spring.profiles.active=pro --server.port=8080 >./logs/spring.log 2>&1 &
#else
#  echo "exist";
#  nohup java -jar $JAVA_OPTS $JAVA_MEM_OPTS $DRONE_COMMIT_BRANCH.jar --spring.profiles.active=pro --server.port=8080 >./logs/spring.log 2>&1 &
#fi

nohup java -jar $JAVA_OPTS $JAVA_MEM_OPTS $DRONE_COMMIT_BRANCH.jar --spring.profiles.active=pro --server.port=8080 >./logs/spring.log 2>&1 &

echo -e "Check the logs for more details."

PID=`ps -ef | grep 'java' | grep "$DRONE_COMMIT_BRANCH.jar" | grep -v 'grep' | awk '{print $2}'`
echo -e "The PID Is $PID"
