#!/bin/bash
# gen-deps.sh - scan a AM·OS package tree and emit a package -> runtime
# dependency map based on ELF DT_NEEDED entries.
#
# Usage: gen-deps.sh [TREE] [OUTPUT]
#
# TREE    defaults to /repo/amos64
# OUTPUT  defaults to /repo/amos64/deps.conf
#
# Output format, one line per package:
#
#     package dep1 dep2 dep3 ...
#
# Packages with no cross-package runtime dependencies appear with just
# the package name. Sorted by package name.
#
# Environment:
#   QUIET=1   suppress the progress line
#
# The install script that consumes this file needs two operations:
#
#   1. Direct deps:   awk -v p=pkg '$1==p { for(i=2;i<=NF;i++) print $i }' deps.conf
#   2. Reverse deps:  awk -v p=pkg '$1!=p { for(i=2;i<=NF;i++) if($i==p) print $1 }' deps.conf
#
# DT_NEEDED is the ground truth for direct link dependencies. It does not
# capture runtime-only dependencies that are not expressed as sonames:
# dlopen() of plugins whose paths are constructed at runtime, NSS modules
# loaded by libc, PAM modules, Python/Perl/Ruby C extensions imported by
# name, and subprocess invocations of commands in another package.

set -euo pipefail
LC_ALL=C
TREE="${1:-/repo/amos64}"
OUT="${2:-/repo/amos64/deps.conf}"
QUIET="${QUIET:-0}"


if [ ! -d "$TREE" ]; then
    echo "error: tree not found: $TREE" >&2
    exit 1
fi

for tool in readelf tar awk find paste mktemp sort sed grep; do
    command -v "$tool" >/dev/null 2>&1 || {
        echo "error: required tool not found: $tool" >&2
        exit 1
    }
done

# Sonames provided by the base system. Every package implicitly depends on
# these; the install script does not resolve them. Extend if the base
# install changes.
SKIP_SONAMES=(
    # glibc
    libc.so.6 libm.so.6 libpthread.so.6 libdl.so.2 librt.so.1
    libresolv.so.2 libnsl.so.1 libutil.so.1 libanl.so.1
    libBrokenLocale.so.1 libmvec.so.1 libcrypt.so.1
    ld-linux-x86-64.so.2
    # gcc runtime. These ship in the gcc and gcc-g++ packages but are
    # present on every installed system as part of the toolchain the
    # base install ships. Reporting them per-package adds a spurious
    # gcc or gcc-g++ edge to every C++ binary in the tree.
    libgcc_s.so.1 libstdc++.so.6
    libgomp.so.1 libatomic.so.1 libitm.so.1
    libquadmath.so.0 libgfortran.so.5
    # glibc NSS modules. Built into libc or shipped as separate .so
    # files inside the glibc package. Always present on an installed
    # node; not a package-manager concern.
    libnss_dns.so.2 libnss_files.so.2 libnss_compat.so.2
    libnss_db.so.2 libnss_nis.so.2 libnss_nisplus.so.2 libnss_hesiod.so.2
     # Optional PAM modules (systemd/GNOME dependencies not in this tree)
    pam_ck_connector.so
    pam_fprintd.so
    pam_gnome_keyring.so
)
declare -A SKIP_MAP=()
for s in "${SKIP_SONAMES[@]}"; do
    SKIP_MAP[$s]=1
done

# NSS sources that are built into glibc or shipped by the base glibc
# package. Every AM·OS system has them; they are not external deps.
# Sources not on this list (mdns4_minimal, resolve, ldap, sss, ...)
# are resolved to libnss_<source>.so[.2] and must be provided by a
# package in the tree.
SKIP_NSS_SOURCES="files compat dns db nis nisplus hesiod"

is_skip_nss_source() {
    case " $SKIP_NSS_SOURCES " in
        *" $1 "*) return 0 ;;
        *) return 1 ;;
    esac
}
# ---- helpers -------------------------------------------------------------

pkg_name_from_txz() {
    local base
    base=$(basename "$1")
    base="${base%.txz}";  base="${base%.tgz}"
    base="${base%.tbz}";  base="${base%.tlz}"
    base="${base%-x86_64-*}"
    base="${base%-i686-*}"
    base="${base%-noarch-*}"
    printf '%s\n' "${base%%-[0-9]*}"
}

