# Sometimes, ECL gives interactive prompts when something goes wrong
# during the build. Avoid this by redirecting stdin from /dev/null.
# See https://github.com/sagemath/sage/issues/11884#comment:34
exec </dev/null


# We don't have to set up CFLAGS etc., since these are taken from ECL
# (i.e., ECL uses the ones that were specified when ECL was built).
# We unset them to avoid passing another copy to the linker,
# which is overquoted in some cases, see Issue #29606.
unset CFLAGS
unset LDFLAGS

# workaround for configure braindamage, see Issue #15546
export EMACS=no


cd src/

# Use newer version of config.guess and config.sub (see Issue #19734)
cp "$SAGE_ROOT"/config/config.* .

# Patch out bad 'multiple targets' rule
# https://github.com/sagemath/sage/pull/35619#issuecomment-1567351409
sed -i.bak 's/^maxima_singlepage.html //' doc/info/Makefile.in

# Note that maxima configure checks for git and, if it finds it, uses
# versions information from the repo. See #15529. We disable this with
# git_found=false

echo
echo "Now configuring Maxima..."
sdh_configure --enable-ecl git_found=false

#---------------------------------------------------------------

sdh_make
sdh_make_install


# Install Maxima into ECL's library directory:
# Ensure that the $ECLLIB directory exists in
# case we're using ECL from the system.
ECLLIB="${SAGE_LOCAL}/lib/ecl"
echo
echo "Now installing the Maxima library as '$ECLLIB/maxima.fas'..."
mkdir -p "${ECLLIB}"
cp -f "src/binary-ecl/maxima.fas" "$ECLLIB/maxima.fas" \
    || sdh_die "Failed to install 'src/binary-ecl/maxima.fas' as '$ECLLIB/maxima.fas'."


if [[ "$SAGE_SPKG_INSTALL_DOCS" = yes ]] ; then
    # Install a copy of the Maxima HTML documentation locally.

    # The Maxima HTML docs are already built, we just need to copy
    # them to the sage installation.
    MAXIMA_DOCS=$SAGE_SHARE/doc/maxima
    MAXIMA_FIGURES=$MAXIMA_DOCS/figures

    mkdir -p $MAXIMA_FIGURES

    # The HTML documentation only uses the PNG figures, not the PDF
    # ones.
    cp doc/info/*.html        $MAXIMA_DOCS
    cp doc/info/figures/*.png $MAXIMA_FIGURES
fi
