# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=7

inherit desktop wrapper xdg-utils

DESCRIPTION="Return to Castle Wolfenstein with ioquake3 improvements"
HOMEPAGE="https://github.com/iortcw/iortcw/"
SRC_URI="https://github.com/iortcw/iortcw/archive/${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/iortcw/iortcw/releases/download/${PV}/patch-data-141.zip"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64 ~x86"
PROPERTIES="interactive"

RDEPEND="virtual/opengl
	media-libs/libsdl2"
DEPEND="${RDEPEND}"

S="${WORKDIR}/${P}/SP"

RTCW_DATA_ROOT=${RTCW_DATA_ROOT:-"/usr/portage/distfiles/rtcw-data"}

pkg_setup() {
	while true ; do
		if [ -e ${RTCW_DATA_ROOT}/main/pak0.pk3 ] ; then
			break
		else
			einfo
			einfo "You must provide game data in "
			einfo "${RTCW_DATA_ROOT}."
			einfo "You can change the path by exporting environment variable "
			einfo "RTCW_DATA_ROOT pointing to the custom location."
			einfo
			einfo "Press Return to scan the directory again"
			einfo "or hit CTRL+C to abort the emerge."
			read
		fi
	done
}

src_compile() {
	if [ "${ARCH}" == "amd64" ] ; then
		ARCH="x86_64"
	fi
	emake
}

src_install() {
	destDir=${EROOT}/usr/share/${PN}

	emake COPYDIR="${D}/${destDir}" copyfiles

	make_wrapper iortcwsp "./iowolfsp.${ARCH}" "${destDir}"
	dodoc ../README.md
	newicon -s scalable misc/wolf.svg iortcwsp.svg
	make_desktop_entry iortcwsp "Return to Castle Wolfenstein (SP)" iortcwsp

	insinto "${destDir}/main"

	# copy data
	doins ${RTCW_DATA_ROOT}/main/pak0.pk3 \
		  ${RTCW_DATA_ROOT}/main/sp_pak{1,2,3}.pk3

	# install data patch
	cd "${WORKDIR}/main"
	doins sp_pak*.pk3
}

pkg_postinst() {
	xdg_icon_cache_update
}

pkg_postrm() {
	xdg_icon_cache_update
}