cd src

if [ "x$SAGE_DEBUG" = "xyes" ] ; then
    CFLAGS="-g -O0 $CFLAGS"
    CXXFLAGS="-g -O0 $CXXFLAGS"
else
    CFLAGS="-g -O2 $CFLAGS"
    CXXFLAGS="-g -O2 $CXXFLAGS"
fi

if [ "$UNAME" = "CYGWIN" ]; then
    # Some of ECL's sources rely on GNU-isms that are allowed by default on
    # most glibcs, but not in newlib; https://trac.sagemath.org/ticket/25057
    CFLAGS="$CFLAGS -D_GNU_SOURCE"
    CXXFLAGS="$CXXFLAGS -D_GNU_SOURCE"
fi

export CFLAGS
export CXXFLAGS
export LDFLAGS


# These are all used by GNU to specify compilers.
echo "Using CC=$CC"
echo "Using CXX=$CXX"

# Flags which may be set.
echo "The following environment variables will be exported"
echo "Using CFLAGS=$CFLAGS"
echo "Using CXXFLAGS=$CXXFLAGS"
echo "Using CPPFLAGS=$CPPFLAGS"
echo "Using LDFLAGS=$LDFLAGS"
echo "configure scripts and/or makefiles might override these later"
echo ""


# Use newer version of config.guess and config.sub (see Trac #19732)
cp "$SAGE_ROOT"/config/config.* src

sdh_configure --with-gmp-prefix=$SAGE_LOCAL --disable-threads \
              --enable-unicode=yes $ECL_CONFIGURE

# Before running make we touch build/TAGS so its building process is never triggered
touch build/TAGS

# Ensure that ECL will not ask interactive questions (for example, when
# pressing CTRL-C during the build)
exec </dev/null

sdh_make

# Remove old install of ECL, if any.
rm -rf "$SAGE_LOCAL/lib/ecl-"*
if [ $? -ne 0 ]; then
    echo >&2 "Error - Failed to remove old ECL install ... exiting"
    exit 1
fi

sdh_make_install

# Create symbolic link to lib/ecl-version directory.
# Also create a symbolic link lib/ecl/ecl -> include/ecl.
# This is important when the Sage install is moved, see Trac #14662.
cd "$SAGE_DESTDIR$SAGE_LOCAL/lib/" && rm -f ecl && ln -s ecl-* ecl
if [ $? -ne 0 ]; then
    echo >&2 "Error - Failed to create symbolic link to ECL library"
    echo >&2 "directory ... exiting"
    exit 1
fi
cd "$SAGE_DESTDIR$SAGE_LOCAL/lib/ecl" && rm -f ecl && ln -s ../../include/ecl ecl
if [ $? -ne 0 ]; then
    echo >&2 "Error - Failed to create symbolic link to ECL include"
    echo >&2 "directory ... exiting"
    exit 1
fi
