#!/bin/bash

# Error messages
function err_missing_glxinfo {
	echo "ERROR: GLXINFO not found. Please check your X11 and/or MESA/OpenGL setup."
	exit 1
}

function err_nodri {
	echo "ERROR: Direct Rendering is not enabled on your system."
	echo "       Please check your video driver installation!"
	exit 2
}

function err_missing_wget {
	echo "ERROR: WGET is required. Please install this."
	exit 3
}

function err_missing_unzip {
	echo "ERROR: UNZIP is required. Please install this."
	exit 4
}

function err_missing_md5 {
	echo "ERROR: MD5SUM is required. Please install this."
	exit 5
}

function err_permission_denied {
	echo "ERROR: No write permission in current path. Please change directory."
	exit 6
}

function err_mkdir {
	echo "ERROR: No permission to make ${destpath}."
	exit 7
}

function err_extract {
	echo "ERROR: Archive extraction failure."
	exit 8
}

function err_be_update {
	echo "ERROR: Failed retrieving BattleEye updates."
	return 9
}

function err_download {
	echo "ERROR: WGET failed downloading."
	exit 10
}

# Greeting banner
function urtinst_banner {
	cat << .end
 _   _     _             _____                      _ _   __  
| | | |_ _| |__  __ _ _ |_   _|__ _ _ _ _ ___ _ _  | | | /  \ 
| |_| | '_| '_ \/ _\` | ' \| |/ -_) '_| '_/ _ \ '_| |_  _| () |
 \___/|_| |_.__/\__,_|_||_|_|\___|_| |_| \___/_|     |_(_)__/ 
                                                    installer

 This program will help install UrbanTerror 4 on your system.

 Press enter key to continue or control-c now to abort
.end
	read anykey
	# path of where we started
	basepath=`pwd`
	# test or we have write permission
	writetest=`touch .urtinstaller` || err_permission_denied
	if [ "$1" != "-server" ]
		then
		# find glxinfo binary location
		glxinfo_bin=`which glxinfo` || err_missing_glxinfo
		# test or DRI is enabled
		is_dri=`${glxinfo_bin} | sed '/direct/!d;s/^.*: //'`
		if [ ${is_dri} != "Yes" ]
			then
			err_nodri
		fi
	fi
	# test or wget is available
	wget_bin=`which wget` || err_missing_wget
	# test or unzip is available
	unzip_bin=`which unzip` || err_missing_unzip
	# test or md5sum is available 
	md5sum_bin=`which md5sum` || err_missing_md5
}

# Get required files
iob_zip="ioUrbanTerror_Installer_1.0.zip"
urt_zip="UrbanTerror40_full.zip"

function has_iob {
	# if installer status file exist just continue the process
	if [ -f .urtinstaller ]
		then
		get_iob
	fi
	# first time run, but if there is already an existing file, we should test it
	if [ -f ${iob_zip} ]
		then
		testmd5_iob
	else
		get_iob
	fi
}

function get_iob {
	iob_mirror_url="ftp://ftp.snt.utwente.nl/pub/games/urbanterror/iourbanterror/ioUrbanTerror_Installer_1.0.zip"
	echo "Downloading ioUrbanterror binaries ..."
	${wget_bin} -c ${iob_mirror_url} || err_download
}

function testmd5_iob {
	iob_md5_correct="b04c192eebdee5a4be6f33954e0723b7"
	echo -n "Verifying ${iob_zip} integrity ..."
	iob_md5=`${md5sum_bin} ${iob_zip} | sed 's/\ .*//'`
	if [ ${iob_md5} != ${iob_md5_correct} ]
		then
		echo "FAILED!"
		echo "ERROR: Integrity seems corrupted. Removing locale copy."
		rm ${iob_zip}
		get_iob
	else
		echo "OK"
		echo "need-gamedata" > .urtinstaller
	fi
}

function has_urt {
	# continue getting gamedata file if our file is still intact
	if [ -f .urtinstaller ]
		then
		get_urt
	fi
	# test the md5sum if there is already a gamedata zip file
	if [ -f ${urt_zip} ]
		then
		testmd5_urt
	else
		get_urt
	fi
}

function show_mirrors {
	cat << .end
==> List of UrbanTerror4 gamedata downloads host:
-------------------------------------------------
[1] (USA) Dreamhost
[2] (USA) Mr.Sentry
[3] (Europe, NL) UT TWente
[4] (Europe, PL) Vectranet
.end
}

function get_mirror {
	echo -n "Enter a number for download location: "
	read dlhost
	case "$dlhost" in
		"1")
			urt_mirror_url="http://mirrormaid.yi.org/UrbanTerror40_full.zip"
			;;
		"2")
			urt_mirror_url="http://mrsentry.net/release/UrbanTerror40_full.zip"
			;;
		"3")
			urt_mirror_url="ftp://ftp.snt.utwente.nl/pub/games/urbanterror/UrbanTerror40_full.zip"
			;;
		"4")
			urt_mirror_url="ftp://ftp.vectranet.pl/pub/gry/UrbanTerror/UrbanTerror40_full.zip"
			;;
		*)
			echo "Invalid option"
			get_mirror
			;;
	esac
}

function get_urt {
	show_mirrors
	get_mirror
	echo "Downloading UrbanTerror4 gamedata  ..."
	${wget_bin} -c ${urt_mirror_url} || err_download
	# ERRLVL 130 = aborted
}

function testmd5_urt {
	urt_md5_correct="4aec86405b548b041686f92eb584cbf9"
	echo -n "Verifying ${urt_zip} integrity ..."
	urt_md5=`${md5sum_bin} ${urt_zip} | sed 's/\ .*//'`
	if [ ${urt_md5} != ${urt_md5_correct} ]
		then
		echo "FAILED!"
		echo "ERROR: Integrity seems corrupted. Removing locale copy."
		rm ${urt_zip}
		get_urt
	else
		echo "OK"
	fi
}

urtinst_banner
has_iob
has_urt

# Extract the files
destpath=""

function extract {
	if [ `id -u` != 0 ]
		then
		destpath="${HOME}/Games/UrbanTerror4"
		binpath="${HOME}"
		fdopath="${HOME}/.gnome2/vfolders/applications"
	else
		destpath="/usr/local/games/UrbanTerror4"
		binpath="/usr/local/bin"
		fdopath="/usr/local/share/applications"
	fi
	extract_iob
	extract_urt
}

function extract_iob {
	mkdir -p ${destpath} || err_mkdir
	echo "Extracting Linux ELF binaries ..."
	${unzip_bin} -u -d ${destpath} ${iob_zip} *.txt || err_extract
	${unzip_bin} -u -d ${destpath} ${iob_zip} Linux-i386/* || err_extract
	mv -v ${destpath}/Linux-i386/* ${destpath}
	chmod +x ${destpath}/io*i386
	rmdir ${destpath}/Linux-i386
}

function extract_urt {
	echo "Extracting game files ..."
	${unzip_bin} -u -d ${destpath} ${urt_zip} || err_extract
}

function make_start_script {
	echo -n "Making a start up script ..."
	cat << .end > ${binpath}/urbanterror4 && echo "OK"
#!/bin/sh
# Needed to make symlinks/shortcuts work.
# the binaries must run with correct working directory
cd "${destpath}"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
./ioUrbanTerror.i386 "\$@"
.end
	chmod +x ${binpath}/urbanterror4
}

function make_desktop_icon {
	echo -n "Making a desktop icon ..."
	cat << .end > ${HOME}/Desktop/q3ut4.desktop && echo "OK"
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=UrbanTerror 4
Exec=${binpath}/urbanterror4
Icon=${destpath}/iourbanterror.png
Comment=Urban Terror 4
Terminal=false
Type=Application
.end
}

function make_menu_item {
	echo -n "Making a menu item ..."
	cat << .end > ${fdopath}/q3ut4.desktop && echo "OK"
[Desktop Entry]
Encoding=UTF-8
Name=UrbanTerror 4
Comment=Free modern FPS shooter game for multiplayer
Comment[en_US]=Free modern FPS shooter game for multiplayer
Comment[nl]=Gratis moderne FPS schietspel voor multiplayer
Exec=${binpath}/urbanterror4
Icon=${destpath}/iourbanterror.png
Categories=Application;Games;Game;X-Red-Hat-Base;
Terminal=false
Type=Application
.end
}

function make_clickables {
	echo -n "Do you want me to create a desktop icon and menu item [Y/n]? "
	read doicons
	case "$doicons" in
		"Y")
			make_desktop_icon
			make_menu_item
			;;
		"n")
			echo
			;;
		"N")
			echo
			;;
		*)
			echo "Invalid option"
			make_clickables
			;;
	esac
}

extract
make_start_script
make_clickables

# BattleEye update

function be_update {
#	be_client_so="http://www.battleye.com/urt/BEClient_i386.so"
#	be_server_so="http://www.battleye.com/urt/BEServer_i386.so"
#	cd ${destpath}/BattlEye
#	echo "Updating BattleEye files ..."
#	${wget_bin} -c ${be_client_so} ${be_server_so} || err_be_update
	echo "BattleEye support for Urban Terror has been terminated."
	echo "Please disable BE in your game setting!"
}

# Download 3rd party (community) maps
cm_mirror_url=""

function community_maps {
	echo -n "Do you wish me to download some community maps [Y/n]? "
	read mapdl
	case "$mapdl" in
		"Y")
			list_cm_download_mirrors
			get_cm_download_mirrors
			;;
		"n")
			echo "Make sure you enable map downloading in:"
			echo "Setup > Game Options > General Settings > Auto Download"
			;;
		*)
			echo "Invalid option"
			community_maps
			;;
	esac
}

function list_cm_download_mirrors {
	cat << .end
==> List of UrbanTerror4 map downloads host:
--------------------------------------------
[1] (Europe, NL) UT TWente
[2] (USA) ioURT
.end
}

function download_cm {
	cd ${destpath}/q3ut4
	maplist="baeza island kingdom oildepot paradise sliema subterra superman_b4"
	for map in ${maplist}
		do
		${wget_bin} ${cm_mirror_url}/ut4_${map}.pk3
	done
}

function get_cm_download_mirrors {
	echo -n "Enter a number for download location: "
	read cmdlhost
	case "$cmdlhost" in
		"1")
			cm_mirror_url="ftp://ftp.snt.utwente.nl/pub/games/urbanterror/maps/q3ut4"
			download_cm
			;;
		"2")
			cm_mirror_url="http://www.iourt.com/resources/maps/"
			download_cm
			;;
		*)
			echo "Invalid option"
			get_cm_download_mirrors
			;;
	esac
}

be_update
community_maps

# Quit message and clean up

#rm ${basepath}/.urtinstaller > /dev/null
echo
echo "Installation is done. Start UrbanTerror 4 with ${binpath}/urbanterror4"
echo

# Nexu, Monday 5 November 2007 

