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

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

if [[ ! -f ${DEVDIR}/toolchains/i686-linux-gnu/install/bin/i686-linux-gnu-ld ]]; then
	cd toolchains/i686-linux-gnu/build/bintutils
        ${DEVDIR}/src/binutils/*/configure \
		--prefix=${DEVDIR}/toolchains/i686-linux-gnu/install \
                --target=i686-linux-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-linux-gnu/install/bin:$PATH"

if [[ ! -f ${DEVDIR}/toolchains/i686-linux-gnu/install/bin/i686-linux-gnu-gcc ]]; then
	cd toolchains/i686-linux-gnu/build/gcc_first
        ${DEVDIR}/src/gcc/*/configure \
		--prefix=${DEVDIR}/toolchains/i686-linux-gnu/install \
                --target=i686-linux-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}/toolchains/i686-linux-gnu/install/lib/libc.a ]]; then
	cd toolchains/i686-linux-gnu/build/glibc
	${DEVDIR}/src/glibc/*/configure \
		--prefix= \
		--host=i686-linux-gnu
	make -j 4
	make install DESTDIR=${DEVDIR}/toolchains/i686-linux-gnu/install
	cd $DEVDIR

else
	echo "GLIBC already installed"
fi

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

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

if [[ ! -f ${DEVDIR}/toolchains/i686-linux-gnu/install/bin/i686-linux-gnu-gcc2 ]]; then
        cd toolchains/i686-linux-gnu/build/gcc_second
        ${DEVDIR}/src/gcc/*/configure \
                --prefix=${DEVDIR}/toolchains/i686-linux-gnu/install \
                --target=i686-linux-gnu \
                --disable-libssp \
                --disable-libcilkrts \
                --disable-multilib \
                --disable-shared \
                --disable-gcov \
                --disable-threads \
		--with-sysroot=${DEVDIR}/toolchains/i686-linux-gnu \
                --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
