#!/usr/bin/bash
# SCLS list subcommand — list installed packages
SCLS="$(cd "$(dirname "$0")/../../.." && pwd)"
REGISTRY="$SCLS/share/scls/registry"
CONFIG="$SCLS/share/scls/config.yaml"

if [[ ! -d "$REGISTRY" ]]; then
    echo "No packages installed."
    exit 0
fi

# Read flavor from config
SCLS_FLAVOR=""
SCLS_DESC=""
if [[ -f "$CONFIG" ]]; then
    while IFS= read -r line; do
        case "$line" in
            flavor:*)      SCLS_FLAVOR="${line#flavor: }" ;;
            description:*) SCLS_DESC="${line#description: }" ;;
        esac
    done < "$CONFIG"
    SCLS_DESC="${SCLS_DESC%\"}" ; SCLS_DESC="${SCLS_DESC#\"}"
fi

# Collect entries: name version license
entries=()
for f in "$REGISTRY"/*.yaml; do
    [[ -f "$f" ]] || continue
    name="" version="" license=""
    while IFS= read -r line; do
        case "$line" in
            name:*)    name="${line#name: }" ;;
            version:*) version="${line#version: }" ;;
            license:*) license="${line#license: }" ;;
        esac
    done < "$f"
    # Strip surrounding quotes (PyYAML may emit either '...' or "..." for
    # values that need quoting — version strings starting with a digit are a
    # common case). Apply both styles to every field for consistency with
    # scls info.
    for var in name version license; do
        val="${!var}"
        val="${val%\"}" ; val="${val#\"}"
        val="${val%\'}" ; val="${val#\'}"
        printf -v "$var" '%s' "$val"
    done
    # Shorten overly long combined license strings for display
    if [[ "$license" == *" AND "* ]]; then
        short=""
        for part in $license; do
            [[ "$part" == "AND" ]] && continue
            # Extract license family (e.g. LGPL from LGPL-2.1-or-later)
            family="${part%%-[0-9]*}"
            [[ -n "$short" ]] && short="$short/"
            short="$short$family"
        done
        license="Mixed ($short)"
    fi
    # Hide internal packages
    [[ "$name" == "environment" || "$name" == "_meta" ]] && continue
    [[ -n "$name" ]] && entries+=("$name|$version|$license")
done

if [[ ${#entries[@]} -eq 0 ]]; then
    echo "No packages installed."
    exit 0
fi

# Banner
echo ""
echo "     ▜"
echo " ▛▘▛▘▐ ▛▘    Scientific Core Library Stack 2026 [${SCLS_FLAVOR:-unknown}]"
echo " ▄▌▙▖▐▖▄▌    ${SCLS_DESC}"
echo ""

# Sort and print as a table
printf "%-25s %-17s %s\n" "Package" "Version" "License"
printf "%-25s %-17s %s\n" "-------" "-------" "-------"
printf '%s\n' "${entries[@]}" | sort | while IFS='|' read -r name version license; do
    printf "%-25s %-17s %s\n" "$name" "$version" "$license"
done
echo ""
echo "${#entries[@]} packages installed."

