#!/bin/sh

#APP_HOME=/tcs/itcs/bin
APP_HOME=/home/openvalue/bin

APP_NAME="-server-0.0.1.jar"
GITS_APP="ggits-comm-server"
ETLP_APP="ggits-etlp-server"
COMM_APP="sig-comm-server"
TODP_APP="sig-todp-server"
GITS_PID="$APP_HOME/conf/$GITS_APP.pid"
ETLP_PID="$APP_HOME/conf/$ETLP_APP.pid"
COMM_PID="$APP_HOME/conf/$COMM_APP.pid"
TODP_PID="$APP_HOME/conf/$TODP_APP.pid"

check_status() {
    PID_FILE=$1
    PROGRAM_NAME=$2

    if [ -f $PID_FILE ]; then
        PID=$(cat $PID_FILE)
        if ps -p $PID > /dev/null 2>&1; then
            echo "    >> $PROGRAM_NAME is running with PID $PID"
        else
            echo "    >> $PROGRAM_NAME is not running, but PID file exists"
        fi
    else
        echo "    >> $PROGRAM_NAME is not running"
    fi
}

case $1 in
  ggits)
    check_status $GITS_PID $GITS_APP
    ;;
  etlp)
    check_status $ETLP_PID $ETLP_APP
    ;;
  comm)
    check_status $COMM_PID $COMM_APP
    ;;
  todp)
    check_status $TODP_PID $TODP_APP
    ;;
  *)
    check_status $GITS_PID $GITS_APP
    check_status $ETLP_PID $ETLP_APP
    check_status $COMM_PID $COMM_APP
    check_status $TODP_PID $TODP_APP
    ;;
esac

sleep 1

ps -eaf | grep java | grep jar | grep $APP_NAME | grep -v grep | grep -v tail | grep -v kafka | grep -v vi | wc -l
sleep 1
ps -eaf | grep java | grep jar | grep $APP_NAME | grep -v grep | grep -v tail | grep -v kafka | grep -v vi
