#!/bin/bash --login
#
# chkconfig:   - 20 80
# description: Starts and stops the App.

ENV="${ENV}"
NACOS_NAMESPACE="${NACOS_NAMESPACE}"
ADATE=`date +%Y%m%d%H%M%S`
APP_NAME="${PWD##*/}"
APP_VERSION='1.0'
NACOS_SERVER="${NACOS_SERVER}"
NACOS_PORT="${NACOS_PORT}"
NACOS_USER="${NACOS_USER}"
NACOS_PWD="${NACOS_PWD}"

APP_HOME=`pwd`
dirname $0|grep "^/" >/dev/null
if [ $? -eq 0 ];then
   APP_HOME=`dirname $0`
else
    dirname $0|grep "^\." >/dev/null
    retval=$?
    if [ $retval -eq 0 ];then
        APP_HOME=`dirname $0|sed "s#^.#$APP_HOME#"`
    else
        APP_HOME=`dirname $0|sed "s#^#$APP_HOME/#"`
    fi
fi

if [ ! -d "$APP_HOME/logs" ];then
  mkdir $APP_HOME/logs
fi

#LOG_PATH=$APP_HOME/logs/$APP_NAME.out
LOG_PATH=/dev/null
GC_LOG_PATH=$APP_HOME/logs/gc-$APP_NAME-$ADATE.log
#JMX
JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
#JVM Parameter
NACOS_OPTS="-Dspring.cloud.nacos.config.namespace=$NACOS_NAMESPACE -Dspring.cloud.nacos.config.server-addr=$NACOS_SERVER:$NACOS_PORT -Dspring.cloud.nacos.discovery.namespace=$NACOS_NAMESPACE -Dspring.cloud.nacos.discovery.server-addr=$NACOS_SERVER:$NACOS_PORT -Dspring.cloud.nacos.config.username=$NACOS_USER -Dspring.cloud.nacos.config.password=$NACOS_PWD -Dspring.cloud.nacos.discovery.username=$NACOS_USER -Dspring.cloud.nacos.discovery.password=$NACOS_PWD "
JVM_OPTS="-Dname=$APP_NAME -Djava.security.egd=file:/dev/./urandom $NACOS_OPTS -Dspring.profiles.active=$ENV -Dfile.encoding=UTF-8 -Djeesuite.configcenter.profile=$ENV -Duser.timezone=Asia/Shanghai -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider -Xms128M -Xmx256M -XX:+UseG1GC -XX:MaxGCPauseMillis=200"

PROGRAM_ARGS="--eureka.instance.metadataMap.version=$APP_VERSION"

JAR_FILE_NAME=`ls -l *.jar | tail -n 1 | awk '{print $9}'`
JAR_FILE=$APP_HOME/$JAR_FILE_NAME
#echo "Jar file is $JAR_FILE_NAME "

pid=0
start(){
  checkpid
  if [ ! -n "$pid" ]; then
    JAVA_CMD="nohup /usr/local/openjdk-11/bin/java -jar $JVM_OPTS $JAR_FILE > $LOG_PATH 2>&1 &"
    #JAVA_CMD="exec /usr/local/openjdk-11/bin/java -jar $JVM_OPTS $JAR_FILE"
    echo "JAVA_CMD:${JAVA_CMD}"
    ${JAVA_CMD}

    #echo "$RUNNING_PASSWD" | su - $RUNNING_USER -c "$JAVA_CMD"

    echo "---------------------------------"
    echo "Application is starting "
    echo "---------------------------------"
  else
      echo "$APP_NAME is runing PID: $pid"
  fi

}


status(){
   checkpid
   if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
   else
     echo "$APP_NAME runing PID: $pid"
   fi
}

checkpid(){
    pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'`
}

stop(){
    checkpid
    if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing"
    else
      echo "$APP_NAME stop..."
      kill $pid
      i=1
      while ((i<=30))
      do
         checkpid
         if [ ! -n "$pid" ]; then
            echo "$APP_NAME has been stopped successfully!"
            break
         else
            sleep 1
            echo "Waiting for the stopping of $APP_NAME in $i seconds"
            let i++
         fi
      done
      checkpid
      if [ -n "$pid" ]; then
        echo "$APP_NAME is still running for 30 seconds, will kill it."
        kill -9 $pid
        echo "$APP_NAME is killed!"
      fi
    fi
}

restart(){
    stop
    sleep 1s
    start
}

case $1 in
          start) start;;
          stop)  stop;;
          restart)  restart;;
          status)  status;;
              *)  echo "require start|stop|restart|status"  ;;
esac