#!/bin/sh

APP_HOME=$(dirname $0)

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

case $1 in
  api)
    APP_NAME=tsi-api-server
    APP_VERSION=0.0.1
    ;;
  app)
    APP_NAME=tsi-app-server
    APP_VERSION=0.0.1
    ;;
  sig)
    APP_NAME=tsi-sig-server
    APP_VERSION=0.0.1
    ;;
  web)
    APP_NAME=tsi-web-server
    APP_VERSION=0.0.1
    ;;
  *)
    usage
    ;;
esac


EXE_NAME=$APP_NAME-$APP_VERSION.jar
PID_NAME=$APP_NAME.pid
APP_PID=$APP_HOME/conf/$PID_NAME

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

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

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

