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

EAPI="3"

inherit eutils flag-o-matic games subversion

DESCRIPTION="Gamecube, Wii and Triforce emulator"
HOMEPAGE="http://www.dolphin-emu.com/"
SRC_URI=""

ESVN_REPO_URI="http://dolphin-emu.googlecode.com/svn/trunk/"
ESVN_PROJECT="dolphin-emu-read-only"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS=""
IUSE="bluetooth doc openal portaudio pulseaudio +wxwidgets"
RESTRICT=""

RDEPEND="dev-libs/lzo
	>=media-libs/glew-1.5
	media-libs/jpeg
	media-libs/libao
	>=media-libs/libsdl-1.2[joystick]
	sys-libs/zlib
	x11-libs/cairo
	x11-libs/libXxf86vm
	x11-libs/libXext
	>=x11-libs/wxGTK-2.8
	virtual/opengl
	bluetooth? ( net-wireless/bluez )
	openal? ( media-libs/openal )
	portaudio? ( media-libs/portaudio )
	pulseaudio? ( media-sound/pulseaudio )"

DEPEND="${RDEPEND}
	dev-util/scons
	dev-util/pkgconfig
	media-gfx/nvidia-cg-toolkit"

src_prepare() {
	if [[ "$(gcc-fullversion)" == "4.4.3" ]]; then
		einfo "gcc-4.4.3x fix"
		epatch "${FILESDIR}/${P}-gcc-4.4.3x-fix.patch"
	fi
	einfo "system path fix"
	epatch "${FILESDIR}/${P}-system-path-fix.patch"
}

src_compile() {
	cd "${S}"
	local sconsopts=$(echo "${MAKEOPTS}" | sed -ne "/-j/ { s/.*\(-j[[:space:]]*[0-9]\+\).*/\1/; p }")
	scons ${sconsopts} \
		nowx=$(use wxwidgets && echo "false" || echo "true") \
		install=global \
		prefix="/usr" \
		destdir="${D}" \
		flavor=release \
		shared_glew=true \
		shared_lzo=true \
		shared_sdl=true \
		shared_zlib=true \
		shared_sfml=false \
		shared_soil=false \
		verbose=true
}

src_install() {
	cd "${S}"
	scons install

	local binary="${PN}"
	use wxwidgets || binary+="-nogui"

	dodoc Readme.txt
	if use doc; then
		doins -r docs
	fi

	if use wxwidgets; then
		doicon Source/Core/DolphinWX/resources/Dolphin.xpm || die
		make_desktop_entry "${binary}" "Dolphin" "Dolphin" "Game;Emulator"
	fi

	prepgamesdirs
}

pkg_postinst() {
	echo

	if ! use portaudio; then
		ewarn "If you need to use your microphone for a game, rebuild with USE=portaudio"
		echo
	fi

	if ! use wxwidgets; then
		ewarn "Note: It is not currently possible to configure Dolphin without the GUI."
		ewarn "Rebuild with USE=wxwidgets to enable the GUI if needed."
		echo
	fi

	games_pkg_postinst
}