# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit cdrom eutils games unpacker

DESCRIPTION="Unreal Tournament 99 - futuristic deathmatch shooter"
HOMEPAGE="http://www.unrealtournament.com/
	http://utpg.org/"
SRC_URI="mirror://lokigames/../beta/ut/ut-install-436-GOTY.run
	mirror://lokigames/../patches/ut/ut-install-436.run
	http://utpg.org/patches/UTPGPatch${PV}.tar.bz2"

LICENSE="as-is"
SLOT="0"
KEYWORDS="-* amd64 ~x86"
IUSE="3dfx dedicated doc linguas_es linguas_fr linguas_it openal S3TC"
RESTRICT="strip"

RDEPEND="x86? (
		x11-libs/libXext
		x11-libs/libX11
		x11-libs/libXau
		x11-libs/libXdmcp
		3dfx? ( media-libs/glide-v3 )
		!3dfx? (
			virtual/opengl
			=media-libs/libsdl-1.2* ) )
	amd64? (
		app-emulation/emul-linux-x86-xlibs
		app-emulation/emul-linux-x86-sdl
		app-emulation/emul-linux-x86-soundlibs )
	dedicated? ( games-server/unreal-tournament-ded )"
DEPEND=">=app-arch/unshield-0.5-r1
	dev-util/xdelta
	!games-fps/unreal-tournament-goty"

S=${WORKDIR}
dir=${GAMES_PREFIX_OPT}/${PN}
Ddir=${D}/${dir}

QA_TEXTRELS="${dir:1}/System/NullDrv.so
	${dir:1}/System/NullRender.so
	${dir:1}/System/Core.so
	${dir:1}/System/Editor.so
	${dir:1}/System/NullNetDriver.so
	${dir:1}/System/Engine.so
	${dir:1}/System/SDLSoftDrv.so
	${dir:1}/System/Render.so"

set_dirs() {
	# The top-level directory names can change per CD.
	if [[ "${UT_SOURCE}" == "anthology" ]] || [[ -e "${CDROM_ROOT}/System" ]] ; then
		HELP_DIR="Help"
		MUSIC_DIR="Music"
		SOUNDS_DIR="Sounds"
		SYSTEM_DIR="System"
		TEXTURES_DIR="Textures"
	else
		HELP_DIR="HELP"
		MUSIC_DIR="MUSIC"
		SOUNDS_DIR="SOUNDS"
		SYSTEM_DIR="SYSTEM"
		TEXTURES_DIR="TEXTURES"
	fi

	if [[ "${UT_SOURCE}" == "anthology" ]] || [[ -e "${CDROM_ROOT}/Maps" ]] ; then
		MAPS_DIR="Maps"
	elif [[ -e "${CDROM_ROOT}/MAPS" ]] ; then
		MAPS_DIR="MAPS"
	else
		MAPS_DIR="maps"
	fi
}

