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

EAPI=8

inherit cmake

MY_PN="serioussam"
MY_MOD="Tower"
# Game name
GN="serioussam"
# URL prefix
URL="https://github.com/tx00100xt/"

DESCRIPTION="Serious Sam Classic Nightmare Tower Modification"
HOMEPAGE="https://github.com/tx00100xt/SE1-TFE-Tower"
SRC_URI="${URL}SE1-TFE-${MY_MOD}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	${URL}serioussam-mods/raw/main/SamTFE-${MY_MOD}/SamTFE-${MY_MOD}.tar.xz"
S="${WORKDIR}/SE1-TFE-${MY_MOD}-${PV}/Sources"

MY_CONTENT="${WORKDIR}/SE1-TFE-${MY_MOD}-${PV}/${MY_PN}"
MY_MOD_ARC="SamTFE-${MY_MOD}.tar.xz"

LICENSE="GPL-2 BSD ZLIB"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE=""

RDEPEND="games-fps/serioussam"
DEPEND="${RDEPEND}"
BDEPEND="virtual/pkgconfig"

src_configure() {
	einfo "Setting build type Release..."
	CMAKE_BUILD_TYPE="Release"
	local mycmakeargs=(
		-DTFE=TRUE
	)
	cmake_src_configure
}

src_install() {
	local dir="/usr/share/${GN}"
	if use x86; then
		local libdir="/usr/lib"
	else
		local libdir="/usr/lib64"
	fi

	# crerate install dirs
	mkdir "${D}/usr" && mkdir "${D}/usr/share" mkdir "${D}${libdir}" \
		|| die "Failed create install dir"
	for gamedir in ${GN} ${GN}/Mods ${GN}/Mods/${MY_MOD}
	do
		mkdir "${D}${libdir}/${gamedir}" || die "Failed create mod dir"
	done
	mkdir "${D}${dir}" || die "Failed to create install dir"

	# unpack mod content
	cat "${DISTDIR}/${MY_MOD_ARC}" > "${MY_MOD_ARC}" \
		|| die "Failed to copy archive"
	unpack ./"${MY_MOD_ARC}"
	mv Mods "${D}${dir}" || die "Failed to moved mod content"

	# moving libs
	if use x86; then
		mv "${BUILD_DIR}"/Debug/libEntities.so \
			"${D}/usr/lib/${GN}/Mods/${MY_MOD}" \
				|| die "Failed to moved libEntities.so"
		mv "${BUILD_DIR}"/Debug/libGame.so \
			"${D}/usr/lib/${GN}/Mods/${MY_MOD}" \
				|| die "Failed to moved libGame.so"
	else
		mv "${BUILD_DIR}"/Debug/libEntities.so \
			"${D}/usr/lib64/${GN}/Mods/${MY_MOD}" \
				|| die "Failed to moved libEntities.so"
		mv "${BUILD_DIR}"/Debug/libGame.so \
			"${D}/usr/lib64/${GN}/Mods/${MY_MOD}" \
				|| die "Failed to moved libGame.so"
	fi
	# removing temp stuff
	rm -f  "${BUILD_DIR}"/{*.cmake,*.txt,*.a,*.ninja,.gitkeep} \
		|| die "Failed to removed temp stuff"
	rm -fr "${BUILD_DIR}"/Debug && rm -fr \
		"${BUILD_DIR}"/CMakeFiles && rm -fr "${MY_CONTENT}" \
		|| die "Failed to removed temp stuff"
}

pkg_postinst() {
	elog "     Serious Sam Nightmare Tower Modifications installed"
}