###########################################
## GAP
###########################################

cd src

GAP_ROOT="$SAGE_LOCAL/share/gap"

# Enable debug info if requested.
# Note that -g3 allows you to use preprocessor macros in gdb which are widely used
if [ "$SAGE_DEBUG" = yes ] ; then
    export CFLAGS="-O0 -g3 -DDEBUG_MASTERPOINTERS -DDEBUG_GLOBAL_BAGS -DDEBUG_FUNCTIONS_BAGS $CFLAGS"
else
    # Default flags
    export CFLAGS="-O2 -g $CFLAGS"
fi

sdh_configure --with-gmp="$SAGE_LOCAL"
sdh_make -j1

# GAP's "make install" does not do everything correctly; in particular it
# mistakenly installs libtool's wrapper scripts rather than the actual gap
# binary
#
# Installing the headers and libgap works
sdh_make install-headers install-libgap

# Install config.h, which is not currently handled by `make install-headers`
sdh_install gen/config.h "$SAGE_LOCAL/include/gap"

# Now install the gap executable as "gap-bin"; it will be called normally
# through our wrapper script that sets the appropriate GAP_ROOT
SAGE_BIN="$SAGE_LOCAL/bin"
mkdir -p "$SAGE_DESTDIR$SAGE_BIN" || sdh_die "Failed to create the directory $SAGE_BIN"

./libtool --mode=install install gap "$SAGE_DESTDIR$SAGE_BIN/gap-bin" || \
    sdh_die "Failed to install gap-bin to $SAGE_BIN"

./libtool --mode=install install gac "$SAGE_DESTDIR$SAGE_BIN/gac" || \
    sdh_die "Failed to install gac to $SAGE_BIN"

# Now copy additional files GAP needs to run (and a few optional bits) into
# GAP_ROOT; we don't need everything from the source tree
sdh_install bin doc gen grp lib src tst sysinfo.gap "$GAP_ROOT"

# Install only the minimal packages GAP needs to run
sdh_install pkg/GAPDoc-* pkg/primgrp-* pkg/SmallGrp-* pkg/transgrp "$GAP_ROOT"/pkg

# Install additional packages that are not strictly required, but that are
# typically "expected" to be loaded: These are the default packages that are
# autoloaded at GAP startup (via the PackagesToLoad UserPreference) with an
# out-of-the-box GAP installation; see
# https://trac.sagemath.org/ticket/22626#comment:393 for discussion on this
#
# Also include atlasrep which is a dependency of tomlib
sdh_install \
    pkg/atlasrep \
    pkg/autpgrp-* \
    pkg/alnuth-* \
    pkg/crisp-* \
    pkg/ctbllib \
    pkg/FactInt-* \
    pkg/fga \
    pkg/irredsol-* \
    pkg/laguna-* \
    pkg/polenta-* \
    pkg/polycyclic-* \
    pkg/resclasses-* \
    pkg/sophus-* \
    pkg/tomlib-* \
    "$GAP_ROOT"/pkg

# Install the GAP startup script; ensure it is executable
sdh_install -T ../gap "$SAGE_BIN/gap"
chmod +x "$SAGE_DESTDIR$SAGE_BIN/gap"

# Create symlinks under $GAP_ROOT for these executables, as they are expected
# (especially when building kernel packages) to exist
ln -sf "$SAGE_BIN/gap-bin" "$SAGE_DESTDIR$GAP_ROOT/gap"
ln -sf "$SAGE_BIN/gac" "$SAGE_DESTDIR$GAP_ROOT/gac"

# Fix the $GAP_ROOT/bin/<arch>/src symlink to be relative (otherwise it links
# to the actual path of the sources GAP was compiled from)
for srclink in "$SAGE_DESTDIR$GAP_ROOT"/bin/*/src; do
    rm -f "$srclink"
    ln -s "../../src" "$srclink"
done

# TODO: This seems unnecessary--we are already installing all of doc/ to
# GAP_ROOT, which is necessary for some functionality in GAP to work.  Do
# we need this?  Maybe doc/gap could just be a symlink to gap/doc??
if [[ "$SAGE_SPKG_INSTALL_DOCS" = yes ]]; then
    # The (pre-built) HTML documentation is currently (GAP 4.6.3)
    # included, so we don't have to /build/ it here.
    # echo "Now building GAP's documentation..."
    # <COMMAND TO BUILD THE [HTML] DOCUMENTATION>
    echo "Now copying GAP's (HTML) documentation..."
    sdh_install doc/ref doc/tut "$SAGE_SHARE/doc/gap/"
fi
