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

EAPI=2

inherit games

MY_PN=${PN/-bin}
MY_PV=${PV##*.}
MY_SRC_URI="http://buildbot.orphis.net/jpcsp/"

DESCRIPTION="A Java-based PlayStation Portable (PSP) emulator"
HOMEPAGE="http://www.jpcsp.org/"
SRC_URI="amd64? ( jpcsp-${MY_PV}-linux-amd64.7z )
x86? ( jpcsp-${MY_PV}-linux-x86.7z )"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="amd64 ~x86"
IUSE=""
RESTRICT="fetch strip"

RDEPEND="virtual/opengl
	virtual/jre"
DEPEND=""

S="${WORKDIR}/${MY_PN}-linux-${ARCH}"
GAMEDIR="${GAMES_PREFIX_OPT}/${MY_PN}"

# Function to install an appropriate wrapper to setup and launch jpcsp
create_wrapper() {
echo "#!/bin/sh

# Define directories
BINDIR=\"${GAMEDIR}\"
RUNDIR=~/.jpcsp

# Create and initialize prefs directory if necessary
if [ ! -d \"\${RUNDIR}\" ]; then
	mkdir -p \"\${RUNDIR}\"/umdimages
	mkdir -p \"\${RUNDIR}\"/ms0/PSP/GAME
	cp \"\${BINDIR}\"/LogSettings.xml \"\${RUNDIR}\"
	ln -s \"\${BINDIR}\"/{demos,flash0}* \"\${RUNDIR}\"
	ln -s \"\${BINDIR}\"/umdimages/* \"\${RUNDIR}\"/umdimages
	ln -s \"\${BINDIR}\"/ms0/PSP/GAME/* \"\${RUNDIR}\"/ms0/PSP/GAME
fi

# Launch emulator from personal directory
cd \"\${RUNDIR}\"
java -Xmx1024m -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m -Djava.library.path=\"\${BINDIR}\"/lib/linux-${ARCH} -jar \"\${BINDIR}\"/bin/jpcsp.jar \"\$@\"" \
	>${PN}
}

pkg_nofetch() {
	einfo "Packages cannot be downloaded automatically.  Please download version"
	einfo "r${MY_PV} of the Linux ${ARCH} package from this site:"
	einfo "${MY_SRC_URI}"
	echo
	einfo "Then, move to: '${DISTDIR}'"
}

src_install() {
	insinto "${GAMEDIR}"
	doins {Compiler,LogSettings}.xml || die "doins xml failed"
	doins -r bin demos flash0 lib ms0 patches umdimages \
		|| die "doins dir failed"

	exeinto "${GAMES_BINDIR}"
	create_wrapper || die "create wrapper failed"
	doexe ${PN} || die "doexe failed"

	dodoc README.txt

	make_desktop_entry ${PN} "JPCSP" || die "install shortcut failed"
	prepgamesdirs
}

pkg_postinst() {
	games_pkg_postinst
}