#!/bin/bash
set -e

# Check if Elasticsearch is running
if [ -f /app/data/elasticsearch.pid ]; then
    echo "Stopping Elasticsearch..."
    PID=$(cat /app/data/elasticsearch.pid)
    if kill -0 "$PID" 2>/dev/null; then
        kill "$PID"
        echo "Waiting for Elasticsearch to stop..."
        # Wait for process to end
        while kill -0 "$PID" 2>/dev/null; do
            sleep 1
        done
    else
        echo "Elasticsearch process not found, cleaning up PID file."
    fi
    rm -f /app/data/elasticsearch.pid
else
    echo "Elasticsearch PID file not found, nothing to stop."
fi

echo "Elasticsearch stopped."