#!/bin/sh

APP_PROF=wonju
APP_HOME=$(dirname $0)

usage() {
        echo "Usage:" $0 "[api/app/sig/web]"
        echo "RUN tsi-api-server:" $0 "api"
        echo "RUN tsi-app-server:" $0 "app"
        echo "RUN tsi-sig-server:" $0 "sig"
        echo "RUN tsi-web-server:" $0 "web"
        exit
}

case $1 in
  api)
    APP_NAME=tsi-api-server
    APP_VER=0.0.1
    APP_EXT=jar
    ;;
  app)
    APP_NAME=tsi-app-server
    APP_VER=0.0.1
    APP_EXT=jar
    ;;
  sig)
    APP_NAME=tsi-sig-server
    APP_VER=0.0.1
    APP_EXT=war
    ;;
  web)
    APP_NAME=tsi-web-server
    APP_VER=0.0.1
    APP_EXT=war
    ;;
  *)
    usage
    ;;
esac


EXE_NAME=$APP_NAME-$APP_VER.$APP_EXT
PID_NAME=$APP_NAME.pid
APP_PID=$APP_HOME/conf/$PID_NAME

export JAVA_OPT="-server"
#export JAVA_OPT="$JAVA_OPT -Xms4096m -Xmx4096m"
export JAVA_OPT="$JAVA_OPT -Xlog:gc*:file=${APP_HOME}/logs/${APP_NAME}/${APP_NAME}.gc.log"
export APP_OPT="-Dspring.profiles.active=${APP_PROF}"
#export APP_OPT="--spring.config.location=conf//$APP_NAME.yml"

if [ ! -z "$APP_PID" ]; then
  if [ -f "$APP_PID" ]; then
    echo "$APP_NAME is already running...."
    exit
  fi
fi

#nohup java $JAVA_OPT -jar ./$EXE_NAME $APP_OPT 1> /dev/null 2>&1 &
#java $JAVA_OPT -jar ./$EXE_NAME $APP_OPT &
java $JAVA_OPT -jar $APP_OPT ./$EXE_NAME &

echo "$APP_NAME is started...."

sleep 1
#jps -l | grep $APP_NAME | grep -v grep |wc -l
ps -eaf | grep $APP_NAME | grep -v grep |wc -l
sleep 1
#jps -l | grep $APP_NAME | grep -v grep
ps -eaf | grep $APP_NAME | grep -v grep

