#!/bin/sh
TARGET=mipsel-elf
PREFIX=/usr/local

SRC_BINUTILS=binutils-2.19
SRC_GCC=gcc-4.3.3
SRC_NEWLIB=newlib-1.16.0
SRC_GMP=gmp-4.2.4
SRC_MPFR=mpfr-2.4.0

# ---------------------------------------------------------------
# 1. Binutils bauen und installieren
# ---------------------------------------------------------------
if [ ! -e $SRC_BINUTILS.tar.bz2 ]; then
  echo Downloading $SRC_BINUTILS
  wget ftp://ftp-stud.fht-esslingen.de/pub/Mirrors/ftp.gnu.org/binutils/$SRC_BINUTILS.tar.bz2 || exit 1
else
  echo Skipped downloading $SRC_BINUTILS
fi  

if [ ! -e ./$SRC_BINUTILS ]; then
  echo Unpacking $SRC_BINUTILS
  tar -xjf $SRC_BINUTILS.tar.bz2 || exit 1
else
  echo Skipped unpacking $SRC_BINUTILS
fi

if [ ! -e ./$SRC_BINUTILS-build ]; then
  echo Building $SRC_BINUTILS
  mkdir -p $SRC_BINUTILS-build
  cd $SRC_BINUTILS-build
  ../$SRC_BINUTILS/configure --target=$TARGET --prefix=$PREFIX || exit 1
  make || exit 1
  make install || exit 1
  cd ..
else
  echo $SRC_BINUTILS is already built.
fi

# ---------------------------------------------------------------
# 2. GMP bauen und installieren
# ---------------------------------------------------------------
if [ ! -e $SRC_GMP.tar.bz2 ]; then
  echo Downloading $SRC_GMP
  wget http://ftp.sunet.se/pub/gnu/gmp/$SRC_GMP.tar.bz2 || exit 1
else
  echo Skipped downloading $SRC_GMP
fi  

if [ ! -e ./$SRC_GMP ]; then
  echo Unpacking $SRC_GMP
  tar -xjf $SRC_GMP.tar.bz2 || exit 1
else
  echo Skipped unpacking $SRC_GMP
fi

if [ ! -e ./$SRC_GMP-build ]; then
  echo Building $SRC_GMP
  mkdir -p $SRC_GMP-build
  cd $SRC_GMP-build
  ../$SRC_GMP/configure --prefix=$PREFIX || exit 1
  make || exit 1
  make check || exit 1
  make install || exit 1
  cd ..
else
  echo $SRC_GMP is already built.
fi

# ---------------------------------------------------------------
# 3. MPFR bauen und installieren
# ---------------------------------------------------------------
if [ ! -e $SRC_MPFR.tar.bz2 ]; then
  echo Downloading $SRC_MPFR
  wget http://www.mpfr.org/mpfr-current/$SRC_MPFR.tar.bz2 || exit 1
else
  echo Skipped downloading $SRC_MPFR
fi  

if [ ! -e ./$SRC_MPFR ]; then
  echo Unpacking $SRC_MPFR
  tar -xjf $SRC_MPFR.tar.bz2 || exit 1
else
  echo Skipped unpacking $SRC_MPFR
fi

if [ ! -e ./$SRC_MPFR-build ]; then
  echo Building $SRC_MPFR
  mkdir -p $SRC_MPFR-build
  cd $SRC_MPFR-build
  ../$SRC_MPFR/configure --prefix=$PREFIX || exit 1
  make || exit 1
  make check || exit 1
  make install || exit 1
  cd ..
else
  echo $SRC_MPFR is already built.
fi

# ---------------------------------------------------------------
# 4. GCC & newlib bauen und installieren
# ---------------------------------------------------------------
if [ ! -e $SRC_NEWLIB.tar.gz ]; then
  echo Downloading $SRC_NEWLIB
  wget ftp://sources.redhat.com/pub/newlib/$SRC_NEWLIB.tar.gz || exit 1
else
  echo Skipped downloading $SRC_NEWLIB
fi  

if [ ! -e ./$SRC_NEWLIB ]; then
  echo Unpacking $SRC_NEWLIB
  tar -xzf $SRC_NEWLIB.tar.gz || exit 1
  if [ -e  $SRC_NEWLIB-jens.patch ]; then
    echo Patching $SRC_NEWLIB
    patch -p0 < $SRC_NEWLIB-jens.patch || exit 1
  fi	
else
  echo Skipped unpacking $SRC_NEWLIB
fi

if [ ! -e $SRC_GCC.tar.bz2 ]; then
  echo Downloading $SRC_GCC
  wget ftp://ftp-stud.fht-esslingen.de/pub/Mirrors/ftp.gnu.org/gcc/$SRC_GCC/$SRC_GCC.tar.bz2 || exit 1
else
  echo Skipped downloading $SRC_GCC
fi  

if [ ! -e ./$SRC_GCC ]; then
  echo Unpacking $SRC_GCC
  tar -xjf $SRC_GCC.tar.bz2 || exit 1
  cd ./$SRC_GCC
  ln -sf ../$SRC_NEWLIB/newlib/ .
  cd ..
else
  echo Skipped unpacking $SRC_GCC
fi

if [ ! -e ./$SRC_GCC-build ]; then
  echo Building $SRC_GCC
  mkdir -p $SRC_GCC-build
  cd $SRC_GCC-build
  ../$SRC_GCC/configure --target=$TARGET --prefix=$PREFIX --with-float=soft --with-newlib --verbose --enable-languages="c,c++" || exit 1
  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PREFIX/lib make all || exit 1
  make info || exit 1
  make install || exit 1
  cd ..
  echo $PREFIX/lib >/etc/ld.so.conf.d/local.conf
  ldconfig
else
  echo $SRC_GCC is already built.
fi
