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

ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
OPENVT_DIR="$ROOT_DIR/vendor/open-vt"

if [ ! -d "$OPENVT_DIR" ]; then
  echo "OpenVT source not found at $OPENVT_DIR"
  echo "Run ./scripts/openvt-setup.sh first"
  exit 1
fi

cd "$OPENVT_DIR"

echo "Building OpenVT dependencies with local, non-mutating environment shims"

mkdir -p "$HOME/.local/bin"
cat > "$HOME/.local/bin/pyenv" <<'EOF'
#!/usr/bin/env bash
exit 0
EOF
chmod +x "$HOME/.local/bin/pyenv"

export PATH="$HOME/.local/bin:$PATH"
export PIP_BREAK_SYSTEM_PACKAGES=1
export CC_x86_64_pc_windows_gnu=x86_64-w64-mingw32-gcc
export CXX_x86_64_pc_windows_gnu=x86_64-w64-mingw32-g++
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc

if command -v rustup >/dev/null 2>&1; then
  rustup target add x86_64-pc-windows-gnu || true
fi

mkdir -p addons/gd-virtualcamera addons/keylogger

./build_dependencies.sh || true

echo
echo "Post-build status:"
find addons -maxdepth 3 -type f | sort
