#!/bin/sh

# Hopefully newbie-proof executable selector script,
# with a few other tasks. Checks in a crude way if
# the game data folders an be found, updates outdated mods,

# Based on the World of Goo launcher script.
# -- fg



hascmd() {
	# because `which` is non-POSIX
	type "$1" >/dev/null 2>&1
	return $?
}

# title, text, detail
msgbox()
{

echo "
#-------[ $1 ]--------
# $2
#---------------------------------
$3

"
if [ -n "$DISPLAY" ]; then
	if hascmd kdialog; then
		kdialog --error "$2\n\n$3" --title "$1" 2>/dev/null # kdialog auto-expands \n
	elif hascmd zenity; then
		zenity --error --title="$1" --text="$2\n\n$3" # zenity auto-expands \n
	elif hascmd wish; then
		echo "tk_messageBox -type ok -title \"$1\" -message \"$2\" -icon error -detail \"$3\"; exit" | wish
		#echo "pack [label .msg -text \"$1\n\n$2\n\n$3\"]; pack [button .submit -text \"Close\" -command { exit }]" | wish
	elif hascmd xmessage; then
		echo -e "$1\n\n$2\n$3" | xmessage -file -
	fi
fi
}

BINDIR=`readlink -f "$0"`
BINDIR=`dirname "$BINDIR"`

# Find data directory
if [ -n "$AQUARIA_DATA_PATH" ]; then
	DATADIR="$AQUARIA_DATA_PATH"
else
	DATADIR="$BINDIR"
fi


NEED_DIRS=""
check_dir() {
	if [ ! -d "$DATADIR/$1" ]; then
		NEED_DIRS="$NEED_DIRS
>> $1"
		return 1
	fi
}
check_dir data
check_dir gfx
check_dir mus
check_dir scripts
check_dir sfx
check_dir vox

if [ -n "$NEED_DIRS" ]; then
msgbox  "Missing data!" "Your installation is incomplete!" "
(Checked directory: $DATADIR )

Some data folders were not found or are missing:
$NEED_DIRS

Either throw this update pack into your Aquaria installation directory,
or set the AQUARIA_DATA_PATH environment variable,
pointing to the folder where the game files are.
To skip this check, simply run aquaria32 or aquaria64 directly."
exit 1
fi


# The game respects AQUARIA_DATA_DIR, so we go where the binaries are
cd "$BINDIR"

# check for outdated default mods
if [ -d _mods ]; then
	MODSDIR="$HOME/.Aquaria/_mods"
	MODVERFILE="$MODSDIR/._fg_versionmarker"
	if [ -f "$MODVERFILE" ]; then
		MODSVER=`cat "$MODVERFILE"`
	else
		MODSVER=0
	fi
	if [ $MODSVER -lt 2 ]; then
		echo "Updating outdated default mods..."
		cp -r -f -v _mods "$HOME/.Aquaria/"
		echo "2" > "$MODVERFILE"
	fi
fi

MACHINE=`uname -m`
if [ "$MACHINE" = x86_64 ]
then
	EXE="./aquaria64"
else
	EXE="./aquaria32"
fi


GAMELOG="$HOME/.Aquaria/debug.log"
LOGPATH="$HOME/.Aquaria/crash.log"
ERRLOG="$HOME/.Aquaria/stderr.log"

# Run the game
$EXE $@ 2> $ERRLOG

# Check for errors
e=$?
if [ $e -ne 0 ]
then
	echo "Abnormal termination?!"
	echo "Creating log file at: $LOGPATH"
	echo "===== Start of log =====" > $LOGPATH
	
	if [ -f "$LOGPATH" ]; then
		if [ -f "$ERRLOG" ]; then
			echo "## Stderr:" >> $LOGPATH
			cat "$ERRLOG" >> $LOGPATH
			echo "" >> $LOGPATH
		fi
		
		if [ -f "$GAMELOG" ]; then
			echo "## Main game log:" >> $LOGPATH
			cat "$GAMELOG" >> $LOGPATH
			echo "" >> $LOGPATH
		fi

		echo "" >> $LOGPATH
		echo "===== End of game log =====" >> $LOGPATH
		echo "" >> $LOGPATH
		
		echo "## File:" >> $LOGPATH
		file "$EXE" >> $LOGPATH 2>&1
		file -L "$EXE" >> $LOGPATH 2>&1 # just in case
		echo "" >> $LOGPATH
		
		# it may refuse to start with "No such file or directory"
		# this here helps to see if the files are actuially there
		# or if it is a dynamic loader problem
		echo "## Files:" >> $LOGPATH
		ls -lsH >> $LOGPATH 2>&1
		echo "" >> $LOGPATH
		
		echo "## LDD output:" >> $LOGPATH
		ldd "$EXE" >> $LOGPATH 2>&1
		echo "" >> $LOGPATH
		
		echo "## System:" >> $LOGPATH
		uname -a >> $LOGPATH
		echo "" >> $LOGPATH
		
		echo "## Memory info:" >> $LOGPATH
		cat /proc/meminfo >> $LOGPATH
		echo "" >> $LOGPATH

		if hascmd glxinfo; then
			echo "glxinfo not found!" >> $LOGPATH
		else
			echo "Output of glxinfo:" >>$LOGPATH
			glxinfo >>$LOGPATH 2>&1
		fi
		
	else
		echo "Something is wrong, no log file has been created!"
		LOGPATH="-- error, no log file created :("
	fi
	


msgbox "Oops!" "It looks like Aquaria crashed!" "A log file has been created at:

$LOGPATH

Note that this is not an offcial version.
If you want to help fixing the problem,
send a report to the following email address,
possibly with the log attached:

    false.genesis@googlemail.com

Thanks!"

fi

exit $e
