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

inherit games

DESCRIPTION="Jailbreak multiplayer mod and maps for UT2004"
HOMEPAGE="http://jailbreak.beyondunreal.com/"

# Adds 7 maps from http://jailbreak.beyondunreal.com/download/jb200x-maps/
# Also maps from http://www.planetjailbreak.com/community/lockdown.html
BEYOND_SRC="mirror://beyondunreal"
JB_FILE="jailbreak/jb2004/jailbreak${PV}-zip.zip"
MAP_DIR="nalicity/ut2k4other"
JB_MAP_FILE="jailbreak/maps-2004/jb2004-mappack-classic-zip-v2.zip"
SRC_URI="${BEYOND_SRC}/${JB_FILE}
	${BEYOND_SRC}/${JB_MAP_FILE}
	${BEYOND_SRC}/${MAP_DIR}/jb-chrome.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-crusher2.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-collateral.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-coralation.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-divergence.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-epicboy-classic.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-furia.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-galactica.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-gravity.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-nyx.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-oasis-v-2.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-relic.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-scorchedearth.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-spacelego.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-stalwartxl.zip
	${BEYOND_SRC}/${MAP_DIR}/jb-egypt.zip"

# No visible licence
LICENSE="freedist"
SLOT="0"

KEYWORDS="~x86"
IUSE=""
RESTRICT="nostrip"

DEPEND="games-fps/ut2004
	games-fps/ut2004-bonuspack-ece
	app-arch/unzip"

S=${WORKDIR}
dir=${GAMES_PREFIX_OPT}/ut2004
INI_FILE_POS=".ut2004/System/UT2004.ini"

src_install() {
	# Move map files to right directories
	mv *.ogg Music/
	mv *.ut2 Maps/
	mv ReadMe.txt JB-StalwartXL.txt
	mv *.txt Help/
	rm *.{jpg,JPG}

	# Provided by games-fps/ut2004-bonuspack-ece-1
	rm Textures/Construction_S.utx

	insinto ${dir}
	doins -r * || die "doins failed"

	prepgamesdirs
}

pkg_postinst() {
	games_pkg_postinst

	ewarn "To complete the installation, run:"
	ewarn "  emerge --config ${PN}"
	ewarn "to automatically patch ~/${INI_FILE_POS} for all users."
	echo
}

pkg_config() {
	local ini_file

	for dir in /home/*; do
		ini_file=${dir}/${INI_FILE_POS}
		if [[ -e "${ini_file}" ]] ; then
			# Check whether the ini file has already been patched
			if [[ $(grep -c "ServerPackages=Jailbreak" "${ini_file}") = "0" ]] ; then
				# Patch the ini file
				echo "Adding Jailbreak to ${ini_file}"
				sed -i "${ini_file}" \
					-e 's:\[Engine.GameEngine\]:\[Engine.GameEngine\]\nServerPackages=Jailbreak\nServerPackages=JBAddonAvenger\nServerPackages=JBAddonCelebration\nServerPackages=JBAddonLlama\nServerPackages=JBAddonProtection\nServerPackages=JBAddonTeleport\nServerPackages=JBScreen\nServerPackages=JBToolbox\n:' \
					|| echo "sed failed on ${ini_file}"
			fi
		fi
	done
}