#!/usr/bin/env bash
if [ "$SAGE_EDITABLE" = yes ]; then
    cd "$SAGE_SRC"
else
    cd src
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 and SAGE_SPKG_INST.
## They are needed by src/sage_setup/optional_extension.py
## via 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_PKG_CONFIG_PATH=/doesnotexist
export SAGE_SPKG_SCRIPTS=/doesnotexist
export SAGE_SHARE=/doesnotexist

# Trac #29411: We cannot poison SAGE_LOCAL because the pkg-config script
# installed by the pkgconf spkg, generated from build/pkgs/pkgconf/patches/pkg-config.in,
# uses this variable.
# export SAGE_LOCAL=/doesnotexist

# 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
    # 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

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