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

EAPI="3"

inherit games

MY_BUILD=1324610248

DESCRIPTION="Jamestown: Legend of the Lost Colony is a vertically scrolling shoot'em up"
HOMEPAGE="http://finalformgames.com/jamestown/"
SRC_URI="jtownlinux_${PV//./_}_${MY_BUILD}.zip"

LICENSE="as-is BSD LGPL-2 MIT"
SLOT="0"
KEYWORDS="amd64 ~x86"
IUSE=""
RESTRICT="strip fetch"

DEPEND="app-arch/unzip"
RDEPEND="=media-libs/libsdl-1.2*[audio,joystick,video]
	=media-libs/openal-1*
	virtual/opengl
	x11-libs/libX11
	x11-libs/libxcb
	x11-libs/libXdmcp
	x11-libs/libXext"

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

src_unpack() {
	# self unpacking zip archive; unzip warns about the exe stuff
	local a="${DISTDIR}/${A}"
	echo ">>> Unpacking ${a} to ${PWD}"
	unzip -q "${a}"
	[ $? -gt 1 ] && die "unpacking zip failed"
	unzip -q "JamestownInstaller_${PV//./_}-bin"
	[ $? -gt 1 ] && die "unpacking bin failed"
	rm "JamestownInstaller_${PV//./_}-bin"
}

src_install() {
	local dir="${GAMES_PREFIX_OPT}/${PN}"

	insinto "${dir}"
	exeinto "${dir}"

	# Temporarily move files that must be processed separately
	mkdir other
	mv data/*.{TXT,txt,png} other/
	mv data/{Jamestown*,amd64,x86} other/

	# Remove unneeded libraries
	rm other/{amd64,x86}/{libSDL,libopenal}*

	# Set proper path for built-in wrapper
	sed -e "s;^\\(.*JAMESTOWN_DATA_PATH=\\).*\$;\1\"${dir}\";" \
		-i other/Jamestown || die "sed path update failed"

	# Install game data files and libraries
	doins -r data/* || die "doins data failed"
	doins -r other/${ARCH} || die "doins libraries failed"
	doexe other/Jamestown-${ARCH} || die "doexe failed"

	# Install wrapper
	exeinto "${GAMES_BINDIR}"
	newexe other/Jamestown ${PN} || die "newexe failed"

	# Install shortcut
	doicon other/"${PN}.png" || die "doicon failed"
	make_desktop_entry "${PN}" "Jamestown" "${PN}"

	# Install documentation
	dodoc other/[JLR]*.txt || die "dodoc failed"

	prepgamesdirs
}