#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

filter_runtime_warnings() {
  awk '
    /objc\[[0-9]+\]: Class .* is implemented in both/ { next }
    /This may cause spurious casting failures/ { next }
    /pygame\/pkgdata.py:.*pkg_resources is deprecated/ { next }
    /^[[:space:]]*from pkg_resources import resource_stream, resource_exists/ { next }
    /gymnasium\/spaces\/box.py:.*Casting input x to numpy array/ { next }
    /^[[:space:]]*gym\.logger\.warn\("Casting input x to numpy array\."\)/ { next }
    /WARNING:root:Unexpected key\(s\) when loading model:/ { next }
    { print; fflush() }
  '
}

should_launch_tui() {
  for arg in "$@"; do
    case "$arg" in
      --help|-h|--json-only|--health-only|--no-tui)
        return 1
        ;;
    esac
  done
  return 0
}

has_tui_arg() {
  for arg in "$@"; do
    case "$arg" in
      --tui)
        return 0
        ;;
    esac
  done
  return 1
}

is_help_request() {
  for arg in "$@"; do
    case "$arg" in
      --help|-h)
        return 0
        ;;
    esac
  done
  return 1
}

cd "$repo_root"
export PYGAME_HIDE_SUPPORT_PROMPT="${PYGAME_HIDE_SUPPORT_PROMPT:-1}"

if is_help_request "$@"; then
  exec uv run --python 3.13 worldforge-robotics-showcase "$@"
fi

showcase_args=("$@")
runtime_args=(
  --with "stable-worldmodel[train] @ git+https://github.com/galilai-group/stable-worldmodel.git"
  --with "datasets>=2.21"
  --with "huggingface_hub"
  --with "matplotlib"
  --with "lerobot[transformers-dep]"
  --with "pygame"
  --with "opencv-python"
  --with "imageio"
  --with "pymunk"
  --with "gymnasium"
  --with "shapely"
)

if should_launch_tui "$@"; then
  runtime_args+=(--with "textual>=8.2,<9")
  if ! has_tui_arg "$@"; then
    showcase_args=(--tui "$@")
  fi
fi

if [[ "${WORLDFORGE_SHOW_RUNTIME_WARNINGS:-}" == "1" ]]; then
  exec uv run --python 3.13 "${runtime_args[@]}" worldforge-robotics-showcase "${showcase_args[@]}"
fi

exec uv run --python 3.13 "${runtime_args[@]}" worldforge-robotics-showcase "${showcase_args[@]}" \
  2> >(filter_runtime_warnings >&2)
