DEVDIR="$PWD"
DEPSDIR="${DEVDIR}/deps/install"

mkdir -p toolchains/i686-gnu/build/bintutils
mkdir -p toolchains/i686-gnu/build/gcc_first
mkdir -p toolchains/i686-gnu/build/glibc
mkdir -p toolchains/i686-gnu/build/gcc_second
mkdir -p toolchains/i686-gnu/install/

if [[ ! -f ${DEVDIR}/toolchains/i686-gnu/install/bin/i686-gnu-ld ]]; then
	cd toolchains/i686-gnu/build/bintutils
        ${DEVDIR}/src/binutils/*/configure \
		--prefix=${DEVDIR}/toolchains/i686-gnu/install \
                --target=i686-gnu \
                --with-mpc=$DEPSDIR \
                --with-mpfr=$DEPSDIR \
                --with-gmp=$DEPSDIR \
                --with-sysroot --disable-nls --disable-werror
        make -j4
        make install
        make clean
	cd $DEVDIR
else
        echo "Binutils already installed"
fi

export LD_LIBRARY_PATH="${DEPSDIR}/lib/"
export PATH="${DEVDIR}/toolchains/i686-gnu/install/bin:$PATH"
#export PATH="${DEVDIR}/gnu/install/bin:$PATH"
#export CFLAGS="-O1 -Wno-error"
if [[ ! -f ${DEVDIR}/toolchains/i686-gnu/install/bin/i686-gnu-gcc ]]; then
	cd toolchains/i686-gnu/build/gcc_first
        ${DEVDIR}/src/gcc/*/configure \
		--prefix=${DEVDIR}/toolchains/i686-gnu/install \
                --target=i686-gnu \
                --disable-libssp \
                --disable-libcilkrts \
                --disable-multilib \
                --disable-shared \
                --disable-gcov \
                --disable-threads \
                --without-headers \
                --with-newlib \
                --with-mpc=$DEPSDIR \
                --with-mpfr=$DEPSDIR \
                --with-gmp=$DEPSDIR \
                --disable-nls --enable-languages=c
        make -j4 all-gcc
        make install-gcc
        make -j4 all-target-libgcc
        make install-target-libgcc
	cd $DEVDIR
else
        echo "GCC already installed"
fi

if [[ ! -f ${DEVDIR}/gnu/install/lib/libc.a ]]; then
	cd toolchains/i686-gnu/build/glibc
	${DEVDIR}/src/glibc/*/configure \
		--prefix= \
		--host=i686-gnu \
		--with-headers=${DEVDIR}/gnu/install/include \
		--disable-profile \
		--disable-multi-arch \
		--disable-werror
	make -j 4
	make install DESTDIR=${DEVDIR}/gnu/install
	cd $DEVDIR

else
	echo "GLIBC already installed"
fi

echo "DONE!!!"

# install needed headers
cd $DEVDIR/src/linux
make headers_install INSTALL_HDR_PATH="${DEVDIR}/gnu/install"

cd $DEVDIR

ln -s ${DEVDIR}/toolchains/i686-gnu/install ${DEVDIR}/toolchains/i686-gnu/usr
ln -s ${DEVDIR}/toolchains/i686-gnu/install/lib ${DEVDIR}/toolchains/i686-gnu/lib
ln -s ${DEVDIR}/toolchains/i686-gnu/install/bin ${DEVDIR}/toolchains/i686-gnu/bin

if [[ ! -f ${DEVDIR}/toolchains/i686-gnu/install/bin/i686-gnu-gcc2 ]]; then
        cd toolchains/i686-gnu/build/gcc_second
        ${DEVDIR}/src/gcc/*/configure \
                --prefix=${DEVDIR}/gnu/install \
                --target=i686-gnu \
                --disable-libssp \
                --disable-libcilkrts \
                --disable-multilib \
                --disable-shared \
                --disable-gcov \
                --disable-threads \
		--with-sysroot=${DEVDIR}/gnu/install \
                --with-mpc=$DEPSDIR \
                --with-mpfr=$DEPSDIR \
                --with-gmp=$DEPSDIR \
                --disable-nls --enable-languages=c,c++
        make -j4 all-gcc
        make install-gcc
        make -j4 all-target-libgcc
        make install-target-libgcc
        cd $DEVDIR
else
        echo "GCC already installed"
fi
