#!/bin/sh

# Change to game directory
CANONPATH=`readlink -f "$0"`
cd "`dirname "$CANONPATH"`"

if [ ! -e properties ] || [ ! -e res ]
then
	echo "Missing properties/ and res/ directories in `pwd`"
	echo "Your installation is incomplete!"
	exit 1
fi

# Uncomment the line below to dump core when the game crashes; useful for
# debugging, but only works if the game directory is user-writable!
#ulimit -c unlimited

MACHINE=`uname -m`
if [ "$MACHINE" = x86_64 ]
then
	LIBS=./libs64
	BIN=./WorldOfGoo.bin64
else
	LIBS=./libs32
	BIN=./WorldOfGoo.bin32
fi

# Run the game:
export LD_LIBRARY_PATH=$LIBS:"$LD_LIBRARY_PATH"
$BIN $@

# Check for errors
e=$?
if [ $e -ne 0 ]
then
	echo ""
	echo "It looks like World of Goo crashed! If you need support, please include the"
	echo "contents of the log file in your problem report."

	LOGPATH="$HOME/.WorldOfGoo/WorldOfGoo.log"
	if [ -f "$LOGPATH" ]
	then
		echo "The log file is stored at: $LOGPATH"

		echo "" >> $LOGPATH
		echo "Libraries used:" >> $LOGPATH
		ldd $BIN >> $LOGPATH 2>&1

		echo "" >> $LOGPATH
		GLXINFO=`which glxinfo`
		if [ -z "$GLXINFO" ]
		then
			echo "glxinfo not found!" >> $LOGPATH
		else
			echo "Output of glxinfo:" >>$LOGPATH
			glxinfo >>$LOGPATH 2>&1
		fi
		
	else
		echo "Unfortunately, no log file has been created!"
	fi
fi

exit $e
