#!/bin/bash

if [ -r ./configure ]; then
	echo "Configure found!"
else
	echo "No Config file found! Runing autogen.sh..."
	sh autogen.sh
fi

if [ -r ./config.guess ]; then
	echo "config.guess found!"
else
	echo "config.guess not present! Copying it..."
	cp /usr/share/libtool/config.guess ./config.guess
fi

if [ -r ./config.sub ]; then
	echo "config.sub found!"
else
	echo "config.sub not present! Copying it..."
	cp /usr/share/libtool/config.sub ./config.sub
fi

if [ -r ./Makefile ]; then
	echo "Makefile found!"
else
	echo "Makefile missing! Running ./configure..."
	./configure
	if [ -r ./Makefile ]; then
		echo "Makefile present, ready to compile!"
	else
		echo "Configure not completed, Makefile still missing! Exiting..."
		exit 1
	fi
fi

echo "Setting environment variables..."
export sdl_LIBS=""
export LDFLAGS="-framework OpenAL -Wl,-framework,Cocoa -framework SDL -framework Cocoa -lSDLmain  -framework Vorbis -framework Ogg -L/usr/local/lib -L/opt/local/lib -L/sw/lib"
export openal_LIBS="/usr/local/lib/libmikmod.a"

if [ -r ./bin/supertuxkartPPC ]; then
	echo "supertuxkartPPC is present, nothing to do."
else
	echo "Cleaning up..."
	make clean -s

	echo "Building PPC Binary..."
	make -e -s

	echo "Copying PPC Binary..."
	mkdir ./bin
	if [ -r ./src/supertuxkart ]; then
		cp ./src/supertuxkart ./bin/supertuxkartPPC
		else
		echo "Error!"
		exit 1
	fi
fi

if [ -r ./bin/supertuxkartx86 ]; then
	echo "supertuxkartx86 is present, nothing to do."
else
	echo "Cleaning up..."
	make clean -s

	echo "Building x86 Binary..."
	export CXXFLAGS="-g -O2 -Wall -arch i386 `sdl-config --cflags`"
	export LDFLAGS=$LDFLAGS" -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -L/usr/local/lib -L/opt/local/lib -L/sw/lib"
	make -e -s

	echo "Copying x86 Binary..."
	if [ -r ./src/supertuxkart ]; then
		cp ./src/supertuxkart ./bin/supertuxkartx86
		else
		echo "Error!"
		exit 1
	fi
fi

echo "Creating Universal Binary..."
lipo -create ./bin/supertuxkartPPC ./bin/supertuxkartx86 -output ./bin/supertuxkart

echo "Done!"



