#!/bin/bash
# ##############################################################################
# (c) Eric Lassauge - January 2012
# <lassauge {AT} users {DOT} sourceforge {DOT} net >
#
# ##############################################################################
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
# ##############################################################################
# Complete script for MinGW gcc wrapper from cygwin: change POSIX path to windows path
# Needs: cygpath

#Configure to where you installed MinGW
mingwbase=/cygdrive/d/MinGW
mingwroot=`cygpath -w ${mingwbase}`

dollarstar=""
# Parse the gcc flags to change all file pathes
while [ $# -ne 0 ]; do
	case "$1" in
	-o) 
		param=`cygpath -w $2`
		dollarstar="${dollarstar} -o ${param}"
		shift
		;;
	-I)
		param=`cygpath -w $2`
		dollarstar="${dollarstar} -I ${param}"
		shift
		;;
	-I*)
		parami=`echo $1 | sed -e 's/-I//'`
		first=`echo ${parami} | sed -e 's/^\(.\).*/\1/'`
		case $first in
		'/' | '.')
			param=`cygpath -w ${parami}`
			dollarstar="${dollarstar} -I${param}"
			;;
		*)
			dollarstar="${dollarstar} ${1}"
			;;
		esac
		;;
	-B)
		param=`cygpath -w $2`
		dollarstar="${dollarstar} -B ${param}"
		shift
		;;
	-L)
		param=`cygpath -w $2`
		dollarstar="${dollarstar} -L ${param}"
		shift
		;;
	-L*) 
		paraml=`echo $1 | sed -e 's/-L//'`
		first=`echo ${paraml} | sed -e 's/^\(.\).*/\1/'`
		case $first in
		'/' | '.')
			param=`cygpath -w ${paraml}`
			dollarstar="${dollarstar} -L${param}"
			;;
		*)
			dollarstar="${dollarstar} ${1}"
			;;
		esac
		;;
	*)
		if [ -f "$1" ]
		then
			param=`cygpath -w $1`
			dollarstar="${dollarstar} ${param}"
		else
			dollarstar="${dollarstar} $1"
		fi
		;;
	esac
	shift
done

# remove spurious ^M
dollarstar=`echo "${dollarstar}" | sed -e 's///g'`
# echo "# Calling MinGW compiler: gcc ${dollarstar}"
# Add the path to mingw tools
export PATH=${mingwbase}/bin:$PATH
${mingwbase}/bin/gcc -Wl,-mi386pe --sysroot="${mingwroot}" ${dollarstar}
