#!/usr/bin/env bash
# ===========================================
# Setup local three.js libraries for GLB Viewer
# ===========================================

set -e

BASE_DIR="./libs/three"
EXAMPLES="$BASE_DIR/examples/jsm"

echo " Setting up local three.js libraries..."
mkdir -p "$BASE_DIR/build" "$EXAMPLES/controls" "$EXAMPLES/loaders" "$EXAMPLES/environments" "$EXAMPLES/libs/draco"

# Download core three.js module
echo " Downloading three.module.js..."
curl -L -o "$BASE_DIR/build/three.module.js" \
  https://unpkg.com/three@0.160.0/build/three.module.js

# Download required example modules
echo " Downloading OrbitControls..."
curl -L -o "$EXAMPLES/controls/OrbitControls.js" \
  https://unpkg.com/three@0.160.0/examples/jsm/controls/OrbitControls.js

echo " Downloading GLTFLoader..."
curl -L -o "$EXAMPLES/loaders/GLTFLoader.js" \
  https://unpkg.com/three@0.160.0/examples/jsm/loaders/GLTFLoader.js

echo " Downloading DRACOLoader..."
curl -L -o "$EXAMPLES/loaders/DRACOLoader.js" \
  https://unpkg.com/three@0.160.0/examples/jsm/loaders/DRACOLoader.js

echo " Downloading RoomEnvironment..."
curl -L -o "$EXAMPLES/environments/RoomEnvironment.js" \
  https://unpkg.com/three@0.160.0/examples/jsm/environments/RoomEnvironment.js

# Draco decoder files
echo " Downloading Draco decoders..."
for f in draco_decoder.js draco_decoder.wasm draco_wasm_wrapper.js; do
  curl -L -o "$EXAMPLES/libs/draco/$f" \
    "https://unpkg.com/three@0.160.0/examples/jsm/libs/draco/$f"
done

echo "All three.js files downloaded successfully!"
echo "You can now open index.html directly (even offline)."
