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

EAPI=5

inherit games

MY_P=${PV/.0/}

DESCRIPTION="A fork of Mupen64 Nintendo 64 emulator"
HOMEPAGE="http://code.google.com/p/mupen64plus/"
SRC_URI="http://${PN}.googlecode.com/files/${PN}-bundle-src-${MY_P}.tar.gz
plugins? ( http://${PN}.googlecode.com/files/${PN}-extraplugins-src-${PV}.tar.gz )"

LICENSE=""
SLOT="0"
KEYWORDS="amd64"
IUSE="+asm libsamplerate lirc +osd oss +pic +plugins speex"

DEPEND="dev-lang/yasm
		virtual/pkgconfig"
RDEPEND="${DEPEND}
	libsamplerate? ( media-libs/libsamplerate )
	lirc? ( app-misc/lirc )
	speex? ( media-libs/speex )
	app-arch/xz-utils
	media-fonts/dejavu
	media-libs/freetype
	media-libs/libpng
	media-libs/libsdl
	media-libs/sdl-ttf
	sys-libs/zlib[minizip]
	virtual/opengl
	x11-libs/gtk+:2"

S=${WORKDIR}/${PN}-bundle-src-${MY_P}

src_prepare() {
	if use plugins; then
		mv ${WORKDIR}/${PN}-{rsp,video}-* ${S}/source/ || die
		sed -i '/M64P_COMPONENT/s/"$/ rsp-z64 video-arachnoid video-glide64 video-z64"/' m64p_*.sh || die
	fi
}

src_compile() {
	OPTS="${OPTS} NO_OSS=$(use oss && echo 0 || echo 1)"
	OPTS="${OPTS} NO_ASM=$(use asm && echo 0 || echo 1)"
	OPTS="${OPTS} NO_SPEEX=$(use speex && echo 0 || echo 1)"
	OPTS="${OPTS} PREFIX=${GAMES_PREFIX}"
	OPTS="${OPTS} LIBDIR=$(games_get_libdir)"
	OPTS="${OPTS} LIRC=$(use lirc && echo 1 || echo 0)"
	OPTS="${OPTS} OSD=$(use osd && echo 1 || echo 0)"
	OPTS="${OPTS} PIC=$(use pic && echo 1 || echo 0)"
	OPTS="${OPTS} PROFILE=0"
	./m64p_build.sh ${OPTS} OPTFLAGS="${CFLAGS}"
}

src_install() {
	OPTS="${OPTS} DESTDIR=${D}"
	./m64p_install.sh ${OPTS}

	if use plugins; then
		insinto ${GAMES_PREFIX}/share/${PN}
		doins source/${PN}-video-glide64/data/Glide64.ini
	fi

	prepgamesdirs
}