#!/bin/sh

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

PREFIX=$(pwd)/arm-linux
KERNEL=$HOME/embedded/linux-2.4.18-rmk7-ptx3
#HOST=sparc-linux
HOST=

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

#
# Check if everything is there
#
echo -n "check if sources are available..."
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
echo "done"

#
# binutils
#
echo -n "binutils..."
[ -d binutils-2.11.2 ] || {
	echo -n "extracting..."
	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
}
echo "done"

#
# Copy header files to PREFIX
#
echo -n "copying header files..."
[ -d $PREFIX/asm-arm/include ] || {
	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
}
echo "done"

export PATH=$PREFIX/bin:$PATH

#
# gcc
#

echo -n "gcc..."
# rm -rf gcc-2.95.3
[ -d gcc-2.95.3 ] || {
	echo -n "extracting..."
	tar zxf gcc-2.95.3.tar.gz
	cd gcc-2.95.3
	echo -n "patching..."
	bzcat ../gcc-2.95.3.diff.bz2 | patch -p1
	# hack to build gcc without glibc
	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
	cd ..
}
echo "done"

# gcc stage 1
echo -n "gcc-stage 1..."
[ -d gcc-2.95.3-stage1-obj ] || {
	mkdir gcc-2.95.3-stage1-obj
	cd gcc-2.95.3-stage1-obj
#	../gcc-2.95.3/configure --target=arm-linux --prefix=$PREFIX $HOST --with-headers=$KERNEL/include
	../gcc-2.95.3/configure 					\
	  --target=arm-linux		 				\
	  --prefix=$PREFIX 						\
	  $HOST								\
	  --with-headers=$KERNEL/include				\
	  --enable-languages="c" 					\
	  --with-cpu=strongarm110
#	make LANGUAGES=c
	make
	make install
	cd ..
}
echo "done"

# glibc-cross
echo -n "glibc..."
[ -d glibc-2.2.5 ] || {
	gzip -dc glibc-2.2.5.tar.gz | tar -xf -
	cd glibc-2.2.5
	gzip -dc ../glibc-linuxthreads-2.2.5.tar.gz | tar -xf -
	gzip -dc ../glibc-2.2.5-ptx1.diff.gz | patch -p1 
	rm -f sysdeps/unix/sysv/linux/configure
	cd ..
	mkdir glibc-2.2.5-obj	
	cd glibc-2.2.5-obj
	CC=arm-linux-gcc ../glibc-2.2.5/configure arm-linux 		\
	  --build=i686-linux						\
	  --target=arm-linux 						\
	  --prefix=$PREFIX/arm-linux					\
	  --enable-add-ons						\
	  --disable-sanity-checks
	make
	make install
	cd ..
}
echo "done"

# gcc stage 2
echo -n "gcc-stage 2..."
[ -d gcc-2.95.3-stage2-obj ] || {
	mkdir gcc-2.95.3-stage2-obj
	cd gcc-2.95.3
	# remove glibc header hack
	perl -pi -e 's/^(TARGET_LIBGCC2_CFLAGS = -fomit-frame-pointer -fPIC).*/$1/' gcc/config/arm/t-linux
#	perl -pi -e 's/(^T_CFLAGS =.*)//' gcc/config/arm/t-linux
	cd ..
	cd gcc-2.95.3-stage2-obj
	../gcc-2.95.3/configure arm-linux 				\
	  --prefix=$PREFIX 						\
	  --enable-languages="c,c++" 					\
	  --with-cpu=strongarm110
	make
	make install
	cd ..
}
echo "done"

