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

if [ "$SAGE_PYTHON3" = yes ]; then
    echo "Skipping SageNB since it is not Python3 compatible"
    exit 0
fi

cd src

# Install a flat package (not an egg), which is the same as how pip
# installs packages.
sdh_pip_install .
if [ $? -ne 0 ]; then
    echo >&2 "Error installing SageNB"
    exit 1
fi

# let sagenb use mathjax
PYTHON_VERSION=$("$SAGE_LOCAL/bin/python2" -c 'import sys; print("%d.%d" % sys.version_info[:2])')
cd "${SAGE_LOCAL}/lib/python$PYTHON_VERSION/site-packages/sagenb/data"
if [ $? -ne 0 ]; then
    echo >&2 "Error: Cannot find SageNB data directory."
    exit 1
fi
ln -s ../../../../../share/mathjax/ mathjax
if [ ! -d mathjax ]; then
    echo >&2 "Error: Cannot symlink mathjax into the SageNB data directory."
    exit 1
fi
