#!/data/data/com.termux/files/usr/bin/bash
# Foreground supervisor for Termux. Keep this terminal/session open; Android can still kill background apps.
set -u
trap 'echo "Stopping SPG Support Bot supervisor"; exit 0' INT TERM
cd "$(dirname "$0")"
mkdir -p logs
PYTHON="$PWD/.venv/bin/python"
if [ ! -x "$PYTHON" ]; then
  echo "Virtual environment missing. Run: bash install_termux.sh" >&2
  exit 1
fi
while true; do
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting SPG Support Bot" | tee -a logs/supervisor.log
  "$PYTHON" -u spg_support_bot.py 2>&1 | tee -a logs/spg_bot.log
  status=${PIPESTATUS[0]}
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] Bot exited with status ${status}; restarting in 5 seconds" | tee -a logs/supervisor.log
  sleep 5
done
