#!/bin/bash

TARGET=or32-elf

sudo rm -rf /opt/$TARGET/
rm -rf b-binutils b-gcc b-gdb

mkdir b-binutils
cd b-binutils
../../binutils/configure --target=$TARGET --prefix=/opt/$TARGET/ || exit
make all || exit
sudo make install || exit
export PATH=/opt/$TARGET/bin:$PATH
cd ..

mkdir b-gcc
cd b-gcc
../../gcc-3.2.3/configure --target=$TARGET --prefix=/opt/$TARGET/ \
                          --with-gnu-as --with-gnu-ld --verbose --enable-languages=c \
                          --with-newlib || exit
make clean all || exit
sudo sh -c 'PATH=/opt/$TARGET/bin:$PATH make install  || exit'
cd ..

mkdir b-gdb
cd b-gdb
../../gdb-5.0/configure --target=$TARGET || exit
make all || exit
sudo cp gdb/gdb /opt/$TARGET/bin/$TARGET-gdb
cd ..

cd ../or1ksim
./configure --target=$TARGET --prefix=/opt/$TARGET || exit
make all || exit
sudo sh -c 'PATH=/opt/$TARGET/bin:$PATH make install' || exit
cd ../..
