#!/bin/sh

SERVICE_HOME=$(dirname $0)

usage() {
        echo "Usage:" $0 "[comm/data]"
        echo "STOP tsi-comm-server:" $0 "comm"
        echo "STOP 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

if [ ! -z "$SERVICE_PID" ]; then
  if [ -f "$SERVICE_PID" ]; then
    kill -15 `cat "$SERVICE_PID"` >/dev/null 2>&1
    echo "$SERVICE_NAME stopping.........."
  else
    echo "$SERVICE_NAME is not running...."
    exit
  fi
fi

LOOP=$(seq 0 9)
for i in $LOOP
do
  sleep 1
  if [ -f "$SERVICE_PID" ]; then
    echo "$SERVICE_NAME stopping.........."
  else
    echo "$SERVICE_NAME stopped.........."
    exit
  fi
done

echo "$SERVICE_NAME cannot be terminated......."
ps -eaf | grep $SERVICE_NAME | grep -v grep