progress() {
    [ -t 2 ] || return 0
    [ "$QUIET" = "1" ] && return 0
    printf '\r  %-64s' "$1" >&2
}

progress_clear() {
    if [ -t 2 ]; then
        printf '\r\033[K' >&2
    fi
    return 0
}

# ---- work directory ------------------------------------------------------

WORK_DIR=$(mktemp -d /tmp/gen-deps.XXXXXX)

cleanup() {
    if [ -n "${WORK_DIR:-}" ] && [ -d "$WORK_DIR" ]; then
        rm -rf "$WORK_DIR"
    fi
    progress_clear
}
trap cleanup EXIT INT TERM

# ---- collect package list ------------------------------------------------

mapfile -t PKGS < <(
    find "$TREE" -maxdepth 2 -type f \
        \( -name '*.txz' -o -name '*.tgz' -o -name '*.tbz' -o -name '*.tlz' \) \
        | sort
)

if [ ${#PKGS[@]} -eq 0 ]; then
    echo "error: no packages found under $TREE" >&2
    exit 1
fi

n_pkgs=${#PKGS[@]}
echo "Scanning $n_pkgs packages under $TREE" >&2

# ---- single pass: extract each, collect provides and needs ---------------

declare -A PKG_PROVIDES=()
declare -A PKG_NEEDS=()
declare -A PKG_NAMES=()
declare -A DUP_PKG=()

idx=0
for txz in "${PKGS[@]}"; do
    idx=$((idx + 1))
    pkg=$(pkg_name_from_txz "$txz")

    if [ -n "${PKG_NAMES[$pkg]:-}" ] && [ "${PKG_NAMES[$pkg]}" != "$txz" ]; then
        if [ -z "${DUP_PKG[$pkg]:-}" ]; then
            DUP_PKG[$pkg]="${PKG_NAMES[$pkg]} | $txz"
        else
            DUP_PKG[$pkg]="${DUP_PKG[$pkg]} | $txz"
        fi
    fi
    PKG_NAMES[$pkg]="$txz"

    ext="$WORK_DIR/p$idx"
    mkdir -p "$ext"
    if ! tar xf "$txz" -C "$ext" 2>/dev/null; then
        progress_clear
        echo "warn: cannot extract $txz" >&2
        rm -rf "$ext"
        continue
    fi

    progress "scan: $idx/$n_pkgs  $pkg"

    declare -A provides=()
    declare -A needs=()

    while IFS= read -r -d '' f; do
        # Architecture gate: skip anything that is not x86_64 or i386 ELF.
        # Text files with the executable bit, or ELF files for other CPUs
        # (firmware blobs), reach this point and are filtered here.
        readelf -hW "$f" 2>/dev/null \
            | grep -qE 'Machine:.*(Advanced Micro Devices X86-64|Intel 80386)' \
            || continue

        # Dynamic section. Files without one (static binaries) contribute
        # no SONAME and no NEEDED, so skip.
        dyn=$(readelf -dW "$f" 2>/dev/null) || continue
        [ -n "$dyn" ] || continue

        # soname=$(awk '/SONAME/{gsub(/[][]/,"",$5); print $5; exit}' <<< "$dyn")
        soname=$(awk '/SONAME/ { sub(/.*\[/, ""); sub(/\].*/, ""); print; exit }' <<< "$dyn")
        if [ -n "$soname" ]; then
            provides[$soname]=1
        elif [[ "$f" == *.so || "$f" == *.so.* ]]; then
            # No SONAME: treat the library filename itself as the provided name.
            # This covers libraries such as libperl.so and libtcl8.6.so.
            base=$(basename "$f")
            provides[$base]=1
        fi

        while IFS= read -r need; do
            if [ -n "$need" ]; then
                needs[$need]=1
            fi
       # done < <(awk '/NEEDED/{gsub(/[][]/,"",$5); print $5}' <<< "$dyn")
        done < <(awk '/NEEDED/ { sub(/.*\[/, ""); sub(/\].*/, ""); print }' <<< "$dyn")
    done < <(
        find "$ext" -type f \
            \( -executable -o -name '*.so' -o -name '*.so.*' \) \
            -print0 2>/dev/null
    )

    if [ ${#provides[@]} -gt 0 ]; then
        PKG_PROVIDES[$pkg]=$(printf '%s\n' "${!provides[@]}" | sort | paste -sd' ' -)
    else
        PKG_PROVIDES[$pkg]=""
    fi
    if [ ${#needs[@]} -gt 0 ]; then
        PKG_NEEDS[$pkg]=$(printf '%s\n' "${!needs[@]}" | sort | paste -sd' ' -)
    else
        PKG_NEEDS[$pkg]=""
    fi

    rm -rf "$ext"
done

progress_clear

# ---- build soname -> provider map, detect conflicts ----------------------

declare -A SONAME_PKG=()
declare -A SONAME_PROVIDERS=()

for pkg in "${!PKG_PROVIDES[@]}"; do
    provides_str="${PKG_PROVIDES[$pkg]}"
    [ -n "$provides_str" ] || continue
    for s in $provides_str; do
        SONAME_PKG[$s]="$pkg"
        if [ -z "${SONAME_PROVIDERS[$s]:-}" ]; then
            SONAME_PROVIDERS[$s]="$pkg"
        else
            case " ${SONAME_PROVIDERS[$s]} " in
                *" $pkg "*) ;;
                *) SONAME_PROVIDERS[$s]="${SONAME_PROVIDERS[$s]} $pkg" ;;
            esac
        fi
    done
done

# ---- resolve deps for every package -------------------------------------

declare -A PKG_DEPS=()
declare -A MISSING=()

for pkg in "${!PKG_NAMES[@]}"; do
    needs_str="${PKG_NEEDS[$pkg]:-}"
    deps_list=""

    if [ -n "$needs_str" ]; then
        for need in $needs_str; do
            if [ -n "${SKIP_MAP[$need]:-}" ]; then
                continue
            fi
            dep="${SONAME_PKG[$need]:-}"
            if [ -z "$dep" ]; then
                MISSING[$need]=1
                continue
            fi
            if [ "$dep" = "$pkg" ]; then
                continue
            fi
            deps_list="$deps_list $dep"
        done
    fi

    if [ -n "$deps_list" ]; then
        PKG_DEPS[$pkg]=$(printf '%s\n' $deps_list | sort -u | paste -sd' ' -)
    else
        PKG_DEPS[$pkg]=""
    fi
done


# ---- Python pass: dist-info METADATA Requires-Dist -----------------------
#
# Pure-Python packages ship no ELF and no DT_NEEDED. Their runtime
# dependencies are declared in <pkg>.dist-info/METADATA as
# "Requires-Dist: <name>". The AM·OS package that provides a
# distribution named X is, by convention, python-X.
#
# Environment markers are skipped. A line like
#   Requires-Dist: pytest; extra == "test"
# is only installed with the [test] extra; not a runtime dep.
# A line with a comparison operator (Requires-Dist: MarkupSafe>=2.0)
# is a runtime dep; the version constraint is dropped, since the
# install script only needs to know "install this package too".
#
# PEP 503 normalization: lowercase and replace [-_.]+ with -. Applied
# to both the Requires-Dist name and the candidate package name so
# Foo_Bar, foo-bar, and foo.bar all match.

normalize_pypi_name() {
    printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[-_.]\+/-/g'
}

declare -A PYPI_TO_PKG=()
declare -A PKG_PAM_REFS=()
declare -A PKG_NSS_REFS=()

# Build reverse map: normalized python dist name -> package name
for pkg in "${!PKG_NAMES[@]}"; do
    case "$pkg" in
        python-*)
            dist="${pkg#python-}"
            norm=$(normalize_pypi_name "$dist")
            PYPI_TO_PKG[$norm]="$pkg"
            ;;
    esac
done

# Now scan each package for dist-info METADATA files and add deps
for pkg in "${!PKG_NAMES[@]}"; do
    txz="${PKG_NAMES[$pkg]}"
    ext="$WORK_DIR/metadata-$$"
    mkdir -p "$ext"
    tar xf "$txz" -C "$ext" 2>/dev/null || { rm -rf "$ext"; continue; }

    # Find every dist-info METADATA file. A package may ship more than one
    # (rare, but possible if it bundles vendored libs).
    while IFS= read -r -d '' meta; do
        while IFS= read -r line; do
            case "$line" in
                'Requires-Dist:'*)
                    spec="${line#Requires-Dist:}"
                    spec="${spec#"${spec%%[![:space:]]*}"}"   # ltrim
                    spec="${spec%"${spec##*[![:space:]]}"}"   # rtrim

                    # Skip extras-only requirements (not a runtime dependency)
                    case "$spec" in
                        *'extra =='*|*'; extra =='*|*';extra =='*) continue ;;
                    esac

                    # Drop environment marker if present
                    spec="${spec%%;*}"

                    # Drop extras
                    spec="${spec%%\[*}"

                    # Drop version spec, parentheses, or direct URL
                    name=$(printf '%s\n' "$spec" | sed -E 's/[[:space:]]*([<>=!~@]|\().*$//; s/[[:space:]]+$//')
                    name="${name#"${name%%[![:space:]]*}"}"
                    name="${name%"${name##*[![:space:]]}"}"

                    [ -z "$name" ] && continue

                    norm=$(normalize_pypi_name "$name")
                    dep="${PYPI_TO_PKG[$norm]:-}"
                    [ -z "$dep" ] && continue
                    [ "$dep" = "$pkg" ] && continue

                    # Append to the package's existing deps, if any
                    if [ -z "${PKG_DEPS[$pkg]:-}" ]; then
                        PKG_DEPS[$pkg]="$dep"
                    else
                        case " ${PKG_DEPS[$pkg]} " in
                            *" $dep "*) ;;
                            *) PKG_DEPS[$pkg]="${PKG_DEPS[$pkg]} $dep" ;;
                        esac
                    fi
                    ;;
            esac
        done < "$meta"
    done < <(
        find "$ext" -type f -path '*.dist-info/METADATA' -print0 2>/dev/null
    )


    # ---- PAM module references ----
    #
    # A pam.d line looks like:
    #     auth  required  pam_unix.so  try_first_pass
    # The module is the third whitespace-separated field. A leading
    # '-' marks the module as optional and is stripped. An absolute
    # path is reduced to its basename. Anything that does not end in
    # .so or .so.* is not a module reference (e.g. '@include').
    pamrefs=""
    while IFS= read -r -d '' pamfile; do
        while IFS= read -r pamline; do
            pamline="${pamline#"${pamline%%[![:space:]]*}"}"
            case "$pamline" in
                ''|'#'*|'@include'*) continue ;;
            esac
            pammod=$(printf '%s\n' "$pamline" | awk '{print $3}')
            [ -z "$pammod" ] && continue
            pammod="${pammod#-}"
            pammod=$(basename "$pammod")
            case "$pammod" in
                *.so|*.so.*) pamrefs="$pamrefs $pammod" ;;
            esac
        done < "$pamfile"
    done < <(find "$ext" -type f -path '*/etc/pam.d/*' -print0 2>/dev/null)

    if [ -n "$pamrefs" ]; then
        PKG_PAM_REFS[$pkg]=$(printf '%s\n' $pamrefs | sort -u | paste -sd' ' -)
    fi

    # ---- NSS source references ----
    #
    # nsswitch.conf lines look like:
    #     hosts: files dns [NOTFOUND=return] mdns4_minimal
    # Bracket annotations are skipped. Source names on the base list
    # (files, compat, dns, ...) are built into glibc; the rest are
    # resolved to libnss_<source>.so[.2] in the resolution pass.
    nssfile="$ext/etc/nsswitch.conf"
    if [ -f "$nssfile" ]; then
        nssrefs=""
        while IFS= read -r nssline; do
            nssline="${nssline#"${nssline%%[![:space:]]*}"}"
            case "$nssline" in
                ''|'#'*) continue ;;
            esac
            nssrest="${nssline#*:}"
            for src in $nssrest; do
                case "$src" in
                    \[*\]*) continue ;;
                esac
                is_skip_nss_source "$src" && continue
                nssrefs="$nssrefs $src"
            done
        done < "$nssfile"
        if [ -n "$nssrefs" ]; then
            PKG_NSS_REFS[$pkg]=$(printf '%s\n' $nssrefs | sort -u | paste -sd' ' -)
        fi
    fi
    rm -rf "$ext"
done

# ---- Resolve PAM and NSS references to package names --------------------
#
# PAM module references (pam_unix.so) resolve through SONAME_PKG, which
# already contains every .so basename in the tree via the fallback
# branch in the first pass.
#
# NSS source references (mdns4_minimal) resolve to libnss_<source>.so.2
# or libnss_<source>.so. glibc sources are already filtered out above.
#
# Unresolved references go to MISSING, same as unresolved sonames.

for pkg in "${!PKG_PAM_REFS[@]}"; do
    for pammod in ${PKG_PAM_REFS[$pkg]}; do
        # Skip optional modules that we know aren't in the tree
        if [ -n "${SKIP_MAP[$pammod]:-}" ]; then
            continue
        fi

        dep="${SONAME_PKG[$pammod]:-}"
        if [ -z "$dep" ]; then
            MISSING[$pammod]=1
            continue
        fi
        [ "$dep" = "$pkg" ] && continue
        if [ -z "${PKG_DEPS[$pkg]:-}" ]; then
            PKG_DEPS[$pkg]="$dep"
        else
            case " ${PKG_DEPS[$pkg]} " in
                *" $dep "*) ;;
                *) PKG_DEPS[$pkg]="${PKG_DEPS[$pkg]} $dep" ;;
            esac
        fi
    done
