#!/bin/sh
# Written by Uwe Hermann <uwe@hermann-uwe.de>, released as public domain.
# Modifikace Mrazik - stop pri chybe kompilace, wget a untar pouze pokud je potreba

TARGET=arm-none-eabi
PREFIX=/opt/arm/gnuarm-4.7.2/          # Install location of your final toolchain
PARALLEL=""                            # Or: PARALLEL=""

BINUTILS=binutils-2.23.1
GCC=gcc-4.7.2
NEWLIB=newlib-1.20.0
GDB=gdb-7.5

##################################################################
# FUNCTIONS                                                      #
##################################################################

buildbinutils() {
  if [ -f $PREFIX$TARGET/bin/as ]; then echo "$BINUTILS already installed"
  else
      echo "BUILD binutils"
	  if [ -f $BINUTILS.tar.bz2 ]; then echo "$BINUTILS.tar.bz2 exists"
	  else
	      wget -c http://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.bz2
	  fi
	  if [ -d $BINUTILS ]; then echo "$BINUTILS already untared"
	  else
	      tar xfvj $BINUTILS.tar.bz2
	  fi
	  cd build
	  ../$BINUTILS/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib \
	  --with-float=soft --disable-werror --with-gnu-as --with-gnu-ld --disable-nls
	  make $PARALLEL
	  if [ $? -ne 0 ]; then exit; fi
	  make install
	  cd ..
	  rm -rf build/*
	  #rm -rf $BINUTILS
	  #rm -rf $BINUTILS.tar.bz2
  fi
}

prebuildgcc() {
  if [ -f $PREFIX$TARGET/bin/gcc ]; then echo "$GCC already installed"
  else
      echo "build gcc stage 1"
	  if [ -f $GCC.tar.bz2 ]; then echo "$GCC.tar.bz2 exists"
	  else
	    wget -c ftp://ftp.gnu.org/gnu/gcc/$GCC/$GCC.tar.bz2
	  fi
	  if [ -d $GCC ]; then echo "$GCC already untared"
	  else
	    tar xfvj $GCC.tar.bz2
	  fi
	  cd build
	  ../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib \
	  --enable-languages="c" --with-newlib --without-headers --disable-shared --with-float=soft \
          --with-gnu-as --with-gnu-ld
	  make $PARALLEL all-gcc
	  if [ $? -ne 0 ]; then exit; fi
	  make install-gcc
	  cd ..
	  rm -rf build/*
	  #rm -rf $GCC.tar.bz2
  fi
}
buildnewlib() {
  if [ -f $PREFIX$TARGET/lib/libc.a ]; then echo "$NEWLIB already installed"
  else
    echo "build newlib"
	  if [ -f $NEWLIB.tar.gz ]; then echo "$NEWLIB.tar.gz exists"
	  else
	    wget -c ftp://sources.redhat.com/pub/newlib/$NEWLIB.tar.gz
	  fi
	  if [ -d $NEWLIB ]; then echo "$NEWLIB already untared"
	  else
	    tar xfvz $NEWLIB.tar.gz
	  fi
	  cd build
	  ../$NEWLIB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork \
	  --enable-multilib --with-float=soft  --with-gnu-as --with-gnu-ld --disable-nls
	  make $PARALLEL
	  if [ $? -ne 0 ]; then exit; fi
	  make install
	  cd ..
	  rm -rf build/*
	  #rm -rf $NEWLIB
	  #rm -rf $NEWLIB.tar.gz
  fi
}

# Yes, you need to build gcc again!
buildgcc() {
  if [ -f $PREFIX$TARGET/bin/g++ ]; then echo "$GCC with newlib already installed"
  else
      echo "build gcc stage 2"
	  cd build
	  ../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib \
	    --enable-languages="c,c++" --with-newlib --disable-shared --with-float=soft --with-gnu-as --with-gnu-ld
	  make $PARALLEL
	  if [ $? -ne 0 ]; then exit; fi
	  make install
	  cd ..
	  rm -rf build/*
	  #rm -rf $GCC
  fi
}

buildgdb() {
  if [ -f $PREFIX/bin/$TARGET-gdb ]; then echo "$GDB already installed"
  else
      echo "build gdb"
	  if [ -f $GDB.tar.bz2 ]; then echo "$GDB.tar.bz2 exists"
	  else
	    wget -c ftp://ftp.gnu.org/gnu/gdb/$GDB.tar.bz2
	  fi
	  if [ -d $GDB ]; then echo "$GDB already untared"
	  else
	    tar xfvj $GDB.tar.bz2
	  fi
	  cd build
	  ../$GDB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib
	  make $PARALLEL
	  if [ $? -ne 0 ]; then exit; fi
	  make install
	  cd ..
	  rm -rf build
	  #rm -rf $GDB
	  #rm -rf $GDB.tar.bz2
  fi
}
##################################################################
# VLASTNI SKRIPT                                                 #
##################################################################
export PATH="$PATH:$PREFIX/bin"
if [ -d ./build ]; then echo "build directory exists"
else
  mkdir build
fi

buildbinutils
prebuildgcc
buildnewlib
buildgcc
buildgdb
