#!/bin/sh

# you need to change this if you run from outside this dir. 
if [ "$WXROOT" = "" ]; then
  scriptDir="$(cd $(dirname $0);pwd)"
  WXROOT=$scriptDir/../../..
fi

. $WXROOT/distrib/scripts/includes/configure_build.inc

. $WXROOT/distrib/scripts/mac/mac_options.inc

OTHER_OPTS="$OTHER_OPTS --disable-precomp-headers"

do_lipo_build(){
    DIR="bld-$1"
    if [ "$1" = "ppc" ]; then
	ARCH=ppc
    else
	ARCH=i386
    fi
    if [ "$1" = "ppc" -a "$PPC_USE_10_4" = "" ]; then
        export CXX="g++-3.3 -arch $ARCH -DMAC_OS_X_VERSION_MAX_ALLOWED=1040"
        export CC="gcc-3.3 -arch $ARCH -DMAC_OS_X_VERSION_MAX_ALLOWED=1040"
        export MACOSX_DEPLOYMENT_TARGET=10.3
    else
        export CXX="g++-4.0 -arch $ARCH"
        export CC="gcc-4.0 -arch $ARCH"
        export MACOSX_DEPLOYMENT_TARGET=10.4

	if [ "$WXPYTHON" == "1" ]; then
	    OTHER_OPTS="$OTHER_OPTS --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk"
	fi
    fi
    
    mkdir -p $DIR
    cd $DIR
    do_build

    mkdir -p $INSTALLDIR/$1
    
    make prefix=$INSTALLDIR/$1 install

    if [ "$WXPYTHON" == "1" ]; then
        make -C contrib/src/gizmos prefix=$INSTALLDIR/$1 install
        make -C contrib/src/stc prefix=$INSTALLDIR/$1 install
    fi

    cd ..
}

do_lipo_build "ppc"
do_lipo_build "i386"

python $WXROOT/distrib/scripts/mac/lipo-dir.py $INSTALLDIR/ppc $INSTALLDIR/i386 $INSTALLDIR    

rm -rf $INSTALLDIR/ppc $INSTALLDIR/i386
python -c "import os; fname = os.path.abspath('$INSTALLDIR/bin/wx-config'); data = open(fname).read(); data = data.replace('ppc/', ''); open(fname, 'w').write(data)"

if [ $? != 0 ]; then
    exit $?
fi