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

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

case "$(uname -s)" in
  Darwin) platform="macos" ;;
  Linux) platform="linux" ;;
  MINGW*|MSYS*|CYGWIN*) platform="windows" ;;
  *) platform="unknown" ;;
esac

if [[ "${platform}" == "unknown" ]]; then
  echo "Unsupported platform for bundling yt-dlp" >&2
  exit 1
fi

DEST="${ROOT}/apps/tauri/resources/yt-dlp/${platform}/venv"

if command -v uv >/dev/null 2>&1; then
  uv venv "${DEST}" --python 3.12
  "${DEST}/bin/python" -m pip install --upgrade pip
  "${DEST}/bin/python" -m pip install --upgrade yt-dlp
else
  if ! command -v python3 >/dev/null 2>&1; then
    echo "python3 not found; install python or uv" >&2
    exit 1
  fi
  python3 -m venv "${DEST}"
  "${DEST}/bin/python" -m pip install --upgrade pip
  "${DEST}/bin/python" -m pip install --upgrade yt-dlp
fi

echo "yt-dlp bundled at ${DEST}"
