#!/bin/sh

# script for building GCC on Mingw (Windows port)
# author: Tomasz Sowa
# ts at ttmath dot org

# set a correct directory here
cd /mingw/4.6.3
path=`pwd`
echo "current path: "$path


##################################################
# give some info here
################################################## 

findprefix=""
filename=""
dir=""

# delete file: mingwm10.dll from /mingw/x.x.x/msys/bin
# it should only be present in /mingw/x.x.x/bin


# downloads:
# --------------------------------------------------------------------------------------

# binutils
# http://ftp.gnu.org/gnu/binutils/

# gmp
# http://gmplib.org/#DOWNLOAD

# mingw runtime
# http://sourceforge.net/projects/mingw/files/MinGW/Base/mingw-rt/

# win32api
# http://sourceforge.net/projects/mingw/files/MinGW/Base/w32api/

# mpc
# http://www.multiprecision.org/index.php?prog=mpc&page=download

# mpfr
# http://www.mpfr.org/mpfr-current/#download







function findextract
{
direxist=`find . -maxdepth 1 -type d -name "$findprefix*"`

if [ -n "$direxist" ]
then
	echo "directory with prefix $findprefix already exists"
	exit 1
fi


filename=`find . -maxdepth 1 -type f -name "$findprefix*"`

if [ -z "$filename" ]
then
	echo "file begining with $findprefix is missing"
	exit 1
fi


if [ `find . -maxdepth 1 -type f -name "$findprefix*" | wc -l` -ne 1 ]
then
	echo "there are more than one file with prefix "$findprefix
	exit 1
fi




ext=`echo $filename | grep -i ".tar.gz"`

if [ -n "$ext" ]
then
	echo "extracting: "$filename
	tar -xf $filename
	dir=`find . -maxdepth 1 -type d -name "$findprefix*"`
	
	if [ -z "$dir" ]
	then
		echo "I cannot find where it has been extracted to"
		exit 1
	fi
	
	echo "extracted to directory: "$dir
	return
fi



ext=`echo $filename | grep -i ".tar.bz2"`

if [ -n "$ext" ]
then
	echo "extracting: "$filename
	tar -xf $filename
	dir=`find . -maxdepth 1 -type d -name "$findprefix*"`

	if [ -z "$dir" ]
	then
		echo "I cannot find where it has been extracted to"
		exit 1
	fi

	echo "extracted to directory: "$dir
	return
fi




ext=`echo $filename | grep -i ".tar.lzma"`

if [ -n "$ext" ]
then
	echo "extracting: "$filename
	lzma -d -k $filename

	filename2=`echo $filename | sed s/\.lzma$//`
	tar -xf $filename2
	
	dir=`find . -maxdepth 1 -type d -name "$findprefix*"`

	if [ -z "$dir" ]
	then
		echo "I cannot find where it has been extracted to"
		exit 1
	fi
	
	rm $filename2

	echo "extracted to directory: "$dir
	return
fi



echo "I don't know what type of file it is: " $filename
exit 1
}


function testresult
{
	if [ $? -ne 0 ]
	then
		echo "incorrect result status"
		exit 1
	fi
}


configureopt=""




function compile
{
	echo "----------------------------------------------------------------------------"
	echo " building $findprefix "
	echo "----------------------------------------------------------------------------"
	
	findextract

	cd $dir
	echo "configuring in "$dir
	echo "configure options: "$configureopt 
	./configure $configureopt
	testresult
	
	echo "compiling in "$dir
	make && make install
	testresult
	cd $path
	rm -rf $dir

	echo "----------------------------------------------------------------------------"
	echo " $findprefix compiled and installed successfully"
	echo "----------------------------------------------------------------------------"
}




findprefix="binutils"
configureopt="--prefix=$path"
compile

findprefix="w32api"
# files should be placed in $path/w32api directory to correctly compile mingwruntime
configureopt="--prefix=$path/w32api"
compile

findprefix="mingwrt"
configureopt="--prefix=$path"
compile
# header files and libraries after installing will be in w32api/include and w32api/lib directories
# it is better to have them in the current directory
cp -R w32api/include ./
cp -R w32api/lib     ./
rm -rf w32api


findprefix="gmp"
configureopt="--prefix=$path"
compile

findprefix="mpfr"
configureopt="--prefix=$path --with-gmp=$path"
compile

findprefix="mpc"
configureopt="--prefix=$path --with-gmp=$path --with-mpfr=$path"
compile



findprefix="gcc"
configureopt="--prefix=$path --build=mingw32 --host=mingw32 --target=mingw32 --with-gcc --with-gnu-as --enable-threads=win32 --disable-nls --enable-languages=c,c++ --disable-win32-registry --disable-shared --with-as=$path/bin/as.exe --with-ld=$path/bin/ld.exe --with-gmp=$path --with-mpfr=$path --with-mpc=$path --enable-checking=yes"
echo "----------------------------------------------------------------------------"
echo " building $findprefix "
echo "----------------------------------------------------------------------------"
findextract
mkdir gccworkingdir
cd gccworkingdir
echo "configuring gcc"
echo "configure options: "$configureopt
../$dir/configure $configureopt
testresult
cd $path
# dla 2 bootstrapa i nastepnego trzeba miec pliki naglowkowe w /mingw/include i liby w /mingw/lib
# --sysroot takze nie pomaga gdyz wtedy szukany jest katalog $path/mingw/include
cp -R include /c/mingw
cp -R lib     /c/mingw
cd gccworkingdir
make
testresult
echo "----------------------------------------------------------------------------"
echo " new version of GCC compiled successfully"
echo "----------------------------------------------------------------------------"

#make -k check
# expect is missing (configure has failed) and dejagnu
#testresult
#echo "----------------------------------------------------------------------------"
#echo " new version of GCC tested successfully"
#echo "----------------------------------------------------------------------------"

make install
testresult
cd $path
rm -rf /c/mingw/include
rm -rf /c/mingw/lib
rm -rf gccworkingdir
rm -rf $dir
echo "----------------------------------------------------------------------------"
echo " new version of GCC installed successfully"
echo "----------------------------------------------------------------------------"

exit 0
