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

# Apply fixes to upstream source
cd src

# Exit on error
set -e

# Build in a separate directory, not in src/ (a.k.a. a VPATH build).
# This is the recommended way to build GCC.
cd ..
mkdir gcc-build
cd gcc-build


# The ld (linker) on old Darwin systems doesn't understand
# -compatibility_version, it needs -dylib_compatibility_version.
# Similarly for a few more options.  We create an ld wrapper to fix
# these command line options.
if { uname -sr | grep 'Darwin [0-9]\.' ;} &>/dev/null; then
    rm -f "$SAGE_LOCAL/bin/ld"
    LD=`which "${LD:-ld}"`

    echo '#!/bin/bash' >>"$SAGE_LOCAL/bin/ld"
    echo '# ld wrapper generated by the GCC spkg' >>"$SAGE_LOCAL/bin/ld"
    echo >>"$SAGE_LOCAL/bin/ld"
    # Hardcode the path to the "real" ld.
    echo "LD=$LD" >>"$SAGE_LOCAL/bin/ld"

cat >>"$SAGE_LOCAL/bin/ld" <<'EOF'

for arg in "$@"; do
    arg=`echo "$arg" | sed \
        -e 's/^-\(compatibility_version\)/-dylib_\1/' \
        -e 's/^-\(current_version\)/-dylib_\1/' \
        -e 's/^-\(install_name\)/-dylib_\1/' \
    `
    argv=("${argv[@]}" "$arg")
done

exec "$LD" "${argv[@]}"
EOF

    chmod +x "$SAGE_LOCAL/bin/ld"

    # Make GCC use this ld wrapper (found in the $PATH)
    export LD=ld
fi

# On OS X 10.9, g++ and the cdefs.h header are currently incompatible
# Temporary workaround posted at http://trac.macports.org/ticket/41033
if { uname -sr | grep 'Darwin 13\.' ;} &>/dev/null; then
    mkdir -p "$SAGE_LOCAL/include/sys"
    sed 's+defined(__GNUC_STDC_INLINE__)+& \&\& !defined(__cplusplus)+' /usr/include/sys/cdefs.h > "$SAGE_LOCAL/include/sys/cdefs.h"
fi

# On OS X 10.10 there is random ObjC stuff in a C header
if { uname -sr | grep 'Darwin 14\.' ;} &>/dev/null; then
    if [ -f /usr/include/dispatch/object.h ]; then
        mkdir -p "$SAGE_LOCAL/include/dispatch"
        sed 's+typedef void (\^dispatch_block_t)(void)+typedef void* dispatch_block_t+' \
            /usr/include/dispatch/object.h > "$SAGE_LOCAL/include/dispatch/object.h"
    else
        echo "Warning: header /usr/include/dispatch/object.h not found, not applying fix."
    fi
fi

# On OSX:
if [ "$UNAME" = "Darwin" ]; then
    # isl/cloog libraries are almost certainly from Homebrew and won't work
    GCC_CONFIGURE="--without-isl --without-cloog $GCC_CONFIGURE"

    # Use 'bootstrap-debug' build configuration to force stripping of object
    # files prior to comparison during bootstrap (broken by Xcode 6.3).
    # See #18156
    GCC_CONFIGURE="--with-build-config=bootstrap-debug $GCC_CONFIGURE"
fi

# Let GCC build on Raspberry Pi using hard floats.
if [ `uname -m` = "armv6l" ]; then
    GCC_CONFIGURE="--with-arch=armv6 --with-fpu=vfp --with-float=hard $GCC_CONFIGURE"
fi

# Let GCC build on more recent ARM boards using hard floats.
if [ `uname -m` = "armv7l" ]; then
    GCC_CONFIGURE="--with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard $GCC_CONFIGURE"
fi

if [ "$SAGE_CHECK" = yes ]; then
    # Enable internal checks in GCC.  These checks do not affect the
    # binaries produced by GCC, but they do increase the compile time
    # of everything compiled with GCC.
    GCC_CONFIGURE="$GCC_CONFIGURE --enable-checking=yes"
fi

# Use the assembler/linker specified by $AS/$LD if they differ from the
# default.
if [ -n "$AS" -a "$AS" != "as" ]; then
    CONFIGURE_AS="--with-as=$AS"
fi
if [ -n "$LD" -a "$LD" != "ld" ]; then
    CONFIGURE_LD="--with-ld=$LD"
fi

../src/configure \
    --prefix="$SAGE_LOCAL" \
    --with-local-prefix="$SAGE_LOCAL" \
    --with-gmp="$SAGE_LOCAL" --with-mpfr="$SAGE_LOCAL" --with-mpc="$SAGE_LOCAL" \
    --with-system-zlib \
    --disable-multilib \
    --disable-nls \
    --enable-languages=c,c++,fortran \
    --disable-libitm \
    $GCC_CONFIGURE "$CONFIGURE_AS" "$CONFIGURE_LD"

$MAKE BOOT_LDFLAGS="-Wl,-rpath,$SAGE_LOCAL/lib"

$MAKE install


# Force re-installation of gmp, mpir, mpfr and mpc with the GCC that we
# just built. This is needed in particular because gmp/mpir was first
# built without C++ support. Also mark gfortran as not installed in case
# somebody first built gfortran and then GCC.
cd "$SAGE_SPKG_INST"
rm -f gmp-* mpir-* mpfr-* mpc-* gfortran-*

# Force re-configuration: the next time that "make" is run, we need to
# rebuild all packages (#24703) but we should not rebuild gcc (#19324)
touch "$SAGE_ROOT/configure"
