#! /bin/sh

builddir=`pwd`
srcdir=`dirname $0`/../..
srcdir=`cd $srcdir && pwd`
if [ ! -e $srcdir/configure ]; then
  echo "configure script not found"
  exit 1
fi

if [ -e /usr/bin/i586-mingw32msvc-gcc ]; then
  _mingw_tool_prefix="/usr/bin/i586-mingw32msvc"
  STATIC_LIBGCC=
elif [ -e /usr/bin/i486-mingw32-gcc ]; then
  _mingw_tool_prefix="/usr/bin/i486-mingw32"
  STATIC_LIBGCC="-static-libgcc -static-libstdc++"
else
  echo "don't know where mingw compiler is"
  exit 1
fi

export mgwdir=`dirname $0`/gtk-win
. $mgwdir/config.sh || exit 1

en_only=false
if [ "$1" = "--en" ]; then
  en_only=true
  shift
fi

destdir=$mgwdestdir
if $en_only; then
  destdir=$destdir-en
fi

if [ "$mgwconfig" = "debug" ]; then
  _def_cflags='-gstabs'
else
  _def_cflags='-O2'
fi

export ADDR2LINE=$_mingw_tool_prefix-addr2line
export AS=$_mingw_tool_prefix-as
export CC=$_mingw_tool_prefix-gcc
export CPP=$_mingw_tool_prefix-cpp
export CPPFILT=$_mingw_tool_prefix-c++filt
export CXX=$_mingw_tool_prefix-g++
export DLLTOOL=$_mingw_tool_prefix-dlltool
export DLLWRAP=$_mingw_tool_prefix-dllwrap
export GCOV=$_mingw_tool_prefix-gcov
export LD=$_mingw_tool_prefix-ld
export NM=$_mingw_tool_prefix-nm
export OBJCOPY=$_mingw_tool_prefix-objcopy
export OBJDUMP=$_mingw_tool_prefix-objdump
export READELF=$_mingw_tool_prefix-readelf
export SIZE=$_mingw_tool_prefix-size
export STRINGS=$_mingw_tool_prefix-strings
export WINDRES=$_mingw_tool_prefix-windres
export RC=$_mingw_tool_prefix-windres
export AR=$_mingw_tool_prefix-ar
export RANLIB=$_mingw_tool_prefix-ranlib
export STRIP=$_mingw_tool_prefix-strip

export LDFLAGS="$STATIC_LIBGCC -L$mgwtargetdir/lib"
export CPPFLAGS="-I$mgwtargetdir/include"
export CFLAGS="-mms-bitfields -march=i686 $_def_cflags"
export CXXFLAGS="-mms-bitfields -march=i686 $_def_cflags"
export OBJCFLAGS="-mms-bitfields -march=i686 $_def_cflags"

export PKG_CONFIG_LIBDIR="$mgwtargetdir/lib/pkgconfig"
export PKG_CONFIG_PATH="$mgwtargetdir/lib/pkgconfig"

export PYTHON_INCLUDES="-I$mgwtargetdir/include/python"
export PYTHON_LIBS="-lpython$mgwpythonver"
export PYTHON_VERSION=$mgwpythondotver

configure_args="--build=i686-pc-linux-gnu --host=i586-pc-mingw32msvc --target=i586-pc-mingw32msvc \
AR=$AR RANLIB=$RANLIB STRIP=$STRIP AS=$AS DLLTOOL=$DLLTOOL OBJDUMP=$OBJDUMP NM=$NM WINDRES=$WINDRES RC=$RC \
INSTALL=`which install-check` \
--prefix=$destdir"

if $en_only; then
  moo_nls_flags='--disable-nls'
else
  moo_nls_flags=
fi

if [ "$mgwconfig" = "debug" ]; then
  moo_configure_args='--enable-debug'
else
  moo_configure_args='--enable-dev-mode'
fi

if [ "$1" = "--python" ]; then
  shift
  moo_configure_args="$moo_configure_args --enable-moo-module --enable-shared --disable-static"
fi

echo $srcdir/configure $configure_args $moo_configure_args $moo_nls_flags "$@"
exec $srcdir/configure $configure_args $moo_configure_args $moo_nls_flags "$@"
