#!/bin/bash

cd ../..

# Check which revision to build.
if [ -z $2 ]; then
	buildrev="tip"
else
	buildrev=$2
fi

# Update to the appropriate revision.
hg pull
hg update $buildrev

# Set the displayversion.
if [ -z $3 ]; then
	if [ "$buildrev" == "tip" ]; then
		# Get the current SVN version, use sed to remove any non-numbers.
		currentversion=`hg log -l1 | grep changeset | sed 's/.*://g'`
		currentdate=`date +%Y%m%d`
		displayversion="$currentdate-$currentversion"
	else
		# The revision is already specified, so we'll use that as the 
		# display version.
		displayversion="$buildrev"
	fi
else
	# If the param is present, it overrides everything else.
	displayversion=$3
fi

cd platform/macosx

outfile="love-$displayversion-macosx-ub"
outdmg="$outfile.dmg"
outlog="$outfile.log"

xcodebuild > $outlog 2>&1

cp love.dmg $outdmg

# Deal with uploading.
if [ "$1" == "build" ]; then
	pass=`cat pwtehlol`
	ftp -u ftp://love2d:$pass@love2d.org/public_html/builds/files/$outdmg $outdmg
	ftp -u ftp://love2d:$pass@love2d.org/public_html/builds/files/$outlog $outlog
fi

if [ "$1" == "release" ]; then
	echo "release"
fi