unpack_patches() {
	local x

	# We patch to 451. Engine.u changed with almost every version so we can decide what
	# patching is needed based on its MD5. The Windows -> Linux transition is done at
	# version 436 but it's okay to accept all the later Windows and Linux versions
	# because all the the files modified by those patches are either unused by Linux
	# or replaced by the 451 Linux patch.
	case `md5sum System/Engine.u | head -c32` in
		"949edbb406a958ae40d8102c3c2b8162")	UT_VERSION="400"
							einfo "UT version 400 (original unpatched) detected" ;;
		"659efcacbab279e56a2865268e0ec004")	UT_VERSION="432"
							einfo "UT version 432 (GOTY or patched) detected" ;;
		"e6fbb28182bc82a9414cc96a744eda1b")	UT_VERSION="436"
							einfo "UT version 436/440 (GOTY or patched) detected" ;;
		"30991ebeb67c50d2a1ff996947fac7aa")	UT_VERSION="451"
							einfo "UT version 451 (UTPG patched) detected" ;;
		"9aecf807f585ac7543ca67e5c831a714")	UT_VERSION="451b"
							einfo "UT version 451b (UTPG patched) detected" ;;
		*)					UT_VERSION="Unknown"
							ewarn "Unsupported UT version detected"
							ewarn "Continuing anyway but the game may not work correctly. It is recommended"
							ewarn "that you install from the original CD or DVD instead." ;;
	esac

	mkdir patch-ut
	cd patch-ut

	if [[ "${UT_VERSION}" == "400" ]] || [[ "${UT_VERSION}" == "Unknown" ]] ; then
		unpack_makeself ut-install-436.run
	else
		unpack_makeself ut-install-436-GOTY.run
	fi

	cd ..

	# Copy startup script and hard-code game path.
	sed "s:\`FindPath \$0\`:${dir}:" patch-ut/bin/x86/ut > ut || die "sed ut"

	# Apply any appropriate patches.
	if [[ "${UT_VERSION}" == "400" ]] || [[ "${UT_VERSION}" == "432" ]] ; then
		for x in patch-ut/setup.data/data/System/*.0 ; do
			xdelta patch ${x} System/`basename ${x} .0` patched || die "xdelta patch ${x}"
			mv -f patched System/`basename ${x} .0` || die "mv patched"
		done
	fi

	# Unpack 436 and 451 files.
	unpack ./patch-ut/{data,Credits,OpenGL.ini}.tar.gz
	unpack UTPGPatch${PV}.tar.bz2
	rm -f {,System/}{checkfiles.sh,patch.md5}

	# Tweak the default settings for today's original of PCs and good preferences.
	sed -i -r \
		-e "s/^CacheSizeMegs=[0-9]+/CacheSizeMegs=64/" \
		-e "s/^DoPrecache=.*$/DoPrecache=1/" \
		-e "s/^UseTrilinear=.*$/UseTrilinear=True/" \
		-e "s/^ScreenFlashes=.*$/ScreenFlashes=False/" \
		-e "s/^OutputRate=[0-9]+Hz/OutputRate=44100Hz/" \
		-e "s/^Channels=[0-9]+/Channels=24/" \
		System/UnrealTournament.ini || die "sed UnrealTournament.ini"

	if use 3dfx ; then
		# Maybe someone still uses them!
		RENDERER="GlideDrv.GlideRenderDevice"
	else
		# Recommended at http://icculus.org/lgfaq/#renderer
		RENDERER="OpenGLDrv.OpenGLRenderDevice"
	fi

	if use openal ; then
		# Provides more effects.
		AUDIO="ALAudio.ALAudioSubsystem"
	else
		# Less problematic.
		AUDIO="Audio.GenericAudioSubsystem"
	fi

	if use S3TC ; then
		S3TC=1
	else
		S3TC=0
	fi

	# Set the appropriate drivers.
	sed -i \
		-e "s/^GameRenderDevice=.*$/GameRenderDevice=${RENDERER}/" \
		-e "s/^WindowedRenderDevice=.*$/WindowedRenderDevice=${RENDERER}/" \
		-e "s/^RenderDevice=.*$/RenderDevice=${RENDERER}/" \
		-e "s/^AudioDevice=.*$/AudioDevice=${AUDIO}/" \
		-e "s/^UseS3TC=.*$/UseS3TC=${S3TC}/" \
		System/UnrealTournament.ini || die "sed UnrealTournament.ini (drivers)"

	# Fix server vulnerability - part of bug #44351.
	sed -i \
		-e "/^LoadClassMismatch/s:%s.%s:%s:" \
		System/Core.int || die "sed Core.int"

	# Remove duplicate-cased files.
	mv -f System/editor.int System/Editor.int || die
	mv -f System/glidedrv.int System/GlideDrv.int || die
	mv -f System/opengldrv.int System/OpenGlDrv.int || die

	# These files are from the original Unreal. Not sure why the 451 patch includes them.
	rm -f System/Female{1,2}Skins.int System/Male{1,2,3}Skins.int System/SkTrooperSkins.int

	# Remove other unneeded files.
	rm -f System/{libSDL-1.1.so.0,UnrealTournament.ini.PATCH,{Manifest,OpenGLDrv,Startup,d3ddrv,galaxy,softdrv,windrv}.int}
}

pkg_setup() {
	games_pkg_setup
	local extra

	if use S3TC ; then
		# This allows the high resolution textures to be copied from a previous installation. However,
		# the file must be checked to ensure that it really is a high resolution texture file because
		# there are no high resolution textures files without a low resolution equivalent.
		if [[ -n "${CD_ROOT}" ]] && [[ -e "${CD_ROOT}/Textures/UT.utx" ]] && [[ `md5sum "${CD_ROOT}/Textures/UT.utx" | head -c32` == "0717a53dfb125585659207c8021359a2" ]] ; then
			extra=":Textures/UT.utx"
		fi

		CDROM_NAME_SET=( "UT Game CD or Unreal Anthology DVD" "UT High Resolution Textures CD" )
		cdrom_get_cds System/UTMenu.u:SYSTEM/UTMenu.u:AutoRunData/Unreal.ico Help/chaosut:UnrealEd/UnrealEdInstaller.exe${extra}
	else
		CDROM_NAME="UT Game CD or Unreal Anthology DVD"
		cdrom_get_cds System/UTMenu.u:SYSTEM/UTMenu.u:AutoRunData/Unreal.ico
	fi

	case ${CDROM_SET} in
		0|1)	UT_SOURCE="normal"
			einfo 'Found UT Game CD or previous installation' ;;
		2)	UT_SOURCE="anthology"
			einfo 'Found Unreal Anthology DVD' ;;
	esac

	set_dirs
}

src_unpack() {
	local lang lower x

	mkdir Help Maps Music Sounds System Textures

	# Explicitly name files to copy in case we are using some existing installation with additional files.
	HELP={Logo.bmp,ReadMe.htm,Unreal{.ico,Ed.ico,TournamentLogo.bmp,TournamentSetupLogo.bmp}}
	MAPS={AS-{Frigate,Guardia,HiSpeed,Mazon,OceanFloor,Overlord,Rook,Tutorial},CTF-{Command,Coret,Dreary,EternalCave,Face,Gauntlet,LavaGiant,Niven,November,Tutorial},CityIntro,DM-{Barricade,Codex,Conveyor,Curse][,Deck16][,Fetid,Fractal,Gothic,Grinder,HyperBlast,KGalleon,Liandri,Morbias][,Morpheus,Oblivion,Peak,Phobos,Pressure,Pyramid,Stalwart,StalwartXL,Tempest,Turbine,Tutorial,Zeto},DOM-{Cinder,Condemned,Cryptic,Gearbolt,Ghardhen,Lament,Leadworks,MetalDream,Olden,Sesmar,Tutorial},EOL_{Assault,CTF,Challenge,Deathmatch,Domination,Statues},Entry,UT-Logo-Map,UTCredits}.unr
	MUSIC={Botmca9,Botpck10,Cannon,Colossus,Course,Credits,Ending,Enigma,Foregone,Godown,Lock,Mech8,Mission,Nether,Organic,Phantom,Razor-ub,Run,SaveMe,Savemeg,Seeker,Seeker2,Skyward,Strider,Suprfist,UnWorld2,Uttitle,Wheels,firebr,utmenu23}.umx
	SOUNDS={Activates,Addon1,Amb{Ancient,City,Modern,Outside},DDay,DMatch,DoorsAnc,DoorsMod,Extro,FemaleSounds,LadderSounds,MaleSounds,Pan1,VRikers,noxxsnd,rain}.uax
	SYSTEM={{Commando,FCommando,SGirl,Soldier}Skins.int,{Core,Editor,Engine,IpDrv,IpServer,UBrowser,UMenu,UTMenu,UWeb,Unreal{I,Share}}.{int,u},BotPack.u,Botpack.int,Fire.u,GlideDrv.int,OpenGlDrv.in{i,t},UTBrowser.u,UTServerAdmin.int,UTServerAdmin.u,UWindow.u,UnrealTournament.in{i,t},User.ini,Window.int}

	# Some files are localized.
	LOCALIZED_SOUNDS={Announcer,BossVoice,Female{1,2}Voice,Male{1,2}Voice,TutVoice{AS,CTF,DM,DOM},openingwave}.uax
	LOCALIZED_SYSTEM={{Boss,Commando,FCommando,SGirl,Soldier}Skins,{Glide,Ip,OpenGl}Drv,Editor,Engine,IpServer,UBrowser,UMenu,Unreal{,I,Share,Tournament},Window}
	LOCALIZED_SYSTEM_BADCASE={Botpack,UTBrowser,UTMenu,UWindow,AS-{Frigate,Guardia,HiSpeed,Mazon,OceanFloor,Overlord,Rook,Tutorial},CTF-{Command,Coret,Dreary,EternalCave,Face,Gauntlet,LavaGiant,Niven,November,Tutorial},DM-{Barricade,Codex,Conveyor,Curse][,Deck16][,Fetid,Fractal,Gothic,Grinder,HyperBlast,KGalleon,Liandri,Morbias][,Morpheus,Oblivion,Peak,Phobos,Pressure,Pyramid,Stalwart,Tempest,Turbine,Tutorial,Zeto},DOM-{Cinder,Condemned,Cryptic,Gearbolt,Ghardhen,Lament,Leadworks,MetalDream,Olden,Sesmar,Tutorial},EOL_{Assault,Challenge,CTF,Deathmatch,Domination,Statues}}

	# Some low resolution texture files have no high resolution equivalent.
	TEXTURES_LOW={{Boss,Dacoma,FCommando,Female{1,2},Goth,Male{1,2,3},SGirl,SkTrooper}Skins,AlfaFX,Belt_fx,BluffFX,ChizraEFX,Creative,Crypt,DMeffects,DacomaFem,Detail,FireEng,GothFem,GreatFire{,2},ISVFX,LadderFonts,Ladr{Arrow,Static},LavaFX,Liquids,Logo,MenuGr,NivenFX,Palettes,PhraelFx,Render,RotatingU,Scripted,Soldierskins,SpaceFX,Terranius,UT_ArtFX,UWindowFonts,XFX,commandoskins,credits}.utx
	TEXTURES_BOTH={Ancient,ArenaTex,CTF,Coret_FX,Crypt{2,_FX},DDayFX,DecayedS,Egypt{,Pan},Faces,FlareFX,FractalFX,Gen{Earth,FX,Fluid,In,Terra,Warp},HubEffects,Indus{1,2,3,4,5,6,7},JWSky,Lian-X,Metalmys,Mine,Nali{Cast,FX},Old_FX,PlayrShp,Queen,RainFX,Shane{Church,Day,Sky},Skaarj,Sky{Box,City},Slums,Starship,TCrystal,TrenchesFX,UT,UTbase1,UTcrypt,UTtech{1,2,3},XbpFX,Xtortion,castle1,city,eol,of1}.utx

	if [[ "${UT_SOURCE}" == "anthology" ]] ; then
		if use S3TC ; then
			ewarn "The Unreal Anthology DVD does not include the high resolution textures"
			ewarn "but you may install them if you have them available from another source."
			ewarn "Otherwise, please disable the S3TC USE flag."
		fi

		# Symlinks for unshield.
		ln -sfn "${CDROM_ROOT}"/Disk1/data1.hdr "${T}"
		ln -sfn "${CDROM_ROOT}"/Disk*/data*.cab "${T}"

		CAB_GROUPS="Help Maps Music Sounds_All Sounds_English System_All System_English Textures"

		# The manual is pretty heavy at 17MB!
		use doc && CAB_GROUPS="${CAB_GROUPS} Manual_English"

		# Add requested language components. The manuals are all in English!?
		use linguas_es && CAB_GROUPS="${CAB_GROUPS} Sounds_Spanish System_Spanish"
		use linguas_fr && CAB_GROUPS="${CAB_GROUPS} Sounds_French System_French"
		use linguas_it && CAB_GROUPS="${CAB_GROUPS} Sounds_Italian System_Italian"

		# Unpack!
		for x in ${CAB_GROUPS} ; do
			unshield -g 3_UnrealTournament_${x} x "${T}"/data1.cab || die "unshield"
		done

		# Retrieve the manual.
		if use doc ; then
			mv -f 3_UnrealTournament_Manual_English/Manual.pdf manual_en.pdf || die "mv manual_en.pdf"
		fi

		eval mv -f 3_UnrealTournament_Help/${HELP} Help || die "mv Help"
		eval mv -f 3_UnrealTournament_Music/${MUSIC} Music || die "mv Music"
		eval mv -f 3_UnrealTournament_Maps/${MAPS} Maps || die "mv Maps"

		# Move generic sounds.
		eval mv -f 3_UnrealTournament_Sounds_All/${SOUNDS} Sounds || die "mv Sounds (generic)"

		# Move English sounds.
		eval mv -f 3_UnrealTournament_Sounds_English/${LOCALIZED_SOUNDS} Sounds || die "mv Sounds (English)"

		# Move Spanish sounds.
		if use linguas_es ; then
			mkdir -p Sounds/est || die "mkdir Sounds/est"
			eval mv -f 3_UnrealTournament_Sounds_Spanish/${LOCALIZED_SOUNDS} Sounds/est || die "mv Sounds (est)"
		fi

		# Move French sounds.
		if use linguas_fr ; then
			mkdir -p Sounds/frt || die "mkdir Sounds/frt"
			eval mv -f 3_UnrealTournament_Sounds_French/${LOCALIZED_SOUNDS} Sounds/frt || die "mv Sounds (frt)"
		fi

		# Move Italian sounds.
		if use linguas_it ; then
			mkdir -p Sounds/itt || die "mkdir Sounds/itt"
			eval mv -f 3_UnrealTournament_Sounds_Italian/${LOCALIZED_SOUNDS} Sounds/itt || die "mv Sounds (itt)"
		fi

		# Move system files.
		eval mv -f 3_UnrealTournament_System_{All,English}/${SYSTEM} System 2> /dev/null

		for lang in es fr it ; do
			if use linguas_${lang} ; then
				# Move localized system files.
				eval mv -f 3_UnrealTournament_System_*/${LOCALIZED_SYSTEM}.${lang}t System || die "mv System (${lang}t)"

				# Move and fix case for localized system files.
				for x in `eval echo ${LOCALIZED_SYSTEM_BADCASE}.${lang}t` ; do
					lower=`echo ${x} | tr A-Z a-z` || die "tr ${x}"

					if [[ -f `echo 3_UnrealTournament_System_*/${lower}` ]] ; then
						mv -f 3_UnrealTournament_System_*/${lower} System/${x} || die "mv System/${x}"
					elif [[ -f `echo 3_UnrealTournament_System_*/${x}` ]] ; then
						mv -f 3_UnrealTournament_System_*/${x} System/${x} || die "mv System/${x}"
					else
						die "${x} is missing"
					fi
				done
			fi
		done

		# Move low resolution textures.
		eval mv -f 3_UnrealTournament_Textures/${TEXTURES_LOW} Textures || die "mv Textures (low)"

		if ! use S3TC ; then
			eval mv -f 3_UnrealTournament_Textures/${TEXTURES_BOTH} Textures || die "mv Textures (both)"
		fi

		rm -rf 3_UnrealTournament_*
		unpack_patches
	else
		eval cp -f "\"${CDROM_ROOT}/${HELP_DIR}\""/${HELP} Help || die "cp Help"
		eval cp -f "\"${CDROM_ROOT}/${MUSIC_DIR}\""/${MUSIC} Music || die "cp Music"
		eval cp -f "\"${CDROM_ROOT}/${SYSTEM_DIR}\""/${SYSTEM} System || die "cp System"

		if ! use S3TC && [[ `md5sum "${CDROM_ROOT}/Textures/UT.utx" | head -c32` == "0717a53dfb125585659207c8021359a2" ]] ; then
			ewarn "You have requested to install the low resolution textures but the texture"
			ewarn "files found in CDROM_ROOT appear to be the high resolution ones. These"
			ewarn "will be installed instead."
		fi

		# Copy low resolution textures.
		eval cp -f "\"${CDROM_ROOT}/${TEXTURES_DIR}\""/${TEXTURES_LOW} Textures || die "cp Textures (low)"

		if ! use S3TC ; then
			eval cp -f "\"${CDROM_ROOT}/${TEXTURES_DIR}\""/${TEXTURES_BOTH} Textures || die "cp Textures (both)"
		fi

		# Copy generic sounds.
		eval cp -f "\"${CDROM_ROOT}/${SOUNDS_DIR}\""/${SOUNDS} Sounds || die "cp Sounds (generic)"

		# Copy English sounds.
		eval cp -f "\"${CDROM_ROOT}/${SOUNDS_DIR}\""/${LOCALIZED_SOUNDS} Sounds || die "cp Sounds (English)"

		# Copy English manual, if it exists.
		if use doc ; then
			if [[ -f "${CD_ROOT}/Extras/Manual/UT-man-gb.pdf" ]] ; then
				cp -f "${CD_ROOT}/Extras/Manual/UT-man-gb.pdf" manual_en.pdf || die "cp manual_en.pdf"
			elif [[ -f "${ROOT}/usr/share/doc/${PF}/manual_en.pdf" ]] ; then
				cp -f "${ROOT}/usr/share/doc/${PF}/manual_en.pdf" . || die "cp manual_en.pdf"
			fi
		fi

		for lang in es fr it ; do
			if use linguas_${lang} ; then
				# Copy localized system files.
				eval cp -f "\"${CDROM_ROOT}/${SYSTEM_DIR}\""/${LOCALIZED_SYSTEM}.${lang}t System || die "cp System (${lang}t)"

				# Copy and fix case remaining localized system files.
				for x in `eval echo ${LOCALIZED_SYSTEM_BADCASE}.${lang}t` ; do
					lower=`echo ${x} | tr A-Z a-z` || die "tr ${x}"

					if [[ -f "${CDROM_ROOT}/${SYSTEM_DIR}/${lower}" ]] ; then
						cp -f "${CDROM_ROOT}/${SYSTEM_DIR}/${lower}" System/${x} || die "cp System/${x}"
					elif [[ -f "${CDROM_ROOT}/${SYSTEM_DIR}/${x}" ]] ; then
						cp -f "${CDROM_ROOT}/${SYSTEM_DIR}/${x}" System/${x} || die "cp System/${x}"
					else
						die "${x} is missing"
					fi
				done

				# Copy localized sounds.
				mkdir -p Sounds/${lang}t || die "mkdir Sounds/${lang}t"
				for x in `eval echo ${LOCALIZED_SOUNDS}` ; do
					if [[ -f "${CDROM_ROOT}/${SOUNDS_DIR}/${x}_${lang}t" ]] ; then
						cp -f "${CDROM_ROOT}/${SOUNDS_DIR}/${x}_${lang}t" Sounds/${lang}t/${x} || die "cp Sounds/${lang}t/${x}"
					elif [[ -f "${CDROM_ROOT}/${SOUNDS_DIR}/${lang}t/${x}" ]] ; then
						cp -f "${CDROM_ROOT}/${SOUNDS_DIR}/${lang}t/${x}" Sounds/${lang}t/${x} || die "cp Sounds/${lang}t/${x}"
					else
						die "${x} (${lang}t) is missing"
					fi
				done

				# Copy localized manual, if it exists.
				if use doc ; then
					if [[ -f "${CD_ROOT}/Extras/Manual/UT-man-${lang:0:1}.pdf" ]] ; then
						cp -f "${CD_ROOT}/Extras/Manual/UT-man-${lang:0:1}.pdf" manual_${lang}.pdf || die "cp manual_${lang}.pdf"
					elif [[ -f "${ROOT}/usr/share/doc/${PF}/manual_${lang}.pdf" ]] ; then
						cp -f "${ROOT}/usr/share/doc/${PF}/manual_${lang}.pdf" . || die "cp manual_${lang}.pdf"
					fi
				fi
			fi
		done

		# Need to unpack the patches early so we can get ucc-bin for decompressing maps.
		unpack_patches

		cd System

		# Copy or decompress maps as necessary.
		for x in `eval echo ${MAPS}` ; do
			if [[ -e "${CDROM_ROOT}/${MAPS_DIR}/${x}" ]] ; then
				cp -f "${CDROM_ROOT}/${MAPS_DIR}/${x}" ../Maps || die "cp Maps/${x}"
			elif [[ -e "${CDROM_ROOT}/${MAPS_DIR}/${x}.uz" ]] ; then
				./ucc-bin decompress "\"${CDROM_ROOT}/${MAPS_DIR}/${x}.uz\"" -nohomedir || die "decompress Maps/${x}.uz"
				mv -f ${x} ../Maps || die "mv map ${x}"
			else
				die "Maps/${x} missing"
			fi
		done

		cd ..
	fi

	if use S3TC ; then
		unset CDROM_SET
		cdrom_load_next_cd
		set_dirs

		# Because of the check in pkg_setup, this will probably never happen but just in case...
		if [[ `md5sum "${CDROM_ROOT}/Textures/UT.utx" | head -c32` == "076aaca7fa6ec3b7f31781d3b8c150d8" ]] ; then
			ewarn "You have requested to install the high resolution textures but the texture"
			ewarn "files found in CDROM_ROOT appear to be the low resolution ones. These"
			ewarn "will be installed instead."
		fi

		# Copy high resolution textures.
		eval cp -f "\"${CDROM_ROOT}/${TEXTURES_DIR}\""/${TEXTURES_BOTH} Textures || die "cp Textures (CD 2)"
	fi

	# Don't include ucc-bin, due to bug #44351.
	rm -f System/ucc-bin
}

