import os
import json
from pathlib import Path

REPO_ROOT = Path(__file__).parent  # run this from your repo root

def find_packages():
    index = {}
    for item in REPO_ROOT.iterdir():
        if item.is_dir():
            metadata_path = item / "bbsbuild.json"
            if metadata_path.exists():
                with open(metadata_path) as f:
                    try:
                        meta = json.load(f)
                        index[meta["name"]] = {
                            "description": meta.get("description", ""),
                            "version": meta.get("version", "unknown")
                        }
                    except Exception as e:
                        print(f"Error parsing {metadata_path}: {e}")
    return index

def main():
    index = find_packages()
    with open(REPO_ROOT / "index.json", "w") as f:
        json.dump(index, f, indent=2)
    print("index.json generated successfully.")

if __name__ == "__main__":
    main()
