#!/bin/bash
# fempkg - simple binpkg index generator (bare names only)

REPO_DIR="/home/gabry/lfs-repo/binpkg"
INDEX_FILE="$REPO_DIR/index.txt"

echo "[fempkg] Generating index in $REPO_DIR"

# Clean old index
: > "$INDEX_FILE"
# List of packages to force into the index exactly as given
FORCE_PKGS=(
    "re2"
    "ncurses"
    "lm-sensors"
    "linux-firmware"
    "librewolf"
    "Elegant-mojave-float-left-light"
)

for pkg in "$REPO_DIR"/*.tar.zst; do
    [ -e "$pkg" ] || continue
    fname=$(basename "$pkg")
    name="${fname%.tar.zst}"

    # strip version/date info
    pkgname=$(echo "$name" | sed -E 's/-[0-9][^-]*$//')

    # if pkgname is in FORCE_PKGS, use that exact name
    for f in "${FORCE_PKGS[@]}"; do
        if [[ "$pkgname" == "$f"* ]]; then
            pkgname="$f"
            break
        fi
    done

    echo "$pkgname" >> "$INDEX_FILE"
done

# Remove duplicates and sort
sort -u "$INDEX_FILE" -o "$INDEX_FILE"

echo "[fempkg] Index generated: $INDEX_FILE"

