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

inherit eutils toolchain-funcs games 

DESCRIPTION="Playstation2 emulator"
HOMEPAGE="http://www.pcsx2.net/"
SRC_URI="http://www.pcsx2.net/download/0.8release/pcsx0.9.3_and_plugins_src.7z"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="debug alsa oss"

DEPEND="sys-libs/zlib
        media-gfx/nvidia-cg-toolkit
        x11-libs/cairo
        x11-libs/pango
        media-libs/glitz
        media-libs/libpng
        dev-libs/atk
        x11-proto/xf86vidmodeproto
        >=media-libs/freetype-2
        x11-libs/libXxf86vm

        >=x11-libs/gtk+-2"

RDEPEND="debug? ( sys-devel/gdb )
        ${DEPEND}"

S=${WORKDIR}

src_unpack() {

	7z x "${DISTDIR}/pcsx0.9.3_and_plugins_src.7z" || die "unpack failed"
	cd "${S}"
	sed -i \
		-e 's/make install/make/'  \
		pcsx2/build.sh \
		|| die "sed failed"

	if use debug; then
	sed -i \
                       -e 's/export PCSX2OPTIONS="/export PCSX2OPTIONS="--enable-debug /'  \
                        build.sh \
                        || die "sed failed"
                sed -i \
                        -e 's/-g "/-ggdb "/'  \
                        pcsx2/configure.ac \
                        -e 's/-g "/-ggdb "/'  \
                        plugins/gs/zerogs/opengl/configure.ac \
                        -e 's/-g "/-ggdb "/'  \
                        plugins/pad/zeropad/configure.ac \
                        || die "sed failed"
                sed -i \
                        -e 's/configure /configure --enable-debug /'  \
                        plugins/gs/zerogs/build.sh \
                        || die "sed failed"

                sed -i \
                        -e 's/configure /configure --enable-debug /'  \
                        plugins/pad/zeropad/build.sh \
                        || die "sed failed"
        fi

        if ! use alsa; then
                if use oss; then
                         sed -i \
                                -e 's/USEALSA = TRUE/USEALSA = FALSE/'  \
                                plugins/spu2/PeopsSPU2/Makefile \
                                || die "sed failed"
                fi
        fi

        chmod +x plugins/gs/zerogs/opengl/configure
}

src_compile() {

        sh build.sh all || die "compile failed"
}

src_install() {

        newgamesbin pcsx2/Linux/pcsx2 pcsx2.bin || die "newgamesbin failed"
        dogamesbin "${FILESDIR}/pcsx2" || die "dogamesbin failed"

        insinto "${GAMES_DATADIR}/${PN}"
        doins -r bin/* || die "installing data failed"
        insinto "${GAMES_DATADIR}/${PN}/.pixmaps"
        doins -r bin/.pixmaps/* pcsx2/Linux/.pixmaps/* || die "installing pixmap data failed"

        sed -i \
                -e "s:GAMES_BINDIR:${GAMES_BINDIR}:" \
                -e "s:GAMES_DATADIR:${GAMES_DATADIR}:" \
                "${D}/${GAMES_BINDIR}/pcsx2" \
                || die "sed failed"

        dodoc pcsx2/Docs/*.txt
        prepgamesdirs
}