#!/bin/bash
# Custom Cargo Subcommand for formatting both Rust and Vx files
#
# =====================================================================
# SETUP INSTRUCTIONS
# =====================================================================
# To use this custom subcommand (`cargo format`), Cargo needs to find 
# this executable in your PATH under the name `cargo-format`.
#
# A symlink to this script is automatically provided in `.cargo/bin/`.
# Simply add the local `.cargo/bin` directory to your PATH:
#
#   export PATH="$PWD/.cargo/bin:$PATH"
#
# Once added, you can run:
#   cargo format
# =====================================================================

echo "[1/3] Running cargo fmt for Rust files..."
cargo fmt

echo "[2/3] Running vx-format for Vx files..."
cargo build --release --quiet --bin vx-format
find . -type d \( -name target -o -name .git \) -prune -o -name "*.vx" -exec ./target/release/vx-format {} +

echo "[3/3] Running mdformat for Markdown files..."
if git status --porcelain | grep -q '\.md$'; then
    echo "⚠️  Uncommitted Markdown changes detected. Skipping mdformat to prevent accidental data loss."
else
    if command -v mdformat &> /dev/null; then
        mdformat docs/ README.md
    else
        echo "mdformat not found. Skipping markdown formatting. (Install with: pipx install mdformat)"
    fi
fi

echo "Formatting complete!"
