#!/bin/bash

# Stop the player and watchdog
# Use this to gracefully shutdown the player

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "=========================================="
echo "Stopping Kivy Signage Player"
echo "=========================================="
echo ""

# Kill watchdog (start.sh)
echo "Stopping watchdog..."
pkill -f "bash.*start.sh"

# Kill player
echo "Stopping player..."
pkill -f "python3 main.py"

# Give processes time to exit gracefully
sleep 2

# Force kill if still running
pkill -9 -f "bash.*start.sh" 2>/dev/null
pkill -9 -f "python3 main.py" 2>/dev/null

# Clean up heartbeat and stop flag files
rm -f "$SCRIPT_DIR/.player_heartbeat"
rm -f "$SCRIPT_DIR/.player_stop_requested"

echo ""
echo "✓ Player and watchdog stopped"
echo ""
