#!/usr/bin/env bash

#可变参数变量
languageType="java" #支持 java,javac,netcore 发布

#参数值由pom文件传递
packageName= #命令启动包名 xx.jar的xx

#固定变量
#basePath=$(cd `dirname $0`/;cd ../;pwd)
baseDirPath="${basePath}" #解压部署磁盘路径
pid= #进程pid

#所有模块列表
modules=("beidou", "beidou-rtcm")

#模块变量
moduleName="${2}" #模块名
modulePath= #模块路径
packageModulePath= #打包模块路径
biz="sec"

#配置文件路径
configPath="/root/beidou/config"

if [ "${moduleName}" == "beidou" ]
then
    packageName="${biz}-${moduleName}"
    modulePath="${baseDirPath}/${biz}-${moduleName}" #网关模块路径
    packageModulePath="${biz}-${moduleName}"
elif [ "${moduleName}" == "beidou-rtcm" ]
then
    packageName="${biz}-${moduleName}"
    modulePath="${baseDirPath}/${biz}-${moduleName}" #网关模块路径
    packageModulePath="${biz}-${moduleName}"
else
    packageName="${biz}-${moduleName}"
    modulePath="${baseDirPath}/${biz}-${moduleName}" #网关模块路径
    packageModulePath="${biz}-${moduleName}"
fi

#检测pid
function getPid()
{
    echo "检测状态---------------------------------------------"
    pid=`ps -ef | grep -n ${packageName}.jar | grep -v grep | awk '{print $2}'`
    if [ ${pid} ]
    then
        echo "运行pid：${pid}"
    else
        echo "未运行"
    fi
}

#启动程序
function start()
{
    #查询是否有启动进程
    getPid
    if [ ${pid} ]
    then
        echo "程序已启动"
    else
        echo "启动程序---------------------------------------------"
        #进入模块运行目录
        cd ${modulePath}/target/

        nohup java -Dfile.encoding=utf-8 -Xms128m -Xmx256m -jar ${packageName}.jar --spring.config.location=${configPath}/application-${moduleName}.properties --logging.config=${configPath}/logback-${moduleName}.xml >/dev/null 2>&1 &

        #查询是否有启动进程
        getPid
        if [ ${pid} ]
        then
            echo "已启动"
        else
            echo "启动失败"
        fi
    fi
}

#停止程序
function stop()
{
    getPid
    if [ ${pid} ]
    then
        echo "停止程序---------------------------------------------"
        kill -15 ${pid}
        echo -n "停止中"

        counter=0
        while [ $counter -lt 1 ]
        do
            #getPid
            pid=`ps -ef | grep -n ${packageName}.jar | grep -v grep | awk '{print $2}'`
            if [ ${pid} ]
            then
                #停止失败
                echo -n "..."
            else
                break
            fi

            sleep 2s
        done
        echo "success"
    fi
}

#检查包名参数
function checkParam()
{
    if [ ${moduleName} ]
    then
        echo -n ""
        #echo "packageName:${packageName}"
    else
        echo "模块名参数未输入"
        echo "所有模块列表："
        for modules in ${modules[@]}
        do
            echo -n $modules
        done
        echo " "
        exit
    fi
}

#启动时带参数，根据参数执行
if [ ${#} -ge 1 ]
then
    case ${1} in
        "start")
            checkParam
            start
        ;;
        "stop")
            checkParam
            stop
        ;;
        "restart")
            checkParam
            stop
            start
        ;;
        "status")
            checkParam
            ps -ef | grep -n ${packageName}.jar | grep -v grep
        ;;
        "log")
            checkParam
            tail -n 50 -f ${configPath}/logs/${moduleName}.log
        ;;
        "info")
            ps aux | grep java | grep -E 'runjian|rundo|admin' | grep -v grep
        ;;
        "killall")
            echo "杀掉所有模块进程"
            ps -ef | grep sec | grep -v grep | awk '{print $2}' | xargs kill -15
        ;;
        "info")
            ps aux | grep java | grep -E 'sec' | grep -v grep
        ;;
        "package")
            checkParam
            cd ${baseDirPath}
            echo "打包单个模块"
            mvn clean package -pl ${packageModulePath} -am -D"maven.test.skip=true"
        ;;
        "packageAll")
            echo "打包所有模块"
            cd ${baseDirPath}
            mvn clean package -D"maven.test.skip=true"
        ;;
        "update")
            checkParam
            echo "更新代码并重新部署"
            cd ${baseDirPath}
            git pull
            mvn clean package -pl ${packageModulePath} -am -D"maven.test.skip=true"
            stop
            start
        ;;
        *)
            echo "${1}无任何操作"
        ;;
    esac
else
    echo "
    command如下命令：
    start：启动
    stop：停止进程
    restart：重启
    status：查看模块状态
    log：查看模块日志
    info：查看所有模块进程
    killall：杀掉所有模块进程
    package：打包单个模块
    packageAll：打包所有模块
    update: 更新代码并部署

    示例命令如：./services.sh start beidou
    所有模块列表："
    echo -n "    "
    for modules in ${modules[@]}
    do
        echo -n $modules
    done
    echo " "
fi
