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

inherit eutils games

MY_P="TheHuntedc2"
DESCRIPTION="Single-player fps game based off of the Darkplaces engine"
HOMEPAGE="http://thehunted.ru1337.com/"
SRC_URI="http://ru1337.com/files/${MY_P}.exe
	http://icculus.org/twilight/darkplaces/files/darkplacesengine20050818.zip"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86"
IUSE="sdl"

RDEPEND="virtual/opengl
	sdl? ( media-libs/libsdl )
	media-libs/libogg
	media-libs/libvorbis
	media-libs/jpeg
	media-libs/alsa-lib
	sys-libs/zlib"
DEPEND="${RDEPEND}
	dev-util/pkgconfig
	app-arch/unzip"

S=${WORKDIR}/${MY_P}

src_unpack() {
	unzip -qo "${DISTDIR}/${MY_P}.exe"
	if [ $? -ne 0 -a $? -ne 1 ]
	then
		die "unzip failed"
	fi
	cd "${S}"
	unpack darkplacesengine20050818.zip
	unzip -qo darkplacesenginesource20050818.zip || die "unzip failed"

	cd darkplaces
	sed -i \
		-e "/strcpy(fs_basedir/s:\.:${GAMES_DATADIR}/${PN}:" \
		-e "/strcpy(fs_gamedir/s:\"\":\"thdata\":" \
		fs.c \
		|| die "sed failed"

	sed -i \
		-e '/^CC/d' \
		-e "s:-lasound:$(pkg-config --libs alsa):" \
		-e 's:darkplaces-sdl:thehunted-sdl:' \
		-e 's:darkplaces-glx:thehunted-glx:' \
		makefile.inc \
		|| die "sed failed"
}

src_compile() {
	cd darkplaces

	emake cl-release \
		CFLAGS_RELEASE="" OPTIM_RELEASE="" \
		CFLAGS_COMMON="${CFLAGS}" \
		|| die "emake cl-release failed"

	if use sdl;	then
		emake sdl-release \
			CFLAGS_RELEASE="" OPTIM_RELEASE="" \
			CFLAGS_COMMON="${CFLAGS}" \
			|| die "emake sdl-release failed"
	fi
}

src_install() {
	dogamesbin darkplaces/thehunted-glx || die "dogamesbin failed"
	if use sdl; then
		dogamesbin darkplaces/thehunted-sdl || die "dogamesbin failed"
	fi

	insinto "${GAMES_DATADIR}"/${PN}
	doins -r thdata/ || die "doins failed"
	dodoc *.txt || die "dodoc failed"
	newicon darkplaces/darkplaces72x72.png ${PN}.png

	if use sdl; then
		make_desktop_entry thehunted-sdl "The Hunted Chronicles" ${PN}.png
	else
		make_desktop_entry thehunted-glx "The Hunted Chronicles" ${PN}.png
	fi
	prepgamesdirs
}