#!/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"

STRIP_COMMENTS="sed s/#.*//;"
OUTPUT_DIR="src/doc/en/installation"
mkdir -p "$OUTPUT_DIR"

shopt -s extglob

for SYSTEM in arch debian fedora cygwin homebrew; do
    SYSTEM_PACKAGES=$(echo $(${STRIP_COMMENTS} build/pkgs/$SYSTEM.txt))
    OPTIONAL_SYSTEM_PACKAGES=
    for PKG_SCRIPTS in build/pkgs/*; do
        if [ -d $PKG_SCRIPTS ]; then
            PKG_BASE=$(basename $PKG_SCRIPTS)
            SYSTEM_PACKAGES_FILE=$PKG_SCRIPTS/distros/$SYSTEM.txt
            if [ ! -f $PKG_SCRIPTS/type ]; then
                echo >&2 "Warning: $PKG_SCRIPTS/type is missing."
                continue
            fi
            PKG_TYPE=$(cat $PKG_SCRIPTS/type)
            if [ -f $SYSTEM_PACKAGES_FILE -a -f $PKG_SCRIPTS/spkg-configure.m4 ]; then
               PKG_SYSTEM_PACKAGES=$(echo $(${STRIP_COMMENTS} $SYSTEM_PACKAGES_FILE))
               if [ -n "PKG_SYSTEM_PACKAGES" ]; then
                   case "$PKG_TYPE" in
                       standard)
                           SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                           ;;
                       *)
                           OPTIONAL_SYSTEM_PACKAGES+=" $PKG_SYSTEM_PACKAGES"
                           ;;
                   esac
               fi
            fi
        fi
    done
    if [ "${BOOTSTRAP_QUIET}" = "no" ]; then
	echo >&2 $0:$LINENO: installing "$OUTPUT_DIR"/$SYSTEM.txt and "$OUTPUT_DIR"/$SYSTEM-optional.txt
    fi
    echo "$(sage-print-system-package-command $SYSTEM --prompt --sudo install $(echo $(echo $SYSTEM_PACKAGES | xargs -n 1 echo | sort)))" > "$OUTPUT_DIR"/$SYSTEM.txt
    echo "$(sage-print-system-package-command $SYSTEM --prompt --sudo install $(echo $(echo $OPTIONAL_SYSTEM_PACKAGES | xargs -n 1 echo | sort)))" > "$OUTPUT_DIR"/$SYSTEM-optional.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
OUTPUT_INDEX="$OUTPUT_DIR"/index.rst
cat > "$OUTPUT_INDEX" <<EOF

External Packages
=================

.. toctree::
   :maxdepth: 1

EOF
for PKG_SCRIPTS in build/pkgs/*; do
    if [ -d "$PKG_SCRIPTS" ]; then
        PKG_BASE=$(basename "$PKG_SCRIPTS")
        if [ -f "$PKG_SCRIPTS"/SPKG.rst ]; then
            # Instead of just copying, we may want to call
            # a version of sage-spkg-info to format extra information.
            cp "$PKG_SCRIPTS"/SPKG.rst "$OUTPUT_DIR"/$PKG_BASE.rst
            echo >> "$OUTPUT_INDEX" "   $PKG_BASE"
        fi
    fi
done
cat >> "$OUTPUT_INDEX" <<EOF
.. include:: ../footer.txt
EOF

# #30064: Create the directory first
OUTPUT_DIR="src/doc/en/reference/repl"
mkdir -p "$OUTPUT_DIR"
OUTPUT="$OUTPUT_DIR/options.txt"
if [ "${BOOTSTRAP_QUIET}" = "no" ]; then
    echo >&2 $0:$LINENO: installing "$OUTPUT"
fi
./sage -advanced > "$OUTPUT"
