#!/bin/sh

# Lines to customize BEGIN
COMPANY_NAME="Visionaire-Studio"
GAME_NAME="GAME_NAME"
#export SDL_DYNAMIC=/my/actual/libSDL-2.0.so.0

# Lines to customize END

if [ "$XDG_DATA_HOME" = "" ]; then
  export XDG_DATA_HOME="$HOME/.local/share"
fi

LOGPATH="$XDG_DATA_HOME/$COMPANY_NAME/$GAME_NAME/messages.log"

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

if [ ! -e libs64 ] || [ ! -e config.ini ]
then
	echo "Missing libs64/ and config.ini 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=./visplayer
else
	echo "unsupported architecture $MACHINE"
	exit 1
fi

# Run the game:
export LD_LIBRARY_PATH=$LIBS:"$LD_LIBRARY_PATH"
if [ -z "$SDL_DYNAMIC" ]
then
	export SDL_DYNAMIC_API=$SDL_DYNAMIC
fi
$BIN $@

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

	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

