#!/bin/bash
#rd_fuxiong_pan
:<<!
resolve links - $0 may be a softlink
# mservice执行脚本,启动参数为start|status|stop|restart
修改记录：
2018-03-02:
删除：
    1.去除debug参数，修改为环境变量控制
    2.去除第二个参数传递配置名，从../assembly/product/env/$2/复制配置文件到bin目录，使用独立脚本
    
变更：
    1.按照最新的目录结构加载classpath
    2.拆解不同的配置初始化脚本
    3.增加第二个参数
!
#脚本只要发生错误，就终止执行
set -e
#在运行结果之前，先输出执行的那一行命令
#set -x

#延时检查进程是否存在
if [ ! $KD_STARTUP_SLEEP_TIME ]; then
      KD_STARTUP_SLEEP_TIME=3
fi


#如果存在目录的环境变量，则不再进入脚本所在目录
#如果不存在，则进入脚本所在的目录
if [ ! $PRGDIR ]; then
    #获取脚本所有的绝对路径
    PRG="$0"
    while [ -h "$PRG" ]; do
      ls=`ls -ld "$PRG"`
      link=`expr "$ls" : '.*-> \(.*\)$'`
      if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
      else
        PRG=`dirname "$PRG"`/"$link"
      fi
    done

    # Get standard environment variables
    PRGDIR=`dirname "$PRG"`
    export PRGDIR=$PRGDIR
    #进入脚本所在的目录
    cd $PRGDIR
fi

#检查java运行环境
if [ -x "$JAVA_HOME/bin/java" ]; then
    JAVA="$JAVA_HOME/bin/java"
else
    JAVA=`which java`
fi

#检查java命令是否存在
if [ ! -x "$JAVA" ]; then
    echo "无法找到java运行文件，请安装jdk，或者设置PATH环境变量或者JAVA_HOME环境变量"
    echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
    exit 1
fi

#加载应用环境变量
source setenv.sh




#判断是否存在参数-d或者--daemonize，有则后台运行
if [[ $* =~ "-d" || $* =~ "--daemonize" ]] ; then
    daemonized="true"
fi

start()
{
    #拉jar包
    #java -jar -DAPPSTORE_URL=http://172.18.2.144:88/kingdee/dev2.0/mservice/ -DdeployedAppIds=fi-er,fi-ai -DCUSLIBS=  kd-appstore-download-1.0.jar
    $JAVA -jar kd-appstore-download-1.0.jar
    #获取进程号,避免在start时,出现pid值仍然存在的情况
    pidlist=`ps -ef|grep "appName=$appName " |grep -v "grep"|awk '{print $2}'`
    #如果有进程存在，则直接跳出
    if [ "$pidlist" ]; then
        echo "ERROR:: $appName is still running,please confirm and use stop|restart!"
        exit 1
    fi
  
    #初始化环境变量
    #source执行子级命令后继续执行父级命令
    #同时子级设置的环境变量会影响到父级的环境变量
    source setclasspath.sh
    
    #判断是否存在classpath
    if [ ! $KD_CLASSPATH ]; then
      echo "ERROR:: 无法初始化环境变量CLASSPATH,异常退出"
      echo "ERROR:: can not setCLASSPATH,exit error!!!"
      exit 1
    fi

    echo -n "Starting $appName: "
    #如果参数长度为零，则运行在前端
    if [ -z "$daemonized" ]; then
        echo "start app in front end !!!"
        exec "$JAVA" $JVM_OPTS -DappName=$appName -DMONITOR_HTTP_PORT=9999 $JVM_DEBUG_OPTS $JVM_GC_OPTS $JVM_JMX_OPTS $JAVA_APP_OPTS $WEB_OPTS \
               -classpath "$KD_CLASSPATH" $BOOTCLASS
    else
        #后台运行
        nohup "$JAVA" $JVM_OPTS -D"appName=$appName "  -DMONITOR_HTTP_PORT=9999  $JVM_DEBUG_OPTS $JVM_GC_OPTS $JVM_JMX_OPTS $JAVA_APP_OPTS $WEB_OPTS \
               -classpath "$KD_CLASSPATH" $BOOTCLASS > ../logs/std.out  2>&1 &
        
        if [ ! -z "$KD_STARTUP_SLEEP_TIME" ]; then
          sleep $KD_STARTUP_SLEEP_TIME
        fi
        pidlist=`ps -ef|grep "appName=$appName " |grep -v "grep"|awk '{print $2}'`
        if [ "$pidlist" = "" ] ; then
           echo "ERROR:: Could not execute $appName java process，please check the error log"
           exit 1
        else
           echo "success to run $appName app，please check status!"
           exit 0
        fi
    fi
}


stop()
{
  echo -n $"Stopping $appName: "
  pidlist=`ps -ef|grep "appName=$appName " |grep -v "grep"|awk '{print $2}'`
  if [ "$pidlist" = "" ];then
      echo "no $appName pid alive"
  else
      for pid in ${pidlist}
      {
         kill -9 $pid
         echo "KILL $pid:"
      }
      echo "$appName stop success"
   fi
}

status()
{
       pidlist=`ps -ef|grep "appName=$appName " |grep -v "grep"|awk '{print $2}'`
       if [ "$pidlist" = "" ] ; then
          echo "$appName is stoped! "
       else
          echo "$appName is running! "
       fi
}

#此处有个问题,之前运行为正常start模式,重启后,也会变成debug模式
#已修改,不要使用pid,换为dpid,应该是因为pid变量在上面有使用:(,那不是局部变量么?
restart() {
      pidlist=`ps -ef|grep $BOOTCLASS |grep "appName=$appName " |grep -v "grep"|awk '{print $2}'`
      if [ "$pidlist" = "" ];then
          echo "no $appName pid alive"
          start
      else
          for pid in ${pidlist}
          {
             kill -9 $pid
             echo "KILL $pid:"
          }
          echo "$appName stop success"
          start
       fi
}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload|force-reload)
    restart
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|debug}"
    RETVAL=1
esac

exit $RETVAL
