#!/bin/bash
 
cd `dirname $0`
 
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
 
JAR_NAME="lw-superbot-client-1.0.0.jar"
JAR_PATH=$CUR_SHELL_DIR/lib/$JAR_NAME
 
JAVA_MEM_OPTS=" -server -Xms4096m -Xmx4096m -Dspring.cloud.inetutils.ignored-interfaces=lo* --add-opens java.base/java.lang=ALL-UNNAMED"
#JAVA_MEM_OPTS=""
 
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log

echo_help()
{
    echo -e "syntax: sh $CUR_SHELL_NAME start|stop|start_wait"
}


run_start()
{
    # check server
    PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
    if [ -n "$PIDS" ]; then
        echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
        exit 1
    fi

    echo "Starting the $JAR_NAME..."

    # start
    nohup java $JAVA_MEM_OPTS -jar $JAR_PATH >> $LOG_PATH 2>&1 &

    COUNT=0
    while [ $COUNT -lt 1 ]; do
        sleep 1
        COUNT=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
        if [ $COUNT -gt 0 ]; then
            break
        fi
    done
    PIDS=`ps  --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
    echo "${JAR_NAME} Started and the PID is ${PIDS}."
    echo "You can check the log file in ${LOG_PATH} for details."
}

run_stop()
{

    PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
    if [ -z "$PIDS" ]; then
        echo "ERROR:The $JAR_NAME does not started!"
        exit 1
    fi

    echo -e "Stopping the $JAR_NAME..."

    for PID in $PIDS; do
        kill $PID > /dev/null 2>&1
    done

    COUNT=0
    while [ $COUNT -lt 1 ]; do
        sleep 1
        COUNT=1
        for PID in $PIDS ; do
            PID_EXIST=`ps --no-heading -p $PID`
            if [ -n "$PID_EXIST" ]; then
                COUNT=0
                break
            fi
        done
    done

    echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
}

run_start_wait()
{
    run_start
    for i in `seq 60` ; do
        COUNT=`tail -10 $LOG_PATH | grep "Tomcat started on port(s)"|wc -l`
        if [[ $COUNT -gt 0 ]]; then
            echo "$(date "+%H:%M:%S") ${JAR_NAME} started."
            break
        else
            echo "$(date "+%H:%M:%S") ${JAR_NAME} waiting for start...."
            sleep 1s
        fi
    done
}


if [ -z $1 ];then
    echo_help
    exit 1
fi

if [ ! -d "$LOG_DIR" ];then
    mkdir "$LOG_DIR"
fi

if [ ! -f "$LOG_PATH" ];then
    touch "$LOG_DIR"
fi

if [ "$1" == "start" ];then
    run_start
elif [ "$1" == "stop" ];then
    run_stop
elif [ "$1" == "start_wait" ];then
    run_start_wait
else
    echo_help
    exit 1
fi 
