#! /bin/bash

# This shell script can be used to install the GNU gcc compiler on an
# OS X computer.  The GNU gcc compiler is usually better at compiling
# C code produced by the Gambit Scheme compiler than the clang (LLVM)
# compiler which is provided with Apple's Xcode.

# After executing:
#
#    ./install-gnu-gcc-osx
#
# you should add this line to your ~/.profile and ~/.bashrc files:
#
#    export PATH=/usr/local/gcc-4.8.1/bin:$PATH

GCCVERSION=4.8.1
GMPVERSION=5.1.3
MPFRVERSION=3.1.2
MPCVERSION=1.0.1
PREFIX=/usr/local/gcc-$GCCVERSION
MAKE="make -j 4"

# Download & install the latest GCC and prerequisites

mkdir temp-gcc
cd temp-gcc

wget ftp://ftp.gnu.org/gnu/gcc/gcc-$GCCVERSION/gcc-$GCCVERSION.tar.gz
wget ftp://ftp.gnu.org/gnu/gmp/gmp-$GMPVERSION.tar.gz
wget ftp://ftp.gnu.org/gnu/mpfr/mpfr-$MPFRVERSION.tar.gz
wget ftp://ftp.gnu.org/gnu/mpc/mpc-$MPCVERSION.tar.gz

tar zxf gcc-$GCCVERSION.tar.gz
tar zxf gmp-$GMPVERSION.tar.gz
tar zxf mpfr-$MPFRVERSION.tar.gz
tar zxf mpc-$MPCVERSION.tar.gz

ln -s ../gmp-$GMPVERSION   gcc-$GCCVERSION/gmp
ln -s ../mpfr-$MPFRVERSION gcc-$GCCVERSION/mpfr
ln -s ../mpc-$MPCVERSION   gcc-$GCCVERSION/mpc

mkdir build-dir
cd build-dir
../gcc-$GCCVERSION/configure --prefix=$PREFIX
time $MAKE bootstrap > build.log

sudo mkdir -p $PREFIX
sudo make install

cd ../..
#rm -rf temp-gcc
ls -ld temp-gcc
