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

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

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

# (Actually the currently generated Makefile won't use any of the above from the
# environment; instead we have to pass them with special options to 'configure',
# which we do below.  Furthermore, CPPFLAGS and CXXFLAGS are only supported by
# our patched 'makemakefile.py', which is called by 'configure'.)

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.

cd src/

# use 2to3 if we are running on python3
if python -c 'import sys; sys.exit(sys.version_info.major != 3)'; then
    2to3 -n -w --no-diffs makemakefile.py
fi

###############################################################################
# Configure, tune, build and test zn_poly:
###############################################################################

echo "Now configuring zn_poly..."
# Note: The '--cppflags' and '--cxxflags' options are added by our patch to
#       'makemakefile.py', and aren't available with vanilla upstream.
#       Moreover, the generated Makefile now takes CC, CXX and CPP from the
#       environment, so no need to pass them later explicitly to 'make'.
./configure --prefix="$SAGE_LOCAL" --cflags="$CFLAGS" --ldflags="$LDFLAGS" \
              --cppflags="$CPPFLAGS" --cxxflags="$CXXFLAGS" \
              --gmp-prefix="$SAGE_LOCAL" || sdh_die "Error configuring zn_poly."

echo "Now building zn_poly with its tuning parameters..."
sdh_make

# Run the brief test suite:
echo
echo "Now building and running zn_poly's quick self-test..."
sdh_make check

echo
echo "zn_poly's *quick* test suite passed."
if [ "$SAGE_CHECK" != yes ]; then
    echo "A more comprehensive test suite can be run if SAGE_CHECK is"
    echo "exported to \"yes\", but it takes about 10x as long to run."
fi

###############################################################################
# Build and manually install the shared library:
###############################################################################

echo
echo "Now building and installing zn_poly's shared library..."

case "$UNAME" in
"Darwin")
    sdh_make libzn_poly.dylib
    sdh_install libzn_poly.dylib "$SAGE_LOCAL/lib"
    install_name_tool -id ${SAGE_LOCAL}/lib/libzn_poly.dylib \
        "$SAGE_DESTDIR_LOCAL/lib/libzn_poly.dylib"
    ;;
"CYGWIN")
    sdh_make libzn_poly.dll.a
    sdh_install cygzn_poly.dll "$SAGE_LOCAL/bin"
    sdh_install libzn_poly*.dll.a "$SAGE_LOCAL/lib"
    ;;
*)
    # Linux, SunOS/Solaris, etc.:
    sdh_make libzn_poly.so
    sdh_install libzn_poly*.so "$SAGE_LOCAL/lib"
    ;;
esac

###############################################################################
# Manually install the header files:
###############################################################################

echo
echo "Now installing zn_poly's header files..."
sdh_install include/{zn_poly,wide_arith}.h "$SAGE_LOCAL/include/zn_poly"
echo "Finished installing zn_poly."
