#!/bin/sh
# Script to build a MinGW cross-compiler for Linux
# http://reactos.csh-consult.dk

if [ -z $1 ]
then
	echo "Usage build-mingw.sh prefix"
	echo "  prefix - Where to install compiler (eg. /home/user/mingw32)"
	exit 1
else
	PREFIX=$1
	echo "Installing compiler in ${PREFIX}"
fi

# Configuration BEGIN
BINUTILS=binutils-2.15.94-20050118-1-src.tar.gz
GCCCORE=gcc-core-4.0.0.tar.gz
GCCGPP=gcc-g++-4.0.0.tar.gz
MINGWRUNTIME=mingw-runtime-3.7.tar.gz
W32API=w32api-3.2.tar.gz
BINUTILS_DL="http://aleron.dl.sourceforge.net/sourceforge/mingw/${BINUTILS}"
#GCCCORE_DL="http://aleron.dl.sourceforge.net/sourceforge/mingw/${GCCCORE}"
GCCCORE_DL="http://mirrors.rcn.net/pub/sourceware/gcc/releases/gcc-4.0.0/${GCCCORE}"
#GCCGPP_DL="http://aleron.dl.sourceforge.net/sourceforge/mingw/${GCCGPP}"
GCCGPP_DL="http://mirrors.rcn.net/pub/sourceware/gcc/releases/gcc-4.0.0/${GCCGPP}"
MINGWRUNTIME_DL="http://aleron.dl.sourceforge.net/sourceforge/mingw/${MINGWRUNTIME}"
W32API_DL="http://aleron.dl.sourceforge.net/sourceforge/mingw/${W32API}"
DL_FILES="${BINUTILS_DL} ${GCCCORE_DL} ${GCCGPP_DL} ${MINGWRUNTIME_DL} ${W32API_DL}"
# Configuration END

# ---------------------------------------------

BUILDBASE=`pwd`

# Check for flex
FLEX=`which flex`
if [ -z ${FLEX} ]
then
    echo "Flex not found. Please install flex. If you have apt-get, run 'apt-get install flex' (use flex-old if you are using debian)."
    exit 1
fi

# Check for bison
BISON=`which bison`
if [ -z ${BISON} ]
then
    echo "Bison not found. Please install bison. If you have apt-get, run 'apt-get install bison'."
    exit 1
fi

cd ${BUILDBASE}

for URL in ${DL_FILES}
do
	BASENAME=`basename ${URL}`
	if [ -f ${BASENAME} ]
	then
		echo ${BASENAME} found.
	else
		echo ${BASENAME} not found. Downloading ${URL}...
		wget ${URL}
	fi
done

mkdir binutils.tmp
cd binutils.tmp
mkdir binutils.src
cd binutils.src
echo "Using BINUTILS package ${BINUTILS}"
tar -zxf ../../${BINUTILS}
BINUTILSSRC=`ls`
cd ..
mkdir build.binutils
cd build.binutils
../binutils.src/${BINUTILSSRC}/configure --disable-nls --target=mingw32 --prefix=${PREFIX} --with-gcc --with-gnu-as --with-gnu-ld --disable-shared
make "CFLAGS=-O2 -fno-exceptions" "LDFLAGS=-s" all install || exit 1
export PATH=${PREFIX}/bin:$PATH

cd ${BUILDBASE}
mkdir mingw.tmp
cd mingw.tmp
tar -zxf ../${MINGWRUNTIME}
tar -zxf ../${W32API}

cd ${BUILDBASE}
mkdir gcc.tmp
cd gcc.tmp
mkdir gcc.src
GCCSRC=${BUILDBASE}/gcc.tmp/gcc.src
mkdir gcccore.src
cd gcccore.src
echo "Using GCC CORE package ${GCCCORE}"
tar -zxf ../../${GCCCORE}
GCCCORESRC=`ls`
cp -R ${GCCCORESRC}/* ${GCCSRC}/
cd ..
mkdir gccgpp.src
cd gccgpp.src
echo "Using GCC G++ package ${GCCGPP}"
tar -zxf ../../${GCCGPP}
GCCGPPSRC=`ls`
cp -R ${GCCGPPSRC}/* ${GCCSRC}/
cd ..
mkdir build.gcc
cd build.gcc
${GCCSRC}/configure --with-gcc --with-gnu-ld --with-gnu-as --target=mingw32 \
--with-headers=${BUILDBASE}/mingw.tmp/include --with-libs=${BUILDBASE}/mingw.tmp/lib \
--prefix=${PREFIX} --enable-threads --disable-nls --enable-languages=c,c++ --disable-win32-registry \
--disable-shared --enable-sjlj-exceptions --enable-interpreter --enable-hash-synchronization --without-newlib
make "CFLAGS=-O6" "LDFLAGS=-s" DEBUG_FLAGS=-g0 all install || exit 1

echo "You may 'rm -rf binutils.tmp mingw.tmp gcc.tmp' now"
echo "Remember to put ${PREFIX}/bin in your path (eg. export PATH=${PREFIX}/bin:\$PATH)"

exit 0
