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

EAPI="2"
WANT_AUTOMAKE="latest"
WANT_AUTOCONF="latest"
inherit autotools eutils games

MY_PV="${PV:0:4}_${PV:4:2}_${PV:6:2}"
EDITION="trooper_edition"

DESCRIPTION="Cube 2: Sauerbraten is an open source game engine (Cube 2) with freeware game data (Sauerbraten)"
HOMEPAGE="http://sauerbraten.org"
SRC_URI="mirror://sourceforge/${PN}/${PN}_${MY_PV}_${EDITION}_linux.tar.bz2"
LICENSE="ZLIB freedist"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="debug doc +client +server master"

DEPEND="
	sys-libs/zlib
	client? (
		media-libs/libsdl[X,opengl]
		media-libs/sdl-mixer[vorbis]
		media-libs/sdl-image[png,jpeg]
		virtual/opengl
		virtual/glu )"

S=${WORKDIR}/${PN}

LIBEXECDIR="${GAMES_PREFIX}/libexec"
DATADIR="${GAMES_DATADIR}/${PN}"
STATEDIR="${GAMES_STATEDIR}/${PN}"

src_prepare() {
	# Remove cruft
	# Not technically nessesary, but will guard against some potential trouble
	find -name CVS -print0 | xargs -0 rm -rf
	rm -rf "${S}"/sauerbraten_unix "${S}"/bin_unix "${S}"/src/{include,lib,mingw,vcpp,xcode}

	# Use a global statedir instead of ${PWD}
	sed -i "${S}"/src/fpsgame/fps.cpp \
		-e "s:autoexec.cfg:${STATEDIR}/autoexec.cfg:" \
		-e "s:config.cfg:${STATEDIR}/config.cfg:" \
		-e "s:servers.cfg:${STATEDIR}/servers.cfg:" \
		|| die "sed failed"
	sed -i "${S}"/src/engine/server.cpp \
		-e "s:server-init.cfg:${STATEDIR}/server-init.cfg:" \
		|| die "sed failed"
	sed -i "${S}"/src/engine/master.cpp \
		-e "s:dir = \"\":dir = \"${STATEDIR}/\":" \
		|| die "sed failed"

	# Fix links so they point to the correct directory
	sed -i "${S}"/README.html \
		-e 's:docs/::' \
		|| die "sed README.html failed"

	# Recreate the enet configure script
	cd "${S}/src/enet"
	eautoreconf
	chmod +x configure
}

src_configure() {
	cd "${S}/src/enet"
	econf || die "econf enet failed"
}

src_compile() {
	cd "${S}/src"
	if use client ; then
		emake CXXFLAGS="${CXXFLAGS}$(use debug && echo " -D_DEBUG")" client || die "make failed!"
	fi
	if use server ; then
		emake CXXFLAGS="${CXXFLAGS}$(use debug && echo " -D_DEBUG")" server || die "make failed!"
	fi
	if use master ; then
		emake CXXFLAGS="${CXXFLAGS}$(use debug && echo " -D_DEBUG")" master || die "make failed!"
	fi
}

src_install() {
	# Setup the global statedir
	dodir "${STATEDIR}"

	if use client ; then
		# Add files to statedir
		for FILE in {autoexec,config,servers}.cfg ; do
			touch "${D}${STATEDIR}/${FILE}" || die "touch failed"
		done

		# Install the game data
		insinto "${DATADIR}"
		doins -r data packages

		# Install the executable
		exeinto "${LIBEXECDIR}"
		newexe src/sauer_client ${PN}

		# Install the wrappers
		games_make_wrapper "${PN}-client" "${LIBEXECDIR}/${PN}" "${DATADIR}"

		# Create menu entry
		insinto /usr/share/pixmaps
		doins "data/cube.png"
		make_desktop_entry ${PN}-client "Cube 2: Sauerbraten" cube.png "Game;ActionGame"
	fi
	if use server ; then
		# Add default files to statedir
		insinto "${STATEDIR}"
		doins "server-init.cfg"

		# Install the executable
		newgamesbin src/sauer_server ${PN}-server
	fi
	if use master ; then
		# Add default files to statedir
		for FILE in master.{cfg,log} ; do
			touch "${D}${STATEDIR}/${FILE}" || die "touch failed"
		done

		# Install the executable
		newgamesbin src/sauer_master ${PN}-master
	fi

	if use doc ; then
		dodoc src/*.txt docs/dev/*.txt
		dohtml -r README.html docs/*
	fi

	prepgamesdirs
	fperms -R g+w "${STATEDIR}" || die "fperms failed"
}