#!/usr/bin/env bash
# Offline installer for the gradio-template bundle (RHEL 9.6, no network).
#
#   scripts/install-gradio.sh 3.9  [/path/to/venv]
#   scripts/install-gradio.sh 3.12 [/path/to/venv]
#
# Installs gradio and its full dependency closure from the bundled wheels only.
# Nothing is fetched from the network: pip runs with --no-index.
set -euo pipefail

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

usage() { sed -n '2,12p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 1; }
[[ $# -ge 1 ]] || usage

PYVER="$1"
case "$PYVER" in
  3.9|39)   TAG=py39  ; PYBIN=python3.9  ;;
  3.12|312) TAG=py312 ; PYBIN=python3.12 ;;
  *) echo "ERROR: unsupported Python version '$PYVER' (expected 3.9 or 3.12)" >&2; usage ;;
esac
VENV="${2:-$BUNDLE/venv-$TAG}"

WHEELS="$BUNDLE/wheels/$TAG"
BOOT="$BUNDLE/bootstrap/$TAG"
LOCK="$BUNDLE/requirements/gradio-$TAG.lock.txt"

for p in "$WHEELS" "$BOOT" "$LOCK"; do
  [[ -e "$p" ]] || { echo "ERROR: missing bundle path: $p" >&2; exit 1; }
done

# --- locate the interpreter -------------------------------------------------
if ! command -v "$PYBIN" >/dev/null 2>&1; then
  cat >&2 <<EOF
ERROR: $PYBIN not found on this host.

Install it from the RHEL 9.6 repositories you already have available
(this bundle deliberately ships no RPMs -- see docs/PREREQUISITES.md):

  Python 3.9  : dnf install python3 python3-devel            # RHEL 9 default
  Python 3.12 : dnf install python3.12 python3.12-pip python3.12-devel
EOF
  exit 1
fi

echo "==> interpreter : $($PYBIN -V 2>&1) ($(command -v "$PYBIN"))"
echo "==> bundle      : $BUNDLE"
echo "==> target venv : $VENV"

# --- create the venv --------------------------------------------------------
# --without-pip keeps us off ensurepip's bundled (older) pip; we install the
# bundled pip wheel explicitly instead.
if [[ -d "$VENV" ]]; then
  echo "==> reusing existing venv"
else
  echo "==> creating venv"
  "$PYBIN" -m venv --without-pip "$VENV"
fi

# --- bootstrap pip / setuptools / wheel -------------------------------------
if [[ ! -x "$VENV/bin/pip" ]]; then
  echo "==> bootstrapping pip, setuptools, wheel"
  PIPWHL="$(ls "$BOOT"/pip-*.whl | head -1)"
  # A pip wheel is importable directly: run pip from inside the wheel itself.
  "$VENV/bin/python" "$PIPWHL/pip" install --no-index --find-links "$BOOT" \
      pip setuptools wheel
fi

# --- install gradio ---------------------------------------------------------
echo "==> installing gradio from $WHEELS (offline)"
"$VENV/bin/pip" install --no-index --find-links "$WHEELS" -r "$LOCK"

# --- report -----------------------------------------------------------------
echo
echo "==> installed:"
"$VENV/bin/python" - <<'PY'
import gradio, gradio_client, fastapi, pydantic, sys
print(f"    python        {sys.version.split()[0]}")
print(f"    gradio        {gradio.__version__}")
print(f"    gradio_client {gradio_client.__version__}")
print(f"    fastapi       {fastapi.__version__}")
print(f"    pydantic      {pydantic.VERSION}")
PY

cat <<EOF

Done. To use it:

  source $VENV/bin/activate
  source $BUNDLE/gradio-offline.env      # keeps gradio from phoning home
  python $BUNDLE/templates/01_hello_interface.py

EOF
