#!/bin/bash

# Display EULA
if [ ! -e .agreedToEULA ]; then
	echo "Showing EULA..."	
	./showterm ./showEULA
	if [ ! -e .agreedToEULA ]; then
		exit 1
	fi
fi

echo "Starting game..."

cd ../game

ls *.exe

# Try launching game as-is (find exe and execute it)
./$(ls *.exe) &>../launcher/output.txt
result=$?

if [ $result != 0 ]; then
	# Something went wrong, see if it has to do with SDL
	cat ../launcher/output.txt
	grep libSDL ../launcher/output.txt
	if (($? == 0)); then
		# Problem with SDL
		echo "Game execution failed. Trying with provided libraries..."
		export LD_LIBRARY_PATH="../lib"
		./$(ls *.exe) &>../launcher/output.txt
		result=$?
	fi
fi

if [ $result != 0 ]; then
	# Something is (still) wrong, give up
	cd ../launcher
	./showterm ./showError
fi


