#!/bin/bash

##
# buildForMac                     <configure-options>
# example:  contrib/buildForMac --[dis|en]able-shared
#
##

##
# test OS level (L=9 SL==10 Lion = 11, ML=12)
LION=""
let os=$(uname -a | cut -d' ' -f 3 | cut -d'.' -f 1)
if [ $(( os >= 11 )) ]; then
  	LION="--with-zlib=/usr/lib"
fi

if [ ! -d contrib ]; then
	echo "you are in the wrong directory - please run $0 in the main directory (which includes contrib and src)"
	exit 1
fi
if [ ! -e configure ]; then make config ; fi
if [ -z $TARGET ]; then export TARGET="MACOSX_DEPLOYMENT_TARGET=10.5" ; fi

# version=$(grep EXIV2_LTVERSION config/config.mk | cut "-d " -f 3 | cut -d: -f 1)
# lib=libexiv2.$version.dylib

build() {
	make clean
	./configure CFLAGS="$arch" CXXFLAGS="$arch" LDFLAGS="$arch -L${PWD}/xmpsdk/src" OBJCFLAGS="$arch" OBJCXXFLAGS="$arch" $TARGET $LION "$@"
	make        CFLAGS="$arch" CXXFLAGS="$arch" LDFLAGS="$arch -L${PWD}/xmpsdk/src" OBJCFLAGS="$arch" OBJCXXFLAGS="$arch" $TARGET all
	# make        CFLAGS="$arch" CXXFLAGS="$arch" LDFLAGS="$arch -L${PWD}/xmpsdk/src" OBJCFLAGS="$arch" OBJCXXFLAGS="$arch" $TARGET samples
	if [ "$?" != "0" ]; then
		echo build failed
		exit
	fi
}

##
# build one architecture at a time
for arch in '-arch i386' '-arch x86_64' ; do
	echo arch = $arch
	a=${arch:6:100}
	build "$@"

	##
	# copy .libs to .arch
	for d in $(find . -name ".libs" -type d); do
		D=$(dirname $d)/.$a
		ditto $d $D
	done 
done

##
# combine the builds into a universal
$(dirname $0)/makeUniversal

# That's all Folks!
##
