#!/usr/bin/env bash

########################################################################
# Regenerate auto-generated files, using information in SAGE_ROOT/build/
#
# This script is run by SAGE_ROOT/bootstrap as part of the bootstrapping phase
# (before configure, before creating source distributions).
#
# The BOOTSTRAP_QUIET variable is set by the top-level
# bootstrap script and controls how verbose we are.
########################################################################

set -e

if [ -z "$SAGE_ROOT" ]; then
    echo Please run the top-level bootstrap script of the Sage distribution.
    exit 1
fi

cd "$SAGE_ROOT"
export PATH=build/bin:$PATH

OUTPUT_DIR="src/doc/en/installation"
mkdir -p "$OUTPUT_DIR"

shopt -s extglob

RECOMMENDED_SPKG_PATTERN="@(_recommended$(for a in $(head -n 1 build/pkgs/_recommended/dependencies); do echo -n "|"$a; done))"
DEVELOP_SPKG_PATTERN="@(_develop$(for a in $(head -n 1 build/pkgs/_develop/dependencies); do echo -n "|"$a; done))"

for SYSTEM in arch debian fedora homebrew opensuse void; do
    SYSTEM_PACKAGES=
    OPTIONAL_SYSTEM_PACKAGES=
    SAGELIB_SYSTEM_PACKAGES=
    SAGELIB_OPTIONAL_SYSTEM_PACKAGES=
    RECOMMENDED_SYSTEM_PACKAGES=
    DEVELOP_SYSTEM_PACKAGES=
    for PKG_BASE in $(sage-package list --has-file distros/$SYSTEM.txt); do
                PKG_SCRIPTS=build/pkgs/$PKG_BASE
                PKG_TYPE=$(cat $PKG_SCRIPTS/type)
                PKG_SYSTEM_PACKAGES=$(sage-get-system-packages $SYSTEM $PKG_BASE)
                if [ -n "PKG_SYSTEM_PACKAGES" ]; then
                    if [ -f $PKG_SCRIPTS/spkg-configure.m4 ]; then
                       case "$PKG_BASE:$PKG_TYPE" in
                           *:standard)
                               SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                               ;;
                           $DEVELOP_SPKG_PATTERN:*)
                               DEVELOP_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                               ;;
                           $RECOMMENDED_SPKG_PATTERN:*)
                               RECOMMENDED_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                               ;;
                           *)
                               OPTIONAL_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                               ;;
                       esac
                    else
                        case "$PKG_BASE:$PKG_TYPE" in
                           $DEVELOP_SPKG_PATTERN:*)
                               DEVELOP_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                               ;;
                            *:standard)
                                SAGELIB_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                                ;;
                            *)
                                SAGELIB_OPTIONAL_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                                ;;
                        esac
                    fi
                fi
    done

    if [ "${BOOTSTRAP_QUIET}" = "no" ]; then
        echo >&2 $0:$LINENO: installing "$OUTPUT_DIR"/$SYSTEM"*.txt"
    fi
    echo "$(sage-print-system-package-command $SYSTEM --prompt --wrap --sudo install $(echo $(echo $SYSTEM_PACKAGES | xargs -n 1 echo | sort | uniq)))" > "$OUTPUT_DIR"/$SYSTEM.txt
    echo "$(sage-print-system-package-command $SYSTEM --prompt --wrap --sudo install $(echo $(echo $OPTIONAL_SYSTEM_PACKAGES | xargs -n 1 echo | sort | uniq)))" > "$OUTPUT_DIR"/$SYSTEM-optional.txt
    echo "$(sage-print-system-package-command $SYSTEM --prompt --wrap --sudo install $(echo $(echo $RECOMMENDED_SYSTEM_PACKAGES | xargs -n 1 echo | sort | uniq)))" > "$OUTPUT_DIR"/$SYSTEM-recommended.txt
    echo "$(sage-print-system-package-command $SYSTEM --prompt --wrap --sudo install $(echo $(echo $DEVELOP_SYSTEM_PACKAGES | xargs -n 1 echo | sort | uniq)))" > "$OUTPUT_DIR"/$SYSTEM-develop.txt
done

OUTPUT_DIR="src/doc/en/reference/spkg"
mkdir -p "$OUTPUT_DIR"
if [ "${BOOTSTRAP_QUIET}" = "no" ]; then
    echo >&2 $0:$LINENO: installing "$OUTPUT_DIR"/"*.rst"
fi
(cat <<EOF
Mathematics
~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --has-file math :standard: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done
cat <<EOF

Front-end, graphics, document preparation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --no-file math --has-file front-end :standard: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done
cat <<EOF

Other dependencies
~~~~~~~~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --no-file math --no-file front-end :standard: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done) > "$OUTPUT_DIR"/index_standard.rst
(cat <<EOF
Mathematics
~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --has-file math :optional: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done
cat <<EOF

Front-end, graphics, document preparation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --no-file math --has-file front-end :optional: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done
cat <<EOF

Other dependencies
~~~~~~~~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --no-file math --no-file front-end :optional: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done) > "$OUTPUT_DIR"/index_optional.rst
for PKG_BASE in $(sage-package list --has-file SPKG.rst | grep '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done > "$OUTPUT_DIR"/index_sagemath.rst
(cat <<EOF
Mathematics
~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --has-file math :experimental: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done

cat <<EOF

Other dependencies
~~~~~~~~~~~~~~~~~~

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst --no-file math :experimental: | grep -v '^sagemath_'); do
    echo "* :ref:\`spkg_$PKG_BASE\`"
done) > "$OUTPUT_DIR"/index_experimental.rst
(cat <<EOF

Details of external packages
============================

Packages are in alphabetical order.

.. default-role:: code

.. toctree::
   :maxdepth: 1

EOF
for PKG_BASE in $(sage-package list --has-file SPKG.rst); do
    echo "   $PKG_BASE"
done
cat <<EOF

.. default-role::

EOF
) > "$OUTPUT_DIR"/index_alph.rst
sage-package list --has-file SPKG.rst | OUTPUT_DIR=$OUTPUT_DIR OUTPUT_RST=1 xargs -P 99 -n 1 sage-spkg-info