src_install() {
	local x

	# Guard against executable flag potentially set.
	for x in `find -type f` ; do
		chmod 640 "${x}" || die
	done

	# System files.
	chmod 750 System/ut-bin System/*.so System/*.so.* || die

	# Move instead of copy to reduce disk stress. (-:
	# Permissions are corrected with prepgamesdirs below.
	dodir "${dir}"
	mv Help Maps Music Sounds System Textures Web "${Ddir}" || die "mv directories"

	# Script.
	exeinto "${dir}"
	doexe ut || die "doexe ut"

	# Icon.
	insinto "${dir}"
	doins patch-ut/icon.xpm || die "doins icon"

	# Desktop.
	games_make_wrapper ut ./ut "${dir}"
	newicon patch-ut/icon.xpm ut.xpm || die "newicon"
	make_desktop_entry ut "Unreal Tournament 99" ut.xpm

	# Manual.
	insinto /usr/share/doc/${PF}
	if [[ "`echo manual_*.pdf`" != "manual_*.pdf" ]] ; then
		doins manual_*.pdf || die "doins manual_*.pdf"
	fi

	prepgamesdirs
}

pkg_postinst() {
	games_pkg_postinst

	if ! use 3dfx ; then
		elog "The OpenGLDrv.OpenGLRenderDevice driver has been selected but the"
		elog "SDLGLDrv.SDLGLRenderDevice and SDLSoftDrv.SDLSoftwareRenderDevice"
		elog "drivers are also available if you need them. To use them, edit"
		elog "~/.loki/ut/System/UnrealTournament.ini after running the game once."
		echo
		elog "If you experience texture corruption, particularly if you have an older"
		elog "graphics card, try editing ~/.loki/ut/System/UnrealTournament.ini and"
		elog "change UseMultiTexture from 1 to 0 for your selected driver."
		echo
	fi

	if use linguas_es || use linguas_fr || use linguas_it ; then
		elog "You have installed support for one or more additional languages. The game"
		elog "defaults to English but you can change the language by running the game"
		elog "once and then editing ~/.loki/ut/System/UnrealTournament.ini. Change the"
		elog "Language entry under [Engine.Engine] to 'est', 'frt' or 'itt' and add"
		elog "a Path entry directly under [Core.System] with '..\\\\Sounds\\\\est\\\\*.uax',"
		elog "'..\\\\Sounds\\\\frt\\\\*.uax' or '..\\\\Sounds\\\\itt\\\\*.uax' for Spanish, French or"
		elog "Italian respectively."
		echo
	fi

	if ! has_version "games-fps/unreal-tournament-bonuspacks" ; then
		elog "emerge 'games-fps/unreal-tournament-bonuspacks' to add many popular maps."
		elog "This is highly recommended if you wish to play online. Note that you need"
		elog "this package for Bonus Packs 1 to 3 even if you just installed the game"
		elog "using the GOTY edition."
		echo
	fi

	if ! use dedicated && ! has_version "games-server/unreal-tournament-ded" ; then
		elog "The 'ucc' dedicated server executable has been moved to"
		elog "games-server/unreal-tournament-ded for security reasons."
		elog "See bug #44351 for details."
		echo
	fi
}