#!/bin/bash

#jar 包启动、停止、查看状态程序


##以下内容需根据应用情况修改
#-------------------------------------
APP_NAME="botany"                   #应用名称，对应jar包botany-0.0.1-SNAPSHOT.jar
APP_VERSION="1.0.0"
SPRING_PROFILE="development"        #应用配置文件
JAVA_OPT="-Xss256k -Xmx1000m"        #JAVA 运行时参数
USE_JPDA="false"                    #是否开启JPDA，true 开启，false关闭
JPDA_PORT="5005"                    #如果有多个java包同时运行，需要修改为不同
JAVA_HOME=/usr/local/java/jdk1.8.0_251    #JAVA 目录
#------------------------------------



JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=$JPDA_ADDRESS,server=y,suspend=n"

#java 路径

CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}

#应用所在目录，PID,LOG文件定义，无需修改
APP_BASE_HOME=$(cd $(dirname "$0") && pwd)
APP_PID=${APP_BASE_HOME}/${APP_NAME}.PID
APP_OUT=${APP_BASE_HOME}/${APP_NAME}.log

cd ${APP_BASE_HOME}

if [ $# -lt 1 ];then
    echo "Usage: $0 start|stop|restart|status"
    exit
fi
have_tty=0
if [ "`tty`" != "not a tty" ]; then
    have_tty=1
fi

checkstatus() {

  if [ -f "$APP_PID" ]; then
    if [ -s "$APP_PID" ]; then
      if [ -r "$APP_PID" ]; then
        PID=`cat "$APP_PID"`
        ps -p $PID >/dev/null 2>&1
        if [ $? -eq 0 ] ; then
          echo "$APP_NAME is running."
        else 
          echo "$APP_NAME is stopped."
          rm -f $APP_PID
          exit 1      
        fi
      fi
    fi
  else
    echo "$APP_NAME is stopped."
    exit 1
  fi 
}

shutdown() {
  if [ ! -z "$APP_PID" ]; then
    if [ -f "$APP_PID" ]; then
      if [ -s "$APP_PID" ]; then
        kill -0 `cat "$APP_PID"` >/dev/null 2>&1
        if [ $? -gt 0 ]; then
          echo "PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted."
          exit 1
        fi
      else
        echo "PID file is empty and has been ignored."
      fi
    else
      echo "\$APP_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
      exit 1
    fi
  fi

  if [ ! -z "$APP_PID" ]; then
    kill -9 `cat "$APP_PID"` >/dev/null 2>&1
    rm -f "$APP_PID" >/dev/null 2>&1
  fi
  echo "$APP_NAME  shutdown successfully."
}

startup() {
  if [ ! -z "$APP_PID" ]; then
    if [ -f "$APP_PID" ]; then
      if [ -s "$APP_PID" ]; then
        echo "Existing PID file found during start."
        if [ -r "$APP_PID" ]; then
          PID=`cat "$APP_PID"`
          ps -p $PID >/dev/null 2>&1
          if [ $? -eq 0 ] ; then
            echo "$APP_NAME appears to still be running with PID $PID. Start aborted."
            echo "If the following process is not a $APP_NAME process, remove the PID file and try again:"
            ps -f -p $PID
            exit 1
          else
            echo "Removing/clearing stale PID file."
            rm -f "$APP_PID" >/dev/null 2>&1
            if [ $? != 0 ]; then
              if [ -w "$APP_PID" ]; then
                cat /dev/null > "$APP_PID"
              else
                echo "Unable to remove or clear stale PID file. Start aborted."
                exit 1
              fi
            fi
          fi
        else
          echo "Unable to read PID file. Start aborted."
          exit 1
        fi
      else
        rm -f "$APP_PID" >/dev/null 2>&1
        if [ $? != 0 ]; then
          if [ ! -w "$APP_PID" ]; then
            echo "Unable to remove or write to empty PID file. Start aborted."
            exit 1
          fi
        fi
      fi
    fi
  fi

  touch "$APP_OUT"
  if [ "$USE_JPDA" == "true" ];then
    nohup java $JPDA_OPTS $JAVA_OPT  -jar ${APP_NAME}-${APP_VERSION}-SNAPSHOT.jar --spring.profiles.active=${SPRING_PROFILE}  > $APP_OUT 2>&1 &
  else
    nohup java $JAVA_OPT  -jar ${APP_NAME}-${APP_VERSION}-SNAPSHOT.jar --spring.profiles.active=${SPRING_PROFILE} > $APP_OUT 2>&1 &
  fi
  if [ ! -z "$APP_PID" ]; then
    echo $! > "$APP_PID"
  fi
  echo "$APP_NAME started."
}

case $1 in
    "start")
    startup
    ;;
    "stop")
    shutdown
    ;;
    "restart")
    shutdown
    startup
    ;;
    "status")
    checkstatus
    ;;
esac