if [ "$SAGE_EDITABLE" = yes ]; then
    cd "$SAGE_SRC"
else
    cd src
    # Issue #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 github issue #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

# We also poison all directories below SAGE_LOCAL
# except for SAGE_SPKG_SCRIPTS, which is needed by sage-dist-helpers
export SAGE_PKGCONFIG=/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

# Issue #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"])')

# 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)
if [ "$SAGE_EDITABLE" = yes ]; then
    # Until https://github.com/sagemath/sage/issues/34209 switches us to PEP 660 editable wheels
    export SETUPTOOLS_ENABLE_FEATURES=legacy-editable
    sdh_pip_editable_install .

    if [ "$SAGE_WHEELS" = yes ]; then
        # Additionally build a wheel (for use in other venvs)
        cd $SAGE_PKGS/sagelib/src && time sdh_build_and_store_wheel --no-build-isolation .
    fi
else
    # Now implied: "$SAGE_WHEELS" = yes
    # We should remove the egg-link that may have been installed previously.
    (cd "$SITEPACKAGESDIR" && rm -f sagemath-standard.egg-link)
    # Use --no-build-isolation to avoid rebuilds because of dependencies:
    # Compiling sage/interfaces/sagespawn.pyx because it depends on /private/var/folders/38/wnh4gf1552g_crsjnv2vmmww0000gp/T/pip-build-env-609n5985/overlay/lib/python3.10/site-packages/Cython/Includes/posix/unistd.pxd
    sdh_pip_install --no-build-isolation .
fi

# Remove (potentially invalid) star import caches.
# lazy star imports are not used by the Sage library, but could still be used by
# downstream code.
python3 -c 'import pathlib; from sage.misc.lazy_import_cache import get_cache_file; pathlib.Path(get_cache_file()).unlink(missing_ok=True)'

# Issue #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.*
