#!/bin/bash

# Setup:
# You need to compile the bin/data directory that you want to use, and set
# OPENTTDDATADIRECTORY to it.
# You need to choose a base directory for the installations; they will be
# placed in base/openttd-______.  This is OPENTTDDIRECTORY.
# Finally, if you want to automagically connect to the #openttdcoop public
# server, you need to change OPENTTDCOOPKEY to your key.

# If there are any bugs, I'm JamesGo in #openttdcoop.


# To get a specific version:
# getopenttd [trunk|stable|cargodist] version

# To start playing on the #openttdcoop public server (downloading if nessecary)
# getopenttd openttdcoop

if [ $# == 0 ]
then
	echo
	echo "getopenttd usage:"
	echo
	echo "To get a specific version:"
	echo "getopenttd [trunk|stable|cargodist] version"
	echo
	echo "To start playing on the #openttdcoop public server (downloading if nessecary)"
	echo "getopenttd openttdcoop"
	echo
fi

OPENTTDDIRECTORY="$HOME/tmp"
OPENTTDDATADIRECTORY="$OPENTTDDIRECTORY/openttd-data" # bin/data
OPENTTDCOOPKEY="" # OPENTTDCOOPKEY="keys/Name.abcdef"


cd $OPENTTDDIRECTORY

if [ "$1" == "trunk" ]
then
	echo "Downloading trunk"
	wget http://binaries.openttd.org/nightlies/trunk/$2/openttd-trunk-$2-source.tar.gz
	tar -xvf openttd-trunk-$2-source.tar.gz
	cd openttd-trunk-$2
fi
if [ "$1" == "stable" ]
then
	echo "Downloading stable"
	wget http://binaries.openttd.org/releases/$2/openttd-$2-source.tar.gz
	tar -xvf openttd-$2-source.tar.gz
	cd openttd-$2
fi
if [ "$1" == "cargodist" ]
then
	echo "Downloading cargodist"
	wget http://bundles.openttdcoop.org/cargodist/$2/openttd-cargodist-$2-source.tar.gz
	tar -xvf openttd-cargodist-$2-source.tar.gz
	cd openttd-cargodist-$2
fi

if [[ "$1" == "trunk" || "$1" == "stable" || "$1" == "cargodist" ]]
then
	./configure
	make -j16
	cd bin
	cp -r $OPENTTDDATADIRECTORY ./data
	cd ../../
fi

if [ "$1" == "openttdcoop" ]
then
	echo "Connecting to #openttdcoop public server"
	VERSION=`curl http://ps.openttdcoop.org/public/finger/openttd|cut -s -f1`
	if [ ! -d openttd-trunk-$VERSION ]
	then
		getopenttd trunk $VERSION
	fi
	cd openttd-trunk-$VERSION/bin
	./openttd -n ps.openttdcoop.org:3979\#1 -p `curl http://ps.openttdcoop.org/3979/$OPENTTDCOOPKEY`
fi

