#/bin/bash

GCC161="gcc-4.8.3+os161-2.1"
MIRROR="http://www.ece.ubc.ca/~os161/download"

GMP="/opt/homebrew/Cellar/gmp/6.2.1_1"
MPFR="/opt/homebrew/Cellar/mpfr/4.1.0"
LIBMPC="/opt/homebrew/Cellar/libmpc/1.2.1"

cd ~/tools
wget $MIRROR/$GCC161.tar.gz

echo '*** Unpacking GCC ***'
for file in *.tar.gz; do
    tar -xzf $file
    rm -f $file
done

PRE_CC=$CC
PRE_CFLAGS=$CFLAGS
CC=gcc
CFLAGS=

echo '*** Building gcc ***'
PATH=$HOME/tools/sys161/bin:$HOME/tools/os161/bin:$PATH
export PATH
cd $GCC161
find . -name '*.info' | xargs touch
touch intl/plural.c
mkdir ../gcc-build
cd ../gcc-build
../$GCC161/configure --enable-languages=c,lto -nfp --disable-shared --disable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx --disable-nls --target=mips-harvard-os161 --prefix=$HOME/tools/os161 --with-gmp=$GMP --with-mpfr=$MPFR  --with-mpc=$LIBMPC 2>&1 | tee ../gcc.log
make 2>&1 | tee -a ../gcc.log
make install 2>&1 | tee -a ../gcc.log
cd ..
echo '*** Finished building gcc ***'
rm -rf gcc-build

cd os161/bin
for file in *; do
    ln -s $file ${file:13}
done
cd ../..

cd ~
echo 'PATH=$HOME/tools/sys161/bin:$HOME/tools/os161/bin:$PATH' >> $HOME/.bashrc
echo 'set path = ($path $HOME/tools/os161/bin $HOME/tools/sys161/bin)' >> $HOME/.cshrc

CC=$PRE_CC
CFLAGS=$PRE_CFLAGS
echo '*** Done ***'


