# Distributed under the terms of the GNU General Public License v2

EAPI=3
inherit eutils games

DESCRIPTION="Steel Storm: Burning Retribution"
HOMEPAGE="http://one.steel-storm.com/"
RESTRICT="fetch"
SRC_URI="${PN}-${PV}-release.tar.gz"
LICENSE="GPL-2"

SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="alsa +client dedicated +sdl"

CDEPEND="sys-libs/zlib
	>=media-libs/libsdl-1.2
	>=media-libs/freetype-2.3.11
	>=media-libs/libogg-1.1.4
	>=media-libs/libvorbis-1.2.3
	>=net-misc/curl-7.19.7
	>=media-libs/libmodplug-0.8.7
	media-libs/libpng
	virtual/jpeg
	virtual/opengl"
RDEPEND=""
DEPEND="${CDEPEND}
	app-arch/unzip"

MY_PN=${PN%-br}
S="${WORKDIR}/${MY_PN}/engine_source/darkplaces"

pkg_nofetch() {
	einfo "Place ${SRC_URI} into ${DISTDIR}."
	einfo "See http://www.humblebundle.com/ for more info."
}

src_setup() {
	if ! use client && ! use dedicated; then
		eerror "client, server or both must be enabled"
		die "client and dedicated server both has been disabled"
	fi

	if ! use client && use sdl; then
		eerror "sdl client build needs 'sdl' flag"
		die "sdl support selected but client is not enabled"
	fi
}

src_unpack() {
	unpack ${PN}-${PV}-release.tar.gz
	cd "${WORKDIR}/${MY_PN}" || die
	unzip engine_source.zip || die
}

src_prepare() {
	sed -i \
		-e "/^CC=/d" \
		-e "s:-O2:${CFLAGS}:" \
		-e "s:-O3:${CFLAGS}:" \
		-e "/-lm/s:$: ${LDFLAGS}:" \
		-e '/^STRIP/s/strip/true/' \
		-e "s/darkplaces-dedicated/${PN}-dedicated/" \
		-e "s/darkplaces-sdl/${PN}/" \
		-e "s/darkplaces-glx/${PN}-glx/" \
		-e "s:/usr/X11R6/:/usr/:g" \
			makefile.inc || die "sed failed"

	sed -i \
		-e '1i DP_LINK_TO_LIBJPEG=1' \
		-e "s:ifdef DP_FS_.*:DP_FS_BASEDIR=${GAMES_DATADIR}/${PN}\n&:" \
		-e "s:/usr/X11R6/:/usr/:g" \
			makefile || die "sed failed"

	if ! use alsa ; then
		sed -i \
			-e "/DEFAULT_SNDAPI/s:ALSA:OSS:" \
			makefile || die "sed failed"
	fi

	# proper DGA header
	sed -i \
		-e 's/xf86dga.h/Xxf86dga.h/' \
			vid_glx.c || die "s/xf86dga.h/Xxf86dga.h/ failed"
}

src_compile() {
	if use client ; then
		emake cl-release || die "emake cl-release failed"
	fi
	if use sdl ; then
		emake sdl-release || die "emake sdl-release failed"
	fi
	if use dedicated ; then
		emake sv-release || die "emake sv-release failed"
	fi
}

src_install() {
	insinto ${GAMES_DATADIR}/${PN}
	doins -r ${S}/../../gamedata || die "doins gamedata failed"

	if use client; then
		dogamesbin ${PN}-glx || die "dogamesbin ${PN}-glx failed"
		use sdl && \
			$(dogamesbin ${PN} || die "dogamesbin ${PN} failed")

		newicon ../../icons/ss_ep2_icon_16.png "${PN}-16.png"
		newicon ../../icons/ss_ep2_icon_48.png "${PN}-32.png"
		newicon ../../icons/ss_ep2_icon_128.png "${PN}-128.png"
		insinto "${GAMES_DATADIR}/${PN}/gamedata"
		doins ../../"key_0.d0pk" || die

		use sdl && \
			make_desktop_entry "${PN}" "Steel Storm: Burning Retribution [SDL]" "${PN}" || \
			make_desktop_entry "${PN}-glx" "Steel Storm: Burning Retribution [GLX]" "${PN}"
	fi

	use dedicated && \
		$(dogamesbin ${PN}-dedicated || die "dogamesbin ${PN}-dedicated failed")
}