#!/usr/bin/env bash

# This script exists to handle the following situation:
# - In the past, a sysadmin created a script /usr/local/bin/sage
#   by copying /usr/local/sage-4.8/sage (say) and changing the
#   SAGE_ROOT line.
# - That sysadmin just installed sage-5.x (possibly by upgrading) and
#   he edited the SAGE_ROOT line in /usr/local/bin/sage, like he is
#   used to doing.
# - The old /usr/local/bin/sage script will try to execute
#   $SAGE_ROOT/local/bin/sage-sage
#   which is *this* script.
# - Since sage-sage isn't used anymore in sage-5.x, we print a warning
#   with a nice explanation and run spkg/bin/sage instead
#
# AUTHOR: Jeroen Demeyer (Trac #11073)
#

# Where is sage installed?
whichsage=`which sage 2>/dev/null`

if [ -n "$whichsage" ]; then
    guesssage="(this is probably '$whichsage')"
else
    guesssage="(this could be '/usr/local/bin/sage' for example)"
fi


# If this doesn't exist, probably our user ran Sage with an explicit
# path or is using a shell alias.


# Print a nice warning
cat >&2 <<EOF
Warning: you are using an old "sage" script to run Sage. This old
script calls sage-sage which is deprecated since sage-5.0.
The best solution is to delete this old "sage" script
$guesssage
and replace it with a symbolic link to "$SAGE_ROOT/sage".
You might need sysadmin privileges for this.

For more information, consult the Sage Installation Guide:
$SAGE_ROOT/devel/sage/doc/output/html/en/installation/source.html#installation-in-a-multiuser-environment
or online at:
http://sagemath.org/doc/installation/source.html#installation-in-a-multiuser-environment

For now, Sage will continue to work with the current setup, but this
might change in future versions.
**********************************************************************
EOF

# The $SAGE_ROOT/sage scripts of all Sage versions since at least
# sage-4.5 change directory to "$SAGE_ROOT" before running sage-sage.
# So, it is safe to assume that the current directory is "$SAGE_ROOT".
exec spkg/bin/sage "$@"
