#!/bin/sh

ACTIVE_PROFILE=wonju
#ACTIVE_PROFILE=wonju,mongodb
SERVICE_HOME=$(dirname $0)

usage() {
	echo "Usage:" $0 "[comm/data]"
	echo "RUN tsi-comm-server:" $0 "comm"
	echo "RUN tsi-data-broker:" $0 "data"
	exit
}

case $1 in
  comm)
    SERVICE_NAME=tsi-comm-server
    SERVICE_VERSION=0.0.1
    ;;
  data)
    SERVICE_NAME=tsi-data-broker
    SERVICE_VERSION=0.0.1
    ;;
  *)
    usage
    ;;
esac


EXE_NAME=$SERVICE_NAME-$SERVICE_VERSION.jar
PID_NAME=$SERVICE_NAME.pid
SERVICE_PID=$SERVICE_HOME/conf/$PID_NAME

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

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

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

echo "$SERVICE_NAME is started...."

sleep 1

ps -eaf | grep $SERVICE_NAME | grep -v grep |wc -l

sleep 1

ps -eaf | grep $SERVICE_NAME | grep -v grep

