#!/usr/bin/env bash
if [ "$SAGE_EDITABLE" = yes ]; then
    cd "$SAGE_SRC"
else
    cd src
    # Trac #34181: Do not allow scripts with shebang lines from old
    # venvs leak into new venvs. (Changes only seem to be necessary
    # for non-editable builds.)
    rm -rf build/scripts-*
fi
## All sagelib-building is done by setup.py.
## This is so that sagelib can be installed by standard Python procedures,
## such as "./setup.py install" or "pip install ."
##
## We poison all environment variables that have paths to the sage source and build directories.
## In this way we make sure that all of the sagelib build's source paths are communicated through
## the current directory (for the source tree).
## Building takes places in the build/ subdirectory.
##
## As a special exception, we feed SAGE_PKGS.
## They are needed by src/sage/misc/package.py.  See meta-ticket #28815 for planned changes to this.

export SAGE_PKGS="$SAGE_ROOT"/build/pkgs
export SAGE_ROOT=/doesnotexist
export SAGE_SRC=/doesnotexist
export SAGE_SRC_ROOT=/doesnotexist
export SAGE_DOC_SRC=/doesnotexist
export SAGE_BUILD_DIR=/doesnotexist

export PYTHON="$SAGE_LOCAL/bin/python3"

# We also poison all directories below SAGE_LOCAL.
export SAGE_PKGCONFIG=/doesnotexist
export SAGE_SPKG_SCRIPTS=/doesnotexist
export SAGE_SHARE=/doesnotexist

# However, we only unset SAGE_LOCAL and SAGE_PKG_CONFIG_PATH.
# sage_setup.setenv receives them from sage_conf via sage.env;
# the poisoned environment variables would override them.
unset SAGE_LOCAL
unset SAGE_PKG_CONFIG_PATH

# Trac #30903: We cannot poison SAGE_DOC because setup.py installs the Jupyter kernel
# spec, which includes setting a symlink to the installed documentation.
# export SAGE_DOC=/doesnotexist

SITEPACKAGESDIR=$(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')

if [ "$SAGE_EDITABLE" = yes ]; then
    # In an incremental build, we may need to uninstall old versions installed by distutils
    # under the old distribution name "sage" (before #30912, which switched to setuptools
    # and renamed the distribution to "sagemath-standard").  There is no clean way to uninstall
    # them, so we just use rm.
    (cd "$SITEPACKAGESDIR" && rm -rf sage sage_setup sage-[1-9]*.egg-info sage-[1-9]*.dist-info)
    time python3 -m pip install --verbose --no-deps --no-index --no-build-isolation --isolated --editable . || exit 1
else
    # Make sure that an installed old version of sagelib in which sage is an ordinary package
    # does not shadow the namespace package sage during the build.
    (cd "$SITEPACKAGESDIR" && rm -f sage/__init__.py)
    # Likewise, we should remove the egg-link that may have been installed previously.
    (cd "$SITEPACKAGESDIR" && rm -f sagemath-standard.egg-link)
    time python3 -u setup.py --no-user-cfg build install || exit 1
fi

# Trac #33103: The temp.* directories are large after a full build.
# We remove them to save space; they are not needed for fast rebuilds.
rm -rf build/temp.*

if [ "$UNAME" = "CYGWIN" ]; then
    sage-rebase.sh "$SAGE_VENV" 2>/dev/null;
fi
