#!/usr/bin/env python3
import os

RECIPE_DIR = os.path.expanduser("~/lfs-repo/recipes")

def load_recipe(pkgname):
    path = os.path.join(RECIPE_DIR, f"{pkgname}.recipe.py")
    if not os.path.exists(path):
        raise FileNotFoundError(f"Recipe {pkgname}.recipe.py not found in {RECIPE_DIR}")
    recipe = {}
    with open(path, "r") as f:
        exec(f.read(), recipe)
    return recipe

def collect_deps(pkgname, seen=None):
    if seen is None:
        seen = set()
    if pkgname in seen:
        return set()
    seen.add(pkgname)

    try:
        recipe = load_recipe(pkgname)
    except FileNotFoundError:
        print(f"[warn] Recipe {pkgname} not found, skipping")
        return set()

    deps = set(recipe.get("deps", []))
    all_deps = set(deps)

    for dep in deps:
        all_deps |= collect_deps(dep, seen)
    return all_deps

if __name__ == "__main__":
    import sys

    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <package-name>")
        print(f"Example: {sys.argv[0]} cryptsetup")
        exit(1)

    pkg = sys.argv[1]
    deps = collect_deps(pkg)
    print(f"All dependencies for {pkg}:")
    for d in sorted(deps):
        print(f" - {d}")

