#!/bin/sh
# Starts the Elegance server if it is not answering.
# Run from cPanel → Cron Jobs every 5 minutes, and once at reboot:
#   */5 * * * * /bin/sh $HOME/elegance-app/keepalive.sh
#   @reboot     /bin/sh $HOME/elegance-app/keepalive.sh

for cand in python3.12 python3.11 python3.10 python3.9 python3; do
  if command -v "$cand" >/dev/null 2>&1; then
    PY="$cand"
    break
  fi
done
PY=${PYTHON:-${PY:-python3}}

cd "$HOME/elegance-app" || exit 1
if ! curl -fsS -m 5 -o /dev/null http://127.0.0.1:4173/api/content; then
  ELEGANCE_HOST=127.0.0.1 TRUST_PROXY=1 nohup "$PY" -u serve.py 4173 >> server.log 2>&1 &
fi
