#!/bin/sh
# Starts TopoLive AI Edition in your browser: a local web server on http://localhost:8820 (serve.py, next to this file).
# It stops by itself an hour after the last TopoLive page is closed, or with File > Quit TopoLive.
#   topolive-ai --no-browser      start the server only
#   topolive-ai --port 8801       another port (the app's saved data is per port, so keep one port)
#   topolive-ai --forget-keys     AI Edition: delete the saved API keys
self=$(readlink -f "$0" 2>/dev/null || echo "$0")
here=$(dirname "$self")
py=$(command -v python3 2>/dev/null || true)
if [ -z "$py" ] || ! "$py" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)' 2>/dev/null; then
  msg="TopoLive AI Edition needs Python 3.8 or newer (python3). Install python3 with your distribution's package manager, then start TopoLive AI Edition again."
  echo "$msg" >&2
  if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
    if command -v zenity >/dev/null 2>&1; then zenity --error --title="TopoLive AI Edition" --text="$msg"
    elif command -v kdialog >/dev/null 2>&1; then kdialog --title "TopoLive AI Edition" --error "$msg"
    elif command -v notify-send >/dev/null 2>&1; then notify-send "TopoLive AI Edition" "$msg"
    fi
  fi
  exit 4
fi
# -I: Python's isolated mode (no PYTHON* settings, no user packages, not the script's folder on the import path)
exec "$py" -I "$here/serve.py" --port 8820 --idle-minutes 60 --edition ai "$@"
