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

EAPI=5

inherit eutils games

MY_ARCH=${ARCH/x86/i386}
MY_PN=${PN/-/_}
MY_UPN="CortexCommand"
MY_PV=${PV/_}
#MY_REV="-1"

DESCRIPTION="a 2-dimensional side-scrolling action game developed by Data Realms"
HOMEPAGE="http://www.datarealms.com/games.php"
SRC_URI="${MY_PN}-${MY_PV}-linux-${MY_ARCH}.bin"

LICENSE="DetaRealms"
SLOT="0"
KEYWORDS="-* amd64 ~x86"
IUSE="+system-libs"
RESTRICT="fetch strip"

BUNDLED_LIBS="dev-libs/openssl
	media-libs/libogg
	media-libs/libvorbis
	media-libs/openal
	net-misc/curl"
RDEPEND=">=sys-devel/gcc-4.6
	sys-apps/util-linux
	sys-libs/zlib
	x11-libs/libSM
	x11-libs/libXcursor
	x11-libs/libXext
	x11-libs/libXxf86vm
	x11-libs/libxcb
	system-libs? ( ${BUNDLED_LIBS} )"
DEPEND="app-arch/unzip"

S="${WORKDIR}/data/${MY_PN}_linux"

GAMEDIR=${GAMES_PREFIX_OPT}/${PN}

QA_PREBUILT="opt/cortex-command/CortexCommand.bin
	opt/cortex-command/libs/*"


pkg_nofetch() {
	echo
	elog "Please purchase and download '${SRC_URI}'"
	elog "then copy to: '${DISTDIR}'"
	echo
}

src_unpack() {
	local a="${DISTDIR}/${A}"
	unzip -q "${a}" 2>/dev/null
	[ $? -gt 1 ] && die "unpack failed"
}

src_prepare() {
	einfo $PWD
}

src_install() {
	insinto "${GAMEDIR}"
	exeinto "${GAMEDIR}"

	# install game files
	doins -r *.rt?
	doins Credits.txt
	doexe ${MY_UPN}.bin

	# install bundled libraries
	exeinto "${GAMEDIR}/libs"
	if use system-libs; then
		# the following are not available in portage
		doexe libs/{liballeg.so.4.4,libcAudio.so,libluabindd.so.0.9.0}
		# game crashes without the following, even if system library installed
		doexe libs/libopenal.so.1
	else
		# install everything
		doexe libs/*
	fi

	# install documentation
	dodoc readme.txt

	# install shortcuts
	newicon ${MY_UPN}.png ${PN}.png
	games_make_wrapper ${PN} ./${MY_UPN}.bin "${GAMEDIR}" "${GAMEDIR}"
	make_desktop_entry ${PN} "Cortex Command"
	prepgamesdirs
}

pkg_postinst() {
	games_pkg_postinst

	# This is ugly, terrible, and should never be done, but it's the only way
	# to allow saving settings and games for now
	chmod -R g+w "${GAMEDIR}"
	echo
	einfo "To play the game, run:"
	einfo "${PN}"
	echo
}