#!/bin/bash
# Generate rustdoc for shared library crates.
# Output goes to rustdoc-out/ (relative to MNW/server/).
# Run from the MNW/server directory.

set -euo pipefail

if [ ! -f "Cargo.toml" ]; then
    echo "Error: Run this script from the MNW/server directory"
    exit 1
fi

OUT_DIR="$(pwd)/rustdoc-out"
SHARED_DIR="$(cd ../shared && pwd)"

CRATES=("synckit-client" "docengine" "tagtree" "s3-storage" "theme-common")

rm -rf "$OUT_DIR"
mkdir -p "$OUT_DIR"

for crate in "${CRATES[@]}"; do
    crate_dir="$SHARED_DIR/$crate"
    if [ ! -d "$crate_dir" ]; then
        echo "Warning: $crate_dir not found, skipping"
        continue
    fi

    echo "Generating docs for $crate..."
    (cd "$crate_dir" && cargo doc --no-deps --target-dir "$OUT_DIR/.target" 2>&1 | tail -1)
done

# Move generated docs from target/doc/ to output root
if [ -d "$OUT_DIR/.target/doc" ]; then
    cp -r "$OUT_DIR/.target/doc/"* "$OUT_DIR/"
    rm -rf "$OUT_DIR/.target"
fi

echo ""
echo "Rustdoc generated in $OUT_DIR/"
ls -1 "$OUT_DIR/" | head -20
