#!/usr/bin/env bash
#
# Wrapper script for Eternal Lands client.
# - Checks the el packages are configured OK
# - Copies required files to ~/.elc if not already there
# - Checks the data_dir in "to be used" el.ini file and optionally corrects
# - Optionally displays a server/configuration selection GUI
# - Optionally runs a user specified client executable
# - If a crash is detected, help is offered
#
# Author Paul Broadhead pjbroad@twinmoons.org.uk
#
# This program (script) is released as Public Domain software.  You are free to
# use it as you wish.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

# get users choice from the $serverfile
getconfig()
{
	sort -u $serverfile | tr -d '\015' | grep -v -e ^$ -e ^# | \
	while read name dir server port description
	do
		echo $name
		echo $description
		echo $server
		echo $port
		echo $dir
	done | \
	zenity --title="Eternal Lands Launcher${non_standard}" --width=450 --height=280 \
		--list \
		--column "Name" \
		--column "Description" \
		--column "Server" \
		--column "Port" \
		--column "Directory" \
		--text "Choose the Server/Configuration - main is the standard"
}

# don't run as root
if [ "$(id -u)" = "0" ]
then
	text="Don't run as the root/adminstration user.\n\nJust don't!"
	echo -e $text
	zenity --error --text="$text"
	exit
fi

# make sure the user config tree exists
mkdir -p ~/.elc/

# copy a servers.lst file if we don't have one
serverfile=~/.elc/servers.lst
if [ ! -r $serverfile -o ! -s $serverfile ]
then
	cp -p /usr/share/games/eternallands/servers.lst $serverfile
fi
chmod +wr $serverfile

# see if we intend to use non-standard build
el_client=/usr/games/el.x86.linux.bin
if [ -r ~/.elc/el_client ]
then
	tmp_el_client="$(cat ~/.elc/el_client)"
	if [ -x "$tmp_el_client" ]
	then
		non_standard=" (non-standard)"
		el_client="$tmp_el_client"
	fi	
fi

# if no command line options specified, see if we want to use the config selection window
if [ "$*" = "" ]
then
	showsel=$HOME/.elc/showconfigsel

	# optionally get the server/config option
	[ ! -r $showsel ] && touch $showsel
	if [ -n "$(grep yes $showsel)" -o ! -s $showsel ]
	then
		[ ! -s $showsel ] && rm -f $showsel
		config="$(getconfig)"
		[ $? -ne 0 ] && exit
	else
		config="$(cat $showsel)"
		[ "$config" = "no" ] && config="main"
	fi

	# if not already configured, choose if to use the server/configuration selection window
	if [ ! -r $showsel ]
	then
		if zenity --question --text="Do you want to choose each time you run the game?"
		then
			echo "yes" > $showsel
		else
			echo "$config" > $showsel
		fi
		zenity --info --text="To change this option, edit or remove the file:\n $showsel"
	fi
else
	config="$1"
	shift
fi

# make sure we have the config directory
config="$(basename $config)"
mkdir -p ~/.elc/${config}/
inifile=~/.elc/${config}/el.ini

# copy an el.ini file if we don't have one
if [ ! -r $inifile -o ! -s $inifile ]
then
	cp -p /usr/share/games/eternallands/el.ini $inifile
fi
chmod +wr $inifile

# check the chosen el.ini file points to installed data
if [ ! -r ~/.elc/no.el.ini.check -a -n "$config" ]
then
	show_suppression=false
	if [ "$(grep ^#data_dir $inifile | tail -1 | grep '^#data_dir = /usr/share/games/eternallands$')" = "" ]
	then
		if zenity --question --text="Warning: $inifile does not use installed game data, shall I fix this problem?"
		then
			sed -i 's/^#data_dir.*$/#data_dir = \/usr\/share\/games\/eternallands/g' $inifile
		else
			show_suppression=true
		fi
	fi
	if $show_suppression
	then
		zenity --info --text="To surpress these warnings, create the file:\n $HOME/.elc/no.el.ini.check"
	fi
fi

# run the client, display error log if it crashes
if ! $el_client $config $* &> ~/.elc/terminal_log.txt
then
	cat | zenity --text-info --width=500 --height=300 --title "Eternal Lands Crashed, this is the Error log." << EOT
Oh my, Eternal Lands Crashed - Don't Panic!

A few of things you can try:
1) Make sure your system is up to date, especially your video drivers.
2) If the client crashes repeatedly, try changing some settings.  Either by directly modifying the configuration file (e.g. gedit ~/.elc/main/el.ini). Or clicking the settings button window at the login screen.
	i) Enable the "Poor Man" option in the "Details" tab of the setting window.  Or edit the configuration file and set "#poor_man=1".
	ii) Disable the "Use animation program" option in the "Adv Video" tab of the setting window.  Or edit the configuration file and set "#use_animation_program=0".

If you continue to have problems check the Eternal Lands forums, especially the "Help Me" and "Bug Report" sections.  If you post a message make sure you include information about your system; i.e video driver and version, operating system and version.  Make sure you check your error log for clues too and include the contents in any forum report.

This information about your graphic hardware and software may help.
Output from "lspci | grep -i vga":
$(lspci | grep -i vga)
Output from "glxinfo | head -5":
$(glxinfo | head -5)

The following is your error log contents.  Note it is overwritten each time you run the game.

$(cat $(ls -1tr ~/.elc/log/main*.log | tail -1))

This was the program output:  Note it is overwritten each time you run the game.

$(cat ~/.elc/terminal_log.txt)

A search through dmesg output:
$(dmesg | grep $(basename $el_client))

EOT
fi
