#!/bin/sh

if [ $# != 2 -a $# != 3 -o $0 != "./install_gcc_539_thumb" ]; then
  echo "Usage: ./install_gcc_539_thumb <path-to-sdk-zip> <target-dir>"
  echo " e.g.: ./install_gcc_539_thumb gcc-539-2aeh-source.tar.bz2 ~/symbian-gcc"
  exit 1
fi

SRC=$1
DEST=`echo $2 | sed s:/\$::`

mkdir -p $DEST

MAKE=make
if [ "`which gmake`" != "" ]; then
        MAKE=gmake
fi

cpp=`which cpp`
if [ ! -e $DEST/bin/cpp ] && [ "`file $cpp | grep 'shell script'`" != "" ]; then LINK_CPP=1; fi

while read a
do
  echo "$a"
  sh -c "$a"
  if test $? != 0; then
    echo "Unexcpected error: aborting."
    exit 1
  fi
done << __END

tar -jxvf $SRC
patch -p0 < gcc-539.patch
mkdir obj-thumb
cd obj-thumb; sh ../src/configure --prefix=$DEST --target=thumb-epoc-pe; 
cd obj-thumb; $MAKE all-binutils all-gas all-ld all-gcc
cd obj-thumb; $MAKE install-binutils install-gas install-ld install-gcc
rm -rf obj-thumb
rm -rf src
cp thumb-specs $DEST/lib/gcc-lib/thumb-epoc-pe/2.9-psion-98r2/specs
cd $DEST/bin; ln -s ../lib/gcc-lib/thumb-epoc-pe/2.9-psion-98r2/cpp thumb-epoc-pe-cpp
cd $DEST/bin; if [ ! -e as ]; then ln -s arm-epoc-pe-as as; fi
if [ "$LINK_CPP" != "" ]; then cd $DEST/bin; ln -s arm-epoc-pe-cpp cpp; fi

__END

