#!/bin/sh -e

#
# Please change the following macros to your paths.
#

PREFIX=/usr/arm-linux
KERNEL=/home/robert/embedded/linux-2.4.5-rmk7-np2-ssv1
#HOST=sparc-linux
HOST=

# ----------------------------------------------------------------------------
# There should be nothing to configure below this line

#
# Check if everything is there
#
mkdir -p $PREFIX
if [ "$KERNEL" = "" -o ! -f "$KERNEL/Documentation/arm/README" ]; then
  echo "You haven't changed KERNEL to point at a valid kernel tree"
  exit 1
fi
if [ ! -f binutils-2.11.2.tar.gz ]; then
  echo Binutils 2.11.2 seems to be missing
  exit 1
fi
if [ ! -f gcc-2.95.3.tar.gz ]; then
  echo GCC 2.95.3 seems to be missing
  exit 1
fi
if [ ! -f gcc-2.95.3.diff.bz2 ]; then
  echo GCC 2.95.3 diff seems to be missing
  exit 1
fi

#
# Build binutils
#
echo "Extracting binutils..."
tar zxf binutils-2.11.2.tar.gz
pushd binutils-2.11.2
./configure --target=arm-linux --prefix=$PREFIX
make 
make install
popd
rm -rf binutils-2.11.2

#
# Copy header files to PREFIX
#
mkdir -p $PREFIX/asm-arm/include
cp -dR $KERNEL/include/asm-arm $PREFIX/asm-arm/include
cp -dR $KERNEL/include/asm     $PREFIX/asm-arm/include
cp -dR $KERNEL/include/linux   $PREFIX/asm-arm/include

#
# Build gcc
#
PATH=$PATH:$PREFIX/bin
rm -rf gcc-2.95.3
echo "Extracting gcc..."
tar zxf gcc-2.95.3.tar.gz
pushd gcc-2.95.3
bzcat ../gcc-2.95.3.diff.bz2 | patch -p1
perl -pi -e 's/^(TARGET_LIBGCC2_CFLAGS.*)/$1 -Dinhibit_libc -D__gthr_posix_h/' gcc/config/arm/t-linux
echo 'T_CFLAGS = -Dinhibit_libc -D__gthr_posix_h' >> gcc/config/arm/t-linux
./configure --target=arm-linux --prefix=$PREFIX $HOST --with-headers=$KERNEL/include
make LANGUAGES=c
make install LANGUAGES=c
popd
rm -rf gcc-2.95.3
