#!/bin/bash
#  Automated script for building MinGW cross compiler v0.1
#
# by Paul Millar <paulm@astro.gla.ac.uk> based info from Pieter Thysebaert
#  -- This script is available under terms and conditions of BSD license. --

##
##  Parameters
##

#
#  Directories, BUILD_ROOT will be deleted, its parent directory must exist
BUILD_ROOT=/usr/local/src/mingw
PREFIX=/usr/local/mingw

#
#  What we're trying to build.
TARGET=${1:-i586-mingw32msvc}

echo Building $TARGET

#
# Which compiler to use
CC="gcc"
# If you have distcc installed, try:
#export CC="distcc gcc"
#export DISTCC_LOG=~/distcc.log
#export DISTCC_HOSTS="localhost node2 node3 node4"
#MAKE_PROCESS_COUNT=8

#
# Location (& versions) of various files.
# For latest versions, see 
#      http://sourceforge.net/project/showfiles.php?group_id=2435
GCC=~/Storage/gcc-3.2.1-20021201-3-src.tar.gz
BINUTILS=~/Storage/binutils-2.13.90-20030111-1-src.tar.gz
W32API=~/Storage/w32api-2.1-src.tar.gz
RUNTIME=~/Storage/mingw-runtime-2.3-src.tar.gz

##
##  Shouldn't need to alter anything from here on.
##

# But if you need to by-pass certain stages, here where you do it ...
do_clean=1
do_binutils=1
do_headers=1
do_gcc=1
do_api=1
do_runtime=1

MAKE="make ${MAKE_PROCESS_COUNT:+-j $MAKE_PROCESS_COUNT}"
PATH=$PATH:$PREFIX/bin

#
# Clean slate
if [ $do_clean -eq 1 ]; then
  rm -rf $BUILD_ROOT
fi

[ ! -d $BUILD_ROOT ] && mkdir $BUILD_ROOT


#
#  Build binutils package
if [ $do_binutils -eq 1 ]; then
  cd $BUILD_ROOT
  tar xzf $BINUTILS
  cd binutils*
  mkdir BUILD && cd BUILD
  ../configure --target=$TARGET --prefix=$PREFIX
  $MAKE && make install
  cd $BUILD_ROOT
  rm -rf binutils*
fi

PREFIX_TARGET="$(echo $PREFIX/$TARGET)"

#
#  Install include directories so componants can compile
if [ $do_headers -eq 1 ]; then
  cd $BUILD_ROOT

  mkdir $PREFIX_TARGET/include

  tar xzf $RUNTIME
  cp -r mingw-runtime-*/include/* $PREFIX_TARGET/include
  rm -rf mingw-runtime-*

  tar xzf $W32API
  cp -r w32api-*/include/* $PREFIX_TARGET/include
  rm -rf w32api-*
fi



#
#  Build and install cross-compiling gcc `c' compiler only
if [ $do_gcc -eq 1 ]; then
  cd $BUILD_ROOT
  tar xzf $GCC
  cd gcc-*
  mkdir BUILD && cd BUILD
  ../configure --prefix=$PREFIX --target=$TARGET --without-headers \
           --with-newlib --disable-threads --enable-languages=c
  $MAKE

  make install
  cd $BUILD_ROOT
  rm -rf gcc-*
fi


unset CC

#
#  Install Windows API
if [ $do_api -eq 1 ]; then
  cd $BUILD_ROOT

  [ ! -d w32api-* ] && tar xzf $W32API

  mkdir build-w32api && cd build-w32api

  ../w32api-*/configure --prefix=$PREFIX_TARGET --target=$TARGET \
            --host=$TARGET --build=$(../w32api-*/config.guess)
  make && make install

  cd $BUILD_ROOT
  rm -rf w32api-* build-w32api
fi


#
#  Build and install the mingw-runtime
if [ $do_runtime -eq 1 ]; then
  cd $BUILD_ROOT
  tar xzf $RUNTIME
  tar xzf $W32API
  mv w32api-* w32api

  mkdir build-mingw-runtime && cd build-mingw-runtime
  
  ../mingw-runtime-*/configure --prefix=$PREFIX_TARGET --target=$TARGET  \
               --host=$TARGET --build=$(../mingw-runtime-*/config.guess)

  # Kludge fix, so we can get libmingwex.a
  cd mingwex
  mv Makefile Makefile.old
  sed "s/cc/$TARGET-gcc/;s/ranlib/$TARGET-ranlib/" < Makefile.old > Makefile
  make  
  cp libmingwex.a $PREFIX_TARGET/lib

  cd $BUILD_ROOT/build-mingw-runtime
  make && make install

  cd $BUILD_ROOT
  rm -rf build-mingw-runtime mingw-runtime-* w32api
fi

