###############################################################################
#
#  zn_poly Sage check script
#
###############################################################################

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

###############################################################################
# Set up environment variables:
###############################################################################

if [ "$SAGE_DEBUG" = yes ]; then
    echo >&2 "Warning: Setting SAGE_DEBUG to 'yes' completely disables optimization."
    CFLAGS="$CFLAGS -O0 -g -fPIC"
    CXXFLAGS="$CXXFLAGS -O0 -g -fPIC"
else
    CFLAGS="-O3 -g $CFLAGS -fPIC"
    CXXFLAGS="-O3 -g $CXXFLAGS -fPIC"
fi

# Work around a bug in GCC 4.7.0 which breaks the build on Itanium CPUs with
# '-O3', '-O2' and '-O1' (cf. #12765, #12751, and the bug URL below.)
if [ "`uname -m`" = ia64 ] && [ "`testcc.sh $CC`" = GCC ]; then
    gcc_version=`$CC -dumpversion`
    case "$gcc_version" in
      4.7.*)
        CFLAGS="$CFLAGS -O0 -finline-functions -fschedule-insns"
        CXXFLAGS="$CXXFLAGS -O0 -finline-functions -fschedule-insns"
        echo >&2 "Warning: Disabling almost all optimization due to a bug in (at least)"
        echo >&2 "         GCC 4.7.0 on Itanium, which otherwise would break the build."
        echo >&2 "         See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48496"
        echo >&2 "         for current status and further details."
        echo >&2 "         (And please report to e.g. sage-devel in case you feel this bug"
        echo >&2 "          should already be fixed in GCC $gcc_version.)"
    esac
fi

export CFLAGS CPPFLAGS CXXFLAGS LDFLAGS # Partially redundant, but safe.

# Actually, these flags have been written to the Makefile during 'configure'.
# Only CC, CPP and CXX settings from the environment currently override the
# ones in the Makefile (which was generated from a patched 'makemakefile.py').
# -leif 04/2012

case "$UNAME" in
    SunOS)
      if ! (ld --version  2>&1 | grep GNU >/dev/null); then
          # Assume it's the Sun linker; change '-soname' to '-h':
          # The following is only supported by the Makefile generated by our
          # patched 'makemakefile.py':
          export SONAME_FLAG=-h
      fi;;
    *) unset SONAME_FLAG # Leave the Makefile default; for safety.
esac

unset SHARED_FLAG # Currently leave the Makefile default ('-shared'); for safety.

###############################################################################
# Build the 'test' program (if it's not already built) and run it:
###############################################################################

cd src/

# The methods for testing zn_poly are more complex than those of most other
# packages.  A 'make check' does some quick tests.  These are run from
# 'spkg-install' to check zn_poly is not obviously disfunctional.
#
# However, a more comprehensive set of tests can be run by first building a
# test program, then running that test program, which we do here.
#
# To quote from the file src/README:
#
# make check
#    Runs some quick tests to make sure that everything appears to be working.
#
# make test
#    Builds a test program. Running "test/test all" runs the whole zn_poly test
#    suite, which tests zn_poly much more thoroughly than "make check".

echo
echo "Now building zn_poly's extensive test suite (if not already built)..."
# $MAKE test CC="$CC" CXX="$CXX" # See comment above. We don't have to pass these.
$MAKE test # Make sure the test program is built.
if [ $? -ne 0 ] || [ ! -x test/test ]; then
    echo >&2 "Error: zn_poly failed to build its 'test' program,"
    echo >&2 "       so zn_poly's extensive test suite cannot be run."
    exit 1
fi

echo
echo "Now running zn_poly's extensive test suite..."
test/test all # Run the extensive test suite.
if [ $? -ne 0 ]; then
    echo >&2 "Error: zn_poly failed to pass its extensive test suite."
    exit 1
fi
echo "zn_poly has passed its extensive test suite."
