#!/usr/bin/env bash

if [ "$SAGE_LOCAL" = "" ]; then
    echo >&2 "SAGE_LOCAL undefined ... exiting"
    echo >&2 "Maybe run 'sage -sh'?"
    exit 1
fi

cd src/

# sympy imports itself, and therefore it indirectly imports sage in
# setup.py.  We need to set PYTHONPATH=. and use the -S option to avoid
# importing the currently installed version of sympy, and to avoid
# importing sage.
export PYTHONPATH="."
echo "building sympy"
python -S setup.py build

if [ $? -ne 0 ]; then
    echo >&2 "Error building sympy"
    exit 1
fi

# It is very important that we have SAGE_LOCAL set, otherwise this
# might potentially delete stuff in /lib
echo "Deleting $SAGE_LOCAL/lib/python*/site-packages/sympy*"
rm -rf "$SAGE_LOCAL"/lib/python*/site-packages/sympy*

echo "installing sympy"
python -S setup.py install

if [ $? -ne 0 ]; then
    echo >&2 "Error installing sympy"
    exit 1
fi
