#!/usr/bin/env bash

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

check_error() {
    if [[ $? -ne 0 ]]; then
        echo >&2 "***********************************************************"
        echo >&2 "Error:" $@
        echo >&2 "***********************************************************"
        exit 1
    fi
}

# Sometimes, ECL gives interactive prompts when something goes wrong
# during the build. Avoid this by redirecting stdin from /dev/null.
# See http://trac.sagemath.org/sage_trac/ticket/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).

export MAKE="$MAKE -j1"  # force sequential build (1 job)

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


CUR=`pwd`

cd src/

# Apply patches:
echo "Applying patches (if any)..."
for patch in ../patches/*.patch; do
    patch -p1 <"$patch"
    check_error "Patch '$patch' failed to apply."
done


# 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..."
./configure --prefix="$SAGE_LOCAL" --libdir="$SAGE_LOCAL/lib" --enable-ecl git_found=false
check_error "Failed to configure Maxima."


# Touching html and info files to avoid to regenerate them.
# This must be done after configuration since the timestamps need
# to be later than include-maxima.texi which is generated at
# configuration time:
echo
echo "Now touching documentation files..."
touch `find doc/info -name \*.info\* -o -name \*.html`
check_error "Failed to touch documentation files."


echo
echo "Now building Maxima..."
$MAKE
check_error "Failed to build Maxima."


echo
echo "Now installing Maxima..."
$MAKE install
check_error "Failed to install Maxima."


if [[ -f "$DOT_SAGE/maxima_commandlist_cache.sobj" ]]; then
    echo
    echo "Deleting Maxima commandlist cache."
    echo "It will be recreated at the next start of Sage."
    rm -f "$DOT_SAGE"/maxima_commandlist_cache.sobj
fi


# Build Maxima as a Lisp library
# and install it into ECL's library directory:
echo
echo "Now building Maxima as an ECL library..."
cd "$CUR"/src/src &&
mkdir -p ./lisp-cache &&
ecl \
  -eval '(require `asdf)' \
  -eval '(setf asdf::*user-cache* (truename "./lisp-cache"))' \
  -eval '(load "maxima-build.lisp")' \
  -eval '(asdf:make-build :maxima :type :fasl :move-here ".")' \
  -eval '(quit)'
check_error "Failed to build Maxima as an ECL library."

ECLLIB=`ecl -eval "(princ (SI:GET-LIBRARY-PATHNAME))" -eval "(quit)"`
echo
echo "Now installing the Maxima library as '$ECLLIB/maxima.fas'..."
PWD=`pwd`
if [[ -f "maxima.system.fasb" ]]; then
    echo "New ASDF encountered"
    cp -f maxima.system.fasb "$ECLLIB/maxima.fas"
    check_error "Failed to install '$PWD/maxima.system.fasb' as '$ECLLIB/maxima.fas'."
elif [[ -f "maxima.fasb" ]]; then
    echo "Old ASDF encountered"
    cp -f maxima.fasb "$ECLLIB/maxima.fas"
    check_error "Failed to install '$PWD/maxima.fasb' as '$ECLLIB/maxima.fas'."
else
    echo >&2 "Error: cannot find an .fasb file to install - exiting..."
    exit 1
fi


if [[ "$SAGE_SPKG_INSTALL_DOCS" = yes ]] ; then
    # Install a copy of the Maxima HTML documentation locally.
    cd "${CUR}/src"

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

    mkdir -p $MAXIMA_FIGURES

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