#!/usr/bin/env bash
# From sage-spkg.
# For type=script packages, the build rule in build/make/Makefile sources
# sage-env but not sage-dist-helpers.
lib="$SAGE_ROOT/build/bin/sage-dist-helpers"
source "$lib"
if [ $? -ne 0 ]; then
    echo >&2 "Error: failed to source $lib"
    echo >&2 "Is $SAGE_ROOT the correct SAGE_ROOT?"
    exit 1
fi

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 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
export SAGE_BUILD_DIR=/doesnotexist

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

export SAGE_OPTIONAL_PACKAGES_WITH_EXTENSIONS=""

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-[1-9]*.egg-info sage-[1-9]*.dist-info)
    time 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_setup_bdist_wheel && sdh_store_wheel .
    fi
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)

    if [ "$SAGE_WHEELS" = yes ]; then
        # 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
        time sdh_pip_install --no-build-isolation .
    else
        SAGE_OPTIONAL_PACKAGES_WITH_EXTENSIONS+="mcqd,tdlib,coxeter3,sirocco,meataxe,bliss"
        time python3 -u setup.py --no-user-cfg build install || exit 1
    fi
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
