#!/bin/sh
#
#  sage-spkg-info SPKG
#  Format information about a Sage package
#
#  Assumes SAGE_ROOT is set
PKG_BASE=$1
PKG_SCRIPTS="$SAGE_ROOT/build/pkgs/$PKG_BASE"
for ext in rst txt; do
    SPKG_FILE="$PKG_SCRIPTS/SPKG.$ext"
    if [ -f "$SPKG_FILE" ]; then
        sed "1,3s/^ *Sage: Open Source Mathematics Software:/$PKG_BASE:/" "$SPKG_FILE"
        break
    fi
done
if [ -r "$PKG_SCRIPTS/type" ] ; then
    echo
    echo "Type"
    echo "----"
    echo
    cat "$PKG_SCRIPTS/type"
    echo
fi
echo
echo "Dependencies"
echo "------------"
echo
dep=
for dep_file in dependencies dependencies_order_only; do
    if [ -r "$PKG_SCRIPTS/$dep_file" ] ; then
        for dep in $(sed 's/^ *//; s/ *#.*//; q' "$PKG_SCRIPTS/$dep_file"); do
            case "$dep" in
                # Do not use order-only syntax, too much information
                \|)         ;;
                # Suppress dependencies on source file of the form $(SAGE_ROOT)/..., $(SAGE_SRC)/...
                \$\(SAGE_*) ;;
                # Suppress FORCE
                FORCE)      ;;
                # Dependencies like $(BLAS)
                \$\(*)      echo "- $dep";;
                # Looks like a package
                *)          if [ -n "$OUTPUT_RST" -a -r "$SAGE_ROOT/build/pkgs/$dep/SPKG.rst" ]; then
                                # This RST label is set in src/doc/bootstrap
                                echo "- :ref:\`spkg_$dep\`"
                            else
                                echo "- $dep"
                            fi;;
            esac
        done
    fi
done
echo
echo "Version Information"
echo "-------------------"
echo
for a in package-version.txt requirements.txt install-requires.txt; do
    if [ -f "$PKG_SCRIPTS"/"$a" ]; then
        echo "$a::"
        echo
        sed 's/^/    /' "$PKG_SCRIPTS/$a"
        echo
    fi
done
echo
echo "Equivalent System Packages"
echo "--------------------------"
echo
PKG_DISTROS="$PKG_SCRIPTS"/distros
systems=""
have_repology=no
for system_package_file in "$PKG_DISTROS"/*.txt; do
    system=$(basename "$system_package_file" .txt)
    if [ -f "$system_package_file" ]; then
        case "$system" in
            repology)
                have_repology=yes
                ;;
            *)
                systems="$systems $system"
                ;;
        esac
    fi
done
if [ $have_repology = yes ]; then
    systems="$systems repology"
fi
system=
for system in $systems; do
        system_package_file="$PKG_DISTROS"/$system.txt
        system_packages="$(echo $(sed 's/#.*//;' $system_package_file))"
        case $system in
            debian)
                # Generic
                echo "Debian/Ubuntu:"
                ;;
            fedora)
                # Generic
                echo "Fedora/Redhat/CentOS:"
                ;;
            repology)
                ;;
            *)
                echo "$system:"
                ;;
        esac
        sage-print-system-package-command $system --prompt='    $ ' --sudo install $system_packages
        echo
done
if [ -z "$system" ]; then
    echo "(none known)"
else
    echo
    SPKG_CONFIGURE="${PKG_SCRIPTS}/spkg-configure.m4"
    if [ -f "${SPKG_CONFIGURE}" ]; then
        if grep -q SAGE_PYTHON_PACKAGE_CHECK "${SPKG_CONFIGURE}"; then
            echo "If the system package is installed and if the (experimental) option"
            echo "--enable-system-site-packages is passed to ./configure, then ./configure"
            echo "will check if the system package can be used."
        else
            echo "If the system package is installed, ./configure will check if it can be used."
        fi
    else
        echo "However, these system packages will not be used for building Sage"
        echo "because spkg-configure.m4 has not been written for this package;"
        echo "see https://github.com/sagemath/sage/issues/27330"
    fi
fi
echo
