#!/bin/sh

set -xv
 
# Target architecture
#TARGET=mips-sgi-irix6
TARGET=mips-linux

# Build directory for cross-gcc tool chain
TOPDIR=/usr/local/gcc-sgi
PATH=${PATH}:${TOPDIR}/bin
LOG=${TOPDIR}/build.log

# Sources for binutils, gcc, kernel, c library
BINUTILS=/usr/local/src/binutils-2.12.91
GCC=/usr/local/src/gcc-3.2
KERNEL=/usr/src/linux
NEWLIB=/usr/local/src/newlib-1.10.0
GLIBC=/usr/local/src/glibc-2.2.5


if [ ]; then
#
# Binutils
#
if [ ! -f ${TOPDIR}/binutils ] ; then 
  cd ${TOPDIR} && mkdir binutils && cd binutils
fi 
  cd ${TOPDIR}/binutils
  ${BINUTILS}/configure --target=$TARGET --prefix=$TOPDIR \
      | tee ${LOG}  
  make all install | tee ${LOG}

#
# Binutils/gas
# Cross assembler should have been build as part of binutils.
# But is not.
#
if [ ! -f ${TOPDIR}/gas ] ; then 
  cd ${TOPDIR} && mkdir gas && cd gas
fi 
  cd ${TOPDIR}/gas
  ${BINUTILS}/gas/configure --target=$TARGET --prefix=$TOPDIR \
      | tee ${LOG}
  make all install | tee ${LOG}


#
# Generate and configure the kernel headers for target.
# These headers are then included via --with-headers.
#
if [ ! -f $TOPDIR/$(TARGET).config ] ; then 
  echo First make the config for the kernel with mips arch.
  exit 1
fi
# Change by hand: .config:CONFIG_CROSSCOMPILE=y
  cp $TOPDIR/$(TARGET).config ${KERNEL}/.config
  cd ${KERNEL} 
  make ARCH=mips oldconfig
  make ARCH=mips include/linux/version.h
  make ARCH=mips symlinks

#
# Get remaining headers for the bootstrap gcc from glibc.
#
cd ${TOPDIR}/${TARGET}
ln -s ${GLIBC}/include
# mkdir sys-include
# ln -s ./ sys
# cd /usr/src/linux/include/asm-mips
# cp *.h ${TOPDIR}/${TARGET}/sys-include
# cp ${GCC}/include/stdio.h ${TOPDIR}/${TARGET}/sys-include

fi

#
# Bootstrap gcc
#
cd ${TOPDIR} && mkdir gcc && cd gcc
${GCC}/configure --target=$TARGET --prefix=$TOPDIR \
  --with-libs=${TOPDIR}/lib \
  --disable-shared \
  --with-newlib \
  --enable-languages=c \
  --with-headers=${KERNEL}/include/ \
  --with-as=${TOPDIR}/${TARGET}/bin/as \
  --with-ld=${TOPDIR}/${TARGET}/bin/ld \
  2>&1 | tee ${LOG}

# ${TOPDIR}/opcodes/ ${TOPDIR}/intl ${TOPDIR}/libiberty"" \
#  --with-headers=${KERNEL}/include/ \
#  --with-newlib\
#  --without-headers\

exit 1;

make all-gcc install-gcc >${LOG}

exit 1;

#
# Newlib
#
cd ${TOPDIR}
mkdir newlib && cd newlib
${NEWLIB}/configure --target=$TARGET --prefix=$TOPDIR --without-headers --with-newlib --with-gnu-as --with-gnu-ld 2>&1 >${LOG}
#make all install >${LOG}

#
# Final gcc?
#
cd ${TOPDIR} && mkdir gcc && cd gcc
${GCC}/configure --target=$TARGET --prefix=$TOPDIR\
  --with-headers=${TOPDIR}/linux/include/\
  --with-newlib\
  --with-as=${TOPDIR}/${TARGET}/bin/as\
  --with-ld=${TOPDIR}/${TARGET}/bin/ld\
  --enable-languages=c,c++  2>&1 >${LOG}
make all install >${LOG}

