#!/bin/sh

# Uncomment the next line for debuging
#set -x

XCODE_PROJ_NAME=Proj.xcodeproj

#only execute if the script is called in the BuildMac folder
if [ "$0" == "./compile.sh" ]; then

	#Parse arg1 into the config
	ConfigArg=`echo "$1" | tr '[A-Z]' '[a-z]'`
	Config="bad config"

	case "$ConfigArg" in
		"debug_x86_64")
			Config="Debug_x86_64"
			;;
		"debug_i386")
			Config="Debug_i386"
			;;
		"debug_ppc")
			Config="Debug_ppc"
			;;
		"release_x86_64")
			Config="Release_x86_64"
			;;
		"release_i386")
			Config="Release_i386"
			;;
		"release_ppc")
			Config="Release_ppc"
			;;
	esac

	if [ "$Config" == "bad config" ]; then
		echo error : unknown config $1
		exit 1
	fi

	#Give arg 2 to xcodebuild as the build action
	BuildAction=$2

	#Load the list of projets
	. ./setProjList.sh

	#Setup CARRARASDK path
	cd ..
	CARRARASDK=`pwd`
	
	#Keep track of failures
	FAILED_LIST=""
	
	for projPath in $PROJ_LIST ;
	do
		CUR_PROJ="$CARRARASDK/Samples/$projPath"
		
		pushd "$CUR_PROJ" > /dev/null
		
		xcodebuild -project $XCODE_PROJ_NAME -target Myextension -configuration $Config $BuildAction
		
		if [ "$?" == "1" ]; then
			FAILED_LIST="$FAILED_LIST $projPath"
		fi
		
		popd > /dev/null
	done

	#print compile report
	echo ""
	echo ""

	for failProj in $FAILED_LIST ;
	do
		echo "Failed: $CARRARASDK/Samples/$failProj/$XCODE_PROJ_NAME"
	done

	echo ""
	
else
	echo "This script must be called from the BuildMac folder: ./compile.sh"
fi


