export TARGET="${1}"
export PREFIX="`pwd`/rel-bin/${TARGET}"
export PATH="${PREFIX}/bin:${PATH}"
export OUT="`pwd`/out"

case "${2}" in
64)
	export CC="cc -m64"
	export CXX="c++ -m64" ;;
32)
	export CC="cc -m32"
	export CXX="c++ -m32" ;;
esac

#### JEŚLI BĘDĄ NOWE WERSJE BINUTILS'A i GCC PROSZĘ ZMIENIĆ NUMER!!!
export binutils_version="2.32"	#TU ZMIENIAMY!
export newlib_version="3.1.0"	#TU ZMIENIAMY!
export gcc_version="LATEST-4.9"	#TU ZMIENIAMY!
export nproc="$(( `grep -c "processor" /proc/cpuinfo` * 2 ))"
export binutils_ext="tar.xz"	#TU POD ŻADNYM POZOREM NIE ZMIENIAJ TYPU!!!!!
export ARGS="--enable-multiarch --enable-multilib"
####

if ! command -v lftp >/dev/null; then
	clear
	printf "\n  Nie znaleziono \"lftp\"!\n  Prosze zanistaluj go na swoim obecnym systemie!\n\n"
	exit 1
fi

(
	if [ ! -e binutils-${binutils_version}${binutils_ext} ]; then
		wget -c "http://ftp.gnu.org/gnu/binutils/binutils-${binutils_version}.${binutils_ext}"
	fi
	if [ ! -d binutils-${binutils_version} ]; then
		tar -xf binutils-*.*
	fi
)	|| exit 1

if ! (
	mkdir -pv build-binutils
	(cd build-binutils; ../binutils-*/configure --target="${TARGET}" --prefix="${PREFIX}" --disable-nls --disable-werror --enable-obsolete --disable-debug --with-gcc --with-gnu-as --with-gnu-ld)
	make -C build-binutils -j${nproc}
	make -C build-binutils -j${nproc} install-strip
)
then
	echo "STAGE=${TARGET}" > .error
	exit 1
fi

(
	if [ ! -e */gcc-*.* ]; then
		lftp -c "open ftp://ftp.fu-berlin.de/unix/languages/gcc/snapshots ; mirror ${gcc_version}"
	fi
	if [ ! -d gcc-* ]; then
		tar -xf */gcc-*.*
	fi
)	|| exit 1

(
	if [ ! -e newlib-*.tar.gz ]; then
		wget -c "ftp://sources.redhat.com/pub/newlib/newlib-${newlib_version}.tar.gz"
	fi
	if [ ! -d newlib-${newlib_version} ]; then
		tar -xf newlib-*.tar.gz
	fi
)	|| exit 1

if ! (
	if [ ! -e .dl_ok ]; then
		(cd gcc-*; contrib/download_prerequisites)
		touch .dl_ok
	fi
	(cd gcc-*; ln -svf ../newlib-${newlib_version}/newlib newlib)
	mkdir -pv build-gcc
	(cd build-gcc; ../gcc-*/configure --target="${TARGET}" --prefix="${PREFIX}" --disable-shared --with-headers="$(readlink -f ../newlib-${newlib_version})/newlib/libc/include" --enable-languages="c,c++" --disable-lto --disable-libmudflap --disable-libgomp --disable-libssp --disable-libstdcxx-pch --with-newlib --with-gnu-as --with-gnu-ld --with-pkgversion="*DANiO* T00LCHAIN" --with-system-zlib ${ARGS})
	#make -C build-gcc -j${nproc} all-gcc
	#make -C build-gcc -j${nproc} install-gcc
)
then
	echo "STAGE=${TARGET}" > .error
	exit 1
fi

if (
	#mkdir -pv build-newlib
	#(cd build-newlib; ../newlib-${newlib_version}/configure --prefix="${PREFIX}" --target="${TARGET}" ${ARGS})
	#(cd newlib-${newlib_version}; ./configure --prefix="${PREFIX}" --target="${TARGET}" ${ARGS})
	#make -C newlib-${newlib_version} -j${nproc} all install distclean
	make -C build-gcc -j${nproc} all install
)
then
	find ${PREFIX} | xargs file | grep "ELF" | grep "executable" | cut -f 1 -d ":" | xargs strip -S 2>/dev/null
	mkdir -pv out
	if [ "${1}" == "64" ]; then
		(cd ${PREFIX}; tar -cJf "${OUT}/${TARGET}-GCC_${gcc_version}-`date +"%d-%m-%Y"`-`uname -s`_x86-64.txz" .)
	else
		(cd ${PREFIX}; tar -cJf "${OUT}/${TARGET}-GCC_${gcc_version}-`date +"%d-%m-%Y"`-`uname -s`_x86-32.txz" .)
	fi
	rm -rf build-*
	#printf "\n  POPRAWNIE ZBUDOWANO GCC DLA ${TARGET}!\n\n"
else
	echo "STAGE=${TARGET}" > .error
	exit 1
fi