done

for pkg in "${!PKG_NSS_REFS[@]}"; do
    for src in ${PKG_NSS_REFS[$pkg]}; do
        dep="${SONAME_PKG[libnss_${src}.so.2]:-}"
        [ -z "$dep" ] && dep="${SONAME_PKG[libnss_${src}.so]:-}"
        if [ -z "$dep" ]; then
            MISSING["libnss_${src}.so"]=1
            continue
        fi
        [ "$dep" = "$pkg" ] && continue
        if [ -z "${PKG_DEPS[$pkg]:-}" ]; then
            PKG_DEPS[$pkg]="$dep"
        else
            case " ${PKG_DEPS[$pkg]} " in
                *" $dep "*) ;;
                *) PKG_DEPS[$pkg]="${PKG_DEPS[$pkg]} $dep" ;;
            esac
        fi
    done
done
# ---- emit ----------------------------------------------------------------

{
cat <<'HEADER'
# Auto-generated by gen-deps.sh. Do not edit by hand.
#
# Runtime dependency map for the AM·OS package tree. One line per package:
#
#     package dep1 dep2 dep3 ...
#
# Dependencies are resolved from:
#   - ELF DT_NEEDED entries: for every shared object and executable in
#     the package, the sonames it links against are mapped back to the
#     package that provides them.
#   - Python dist-info Requires-Dist declarations.
#   - PAM module references in /etc/pam.d/*.
#   - NSS source references in /etc/nsswitch.conf.
#
# Packages with no cross-package runtime dependencies appear with just
# the package name. Sorted by package name.
#
# Sonames provided by the base system (glibc, libgcc, libstdc++) are
# excluded. They are always present and are not a package-manager concern.
#
# This map does NOT capture:
#   - dlopen() of plugins whose paths are constructed at runtime
#   - Perl or Ruby C extensions imported by name
#   - subprocess invocations of commands in another package
#
# Those gaps are not derivable from data in the packages. The install
# script must either handle them with additional rules or leave them
# for the user to resolve manually.
HEADER

if [ ${#DUP_PKG[@]} -gt 0 ]; then
    echo "#"
    echo "# Duplicate package names. The map below uses the last file seen."
    for p in $(printf '%s\n' "${!DUP_PKG[@]}" | sort); do
        echo "#   $p: ${DUP_PKG[$p]}"
    done
fi

echo "#"

for pkg in $(printf '%s\n' "${!PKG_NAMES[@]}" | sort); do
    if [ -n "${PKG_DEPS[$pkg]:-}" ]; then
        printf '%s %s\n' "$pkg" "${PKG_DEPS[$pkg]}"
    else
        printf '%s\n' "$pkg"
    fi
done
} > "$OUT"

progress_clear
echo "Wrote $OUT" >&2

# ---- report --------------------------------------------------------------

if [ ${#MISSING[@]} -gt 0 ]; then
    echo >&2
    echo "Sonames referenced but not provided by any package in the tree:" >&2
    for s in $(printf '%s\n' "${!MISSING[@]}" | sort); do
        echo "  $s" >&2
    done
    echo >&2
    echo "Not necessarily errors. Common causes:" >&2
    echo "  - the soname comes from a base system package not in SKIP_SONAMES" >&2
    echo "  - the soname belongs to a package intentionally not in this tree" >&2
    echo "  - a library package was excluded from the scan" >&2
fi

n_total=$(awk '!/^#/' "$OUT" | wc -l)
n_total=$((n_total))
n_with_deps=$(awk '!/^#/ && NF > 1' "$OUT" | wc -l)
n_with_deps=$((n_with_deps))
echo "  $n_total packages, $n_with_deps with cross-package dependencies" >&2