#!/bin/sh

APP_HOME=$HOME

. "$APP_HOME/conf/setenv.sh"

check_app() {
  APP_NAME=$1
  PID_FILE="$PID_DIR/$APP_NAME.pid"

  echo ""
  echo "[INFO] Checking status of $APP_NAME..."
  sleep 1

  if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if ps -p "$PID" > /dev/null 2>&1; then
      echo "[INFO] $APP_NAME is running (PID: $PID)"
    else
      echo "[INFO] $APP_NAME PID file exists but process not found"
    fi
  else
    COUNT=$(ps -eaf | grep java | grep "$APP_NAME" | grep -v grep | wc -l)
    if [ "$COUNT" -gt 0 ]; then
      echo "[INFO] $APP_NAME is running but no PID file found"
    else
      echo "[INFO] $APP_NAME is not running"
    fi
  fi
  echo ""
}

case "$1" in
  utic-stat-server)
    check_app "$1"
    ;;
  *)
    for APP in $APP_LIST; do
      check_app "$APP"
    done
    ;;
esac

echo ""
echo "[INFO] Checking process information..."
echo ""
for APP in $APP_LIST; do
  echo "Checking $APP"
  ps -eaf | grep "$APP" | grep -v grep | grep -v tail | grep java
done
echo ""

echo ""
echo "java run program.............."
echo ""
ps -eaf | grep java | grep -v grep | grep -v tail
echo ""
