# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/games-action/descent3/descent3-1.4.0b-r1.ebuild,v 1.18 2012/02/05 05:22:29 vapier Exp $

EAPI=5

inherit eutils unpacker cdrom multilib games

MY_EXE="setup_descent3_2.0.0.16.exe"

DESCRIPTION="Descent 3 - 3-Dimensional indoor/outdoor spaceship combat"
HOMEPAGE="http://www.lokigames.com/products/descent3/"
SRC_URI="mirror://lokigames/${PN}/${PN}-1.4.0a-x86.run
	mirror://lokigames/${PN}/${P}-x86.run
	!cdinstall? ( ${MY_EXE}
		https://www.legroom.net/files/software/D3-linux-files-2010.tar.gz )"

LICENSE="LOKI-EULA"
SLOT="0"
KEYWORDS="amd64 ~x86"
RESTRICT="strip"
IUSE="+cdinstall nocd videos"

DEPEND="!cdinstall? ( app-arch/innoextract )"
RDEPEND="sys-libs/lib-compat-loki
	sys-libs/glibc
	media-libs/libsdl[abi_x86_32(-)]
	media-libs/smpeg[abi_x86_32(-)]
	virtual/opengl[abi_x86_32(-)]
	x11-libs/libX11[abi_x86_32(-)]
	x11-libs/libXext[abi_x86_32(-)]"

REQUIRED_USE="!cdinstall? ( nocd videos )"

dir=${GAMES_PREFIX_OPT}/${PN}
Ddir=${D}/${dir}
S=$WORKDIR

QA_TEXTRELS="
opt/descent3/netgames/Anarchy.d3m
opt/descent3/netgames/co-op.d3m
opt/descent3/netgames/ctf.d3m
opt/descent3/netgames/entropy.d3m
opt/descent3/netgames/hoard.d3m
opt/descent3/netgames/hyperanarchy.d3m
opt/descent3/netgames/monsterball.d3m
opt/descent3/netgames/robo-anarchy.d3m
opt/descent3/netgames/tanarchy.d3m
"

pkg_setup() {
	games_pkg_setup
	if use videos ; then
		ewarn "The installed game takes about 1.2GB of space!"
	elif use nocd ; then
		ewarn "The installed game takes about 510MB of space!"
	else
		ewarn "The installed game takes about 220MB of space!"
	fi
}

src_unpack() {
	if use cdinstall; then
		# Confirm CD
		if use videos ; then
			cdrom_get_cds missions/d3.mn3 movies/level1.mve
		else
			cdrom_get_cds missions/d3.mn3
		fi
		# Unpack resources from CD
		F_SOURCE=$CDROM_ROOT
		mkdir "${S}"/data && cd "${S}"/data
		unpack "${F_SOURCE}"/data.tar.gz || die "uncompressing data"
		unpack "${F_SOURCE}"/shared.tar.gz || die "uncompressing shared"
	else
		# Unpack GOG game data
		mkdir "${S}"/{gog,linux}
		F_SOURCE="${S}/gog/app"
		einfo "Unpacking ${MY_EXE}.  This will take a while..."
		mkdir -p "${S}/gog" && cd "${S}/gog" || die "mkdir failed"
		innoextract -e -s -p1 -L "${DISTDIR}/${MY_EXE}" \
			|| die "innoextract failed"
		cd ..

		# Unpack Linux-specific game files
		cd "${S}"/linux
		unpack "D3-linux-files-2010.tar.gz" || die "uncompressing Linux files"

	fi

	# Unpack runtime patches
	mkdir "${S}"/patch-{a,b}
	cd "${S}"/patch-a
	unpack_makeself ${PN}-1.4.0a-x86.run
	cd "${S}"/patch-b
	unpack_makeself ${P}-x86.run
}

src_install() {
	einfo "   Copying data files..."
	exeinto "${dir}"
	insinto "${dir}"

	# Copy files from original Linux CD
	if use cdinstall; then
		doexe ${F_SOURCE}/bin/x86/glibc-2.1/{${PN},nettest} \
			|| die "copying executables"
		doins ${F_SOURCE}/d3.hog || die "copying files"
		# since these files are coming off a cd, the times/sizes/md5sums won't
		# be different ... that means portage will try to unmerge some files (!)
		# we run touch to update the timestamps
		find "${Ddir}" -exec touch "{}" +
		# install remaining files from CD
		doins -r data/* || die "copying cd data files"
		dodoc "${F_SOURCE}"/FAQ.txt,README{,.mercenary} || die "copying docs"
		newicon "$F_SOURCE}"/icon.xpm ${PN}.xpm

	# Otherwise, copy files from GOG package
	else
		# From the Linux package...
		doexe linux/{${PN}{,.dynamic},nettest} || die "copying executables"
		doins -r linux/{d3-linux.hog,netgames,online} \
			|| die "copying Linux files"
		dodoc linux/README* || die "copying docs"
		newicon linux/icon.xpm ${PN}.xpm

		# From the Windows install...
		doins "${F_SOURCE}"/*.hog || die "copying gog data files"
		dodoc "${F_SOURCE}"/language/readmes/d3-faq.txt
	fi

	# Optionally install mission files to prevent need for CD
	if use nocd; then
		einfo "   Copying nocd files..."
		doins -r ${F_SOURCE}/missions || die "copying missions"
	fi

	# Optionall install video files
	if use videos ; then
		use cdinstall && cdrom_load_next_cd
		einfo "   Copying video files..."
		doins -r ${F_SOURCE}/movies || die "copying movies"
	fi

	# Patch CD files to latest version; Linux bundle is pre=patched
	if use cdinstall; then
		cd "${S}"/patch-a
		bin/Linux/x86/loki_patch --verify patch.dat || die "patch-a verify"
		bin/Linux/x86/loki_patch patch.dat "${Ddir}" >& /dev/null \
			|| die "patch-a install"
		cd "${S}"/patch-b
		bin/Linux/x86/loki_patch --verify patch.dat || die "patch-b verify"
		bin/Linux/x86/loki_patch patch.dat "${Ddir}" >& /dev/null \
			|| die "patch-b install"
	fi


	#dosym /usr/$(get_libdir)/loki_libsmpeg-0.4.so.0 \
	dosym /usr/lib32/loki_libsmpeg-0.4.so.0 \
		"${dir}"/libsmpeg-0.4.so.0 || die "failed compatibility symlink"

	games_make_wrapper ${PN} ./${PN}.dynamic "${dir}" "${dir}"

	# Fix for 2.6 kernel crash
	cd "${Ddir}"
	ln -sf ppics.hog PPics.Hog

	prepgamesdirs
	make_desktop_entry ${PN} "Descent 3" ${PN}
}

pkg_postinst() {
	games_pkg_postinst
	elog "To play the game run:"
	elog " ${PN}"
	echo
}