#!/bin/bash

#
# This script gets all needed packages, builds LOVE, makes deb and tar.gz
# and optionally uploads them to servers.
#
# Note: this script must be called in the same directory as the love
# folder resides. It also assumes that love has been checked out previously.
#
# Usage:
#
# autobuild [action [revision [version]]]
# 
# action:
#
# There are only two recognized actions:
#
# build -- Causes files to be uploaded to tehlol servers.
# release -- Coming soon.
#
# All other actions will be ignored, so if you don not wish to upload
# files (but still build LOVE), you can write "test", for instance. 
#
# revision:
#
# The revision that should be built. Should be a valid revision 
# number or tip. If not specified, it defaults to tip.
#
# version: 
#
# The display version that will appear in files, eg. love-version.tar.gz.
# If not specified, the script defaults to use the SVN revision number.
#
# Examples:
#
# autobuild
# (Builds HEAD, but does not upload)
#
# autobuild build
# (Builds tip and uploads to tehlol server with the current rev. as
# version number)
#
# autobuild build tip 1.0
# (Builds tip and uploads it using the version 1.0)
#
#
# Passwords for servers must be in these external files:
#
# tehlol.com: 		pwtehlol
# sourceforge.net: 	pwsourceforge
#

cd ../..

# Get required packages.
#sudo apt-get -y install subversion build-essential liblua5.1-dev \
#libopenal-dev libsdl1.2-dev libfreetype6-dev \
#libphysfs-dev libdevil-dev libtiff4-dev libmng-dev \
#liblcms1-dev ftp-upload libmpg123-dev libmodplug-dev libpng12-dev

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

echo $buildrev

# Upload to the appropriate revision.
hg pull
hg update -r $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 --template "{node|short}"`
		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

echo $displayversion

# Update version in configure.
# cat configure.in | sed "s/LOVE_VERSION/$displayversion/g" > configure.in
head -c 15 configure.in > configure.in.tmp
echo " [$displayversion])" >> configure.in.tmp
tail -n +2 configure.in >> configure.in.tmp
cp configure.in.tmp configure.in
rm configure.in.tmp

# Build ... BUILD!
#sh platform/unix/gen-makefile
sh platform/unix/automagic
./configure
make
make dist

# Move and rename the tar.
tar="love-$displayversion-linux-src.tar.gz"
mv "love-$displayversion.tar.gz" platform/unix/$tar

# Create the deb.
cd platform/unix
sh make-package deb $displayversion

machine=`uname -m`

# Move and rename the deb.
deb="love-$displayversion-ubuntu-$machine.deb"
mv "love-$displayversion.deb" $deb

# Copy and rename the binary.
binary="love-$displayversion-linux-$machine"
cp ../../src/love $binary

# Deal with uploading.
#if [ "$1" == "build" ]; then
#	curl -F build=@$deb -F press=ok http://love2d.org/builds/upload.php?upload
#	curl -F build=@$tar -F press=ok http://love2d.org/builds/upload.php?upload
#	curl -F build=@$binary -F press=ok http://love2d.org/builds/upload.php?upload
#fi

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