SUMMARY="An open-source rewrite engine of the Cave Story"
DESCRIPTION="The platform-adventure game focuses on an amnesiac protagonist \
who awakens in a cave. Through his explorations, he discovers a plot by the \
Doctor, a megalomaniac who intends to force the inhabitants of the cave to \
fight for him in his bid to conquer the world. The protagonist is thrust \
into the position of savior as he endeavors to defeat the Doctor."
HOMEPAGE="https://exlmoto.ru/nxengine/"
COPYRIGHT="2010-2016 Caitlin (rogueeve) Shaw"
LICENSE="GNU GPL v3"
REVISION="4"
srcGitRev="e03f6cb28a65f218ad08c1c7c3bfe4fef05b43db"
portVersionSub="`echo $portVersion | cut -c 1-7`"
SOURCE_URI="https://github.com/EXL/NXEngine/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="291d05078b555437c25f708bdd1e26a8de68086b596afae094c43c3bfd0a5a41"
SOURCE_DIR="NXEngine-$srcGitRev"
SOURCE_URI_2="https://www.cavestory.org/downloads/LIN64-NXEngine-$portVersionSub-Rev-4.tar.gz"
CHECKSUM_SHA256_2="ddc1f69349bd4239adc6485e0377b503425a7960bbd6696c1b30ea6ed11e1851"
SOURCE_DIR_2="NXEngine"
SOURCE_URI_3="https://www.cavestory.org/downloads/LIN64-NXEngine-RUS-$portVersionSub-Rev-4.tar.gz"
CHECKSUM_SHA256_3="f43b87a69ce93d99c2261b2a7f637fcbc25a8f3b677e178d76659be5e1ccb556"
SOURCE_DIR_3="NXEngine-RUS"
ADDITIONAL_FILES="
	nxengine.rdef.in
	NXEngine.sh
	"

ARCHITECTURES="all !x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	nxengine$secondaryArchSuffix = $portVersion
	app:nxengine$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libSDL_1.2$secondaryArchSuffix
	lib:libSDL_ttf_2.0$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel >= r1~alpha4_pm_hrev51410-1
	devel:libfreetype$secondaryArchSuffix
	devel:libSDL$secondaryArchSuffix
	devel:libSDL_ttf$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:make
	"

BUILD()
{
	mkdir -p build/
	make -f Makefile.haiku clean && rm -f nx
	make $jobArgs -f Makefile.haiku
	cp nx build/nx
	make -f Makefile.haiku clean && rm -f nx
	make $jobArgs DEFINES="-D_L10N_CP1251" -f Makefile.haiku
	cp nx build/nx-rus
}

INSTALL()
{
	cd build/
	mkdir -p $appsDir/NXEngine/{ENG,RUS}

	cp $portDir/additional-files/NXEngine.sh "$appsDir/NXEngine/Cave Story"
	chmod +x "$appsDir/NXEngine/Cave Story"

	cp -r $sourceDir2/* $appsDir/NXEngine/ENG
	cp -r $sourceDir3/* $appsDir/NXEngine/RUS
	rm -rf $appsDir/NXEngine/{ENG,RUS}/nx
	rm -rf $appsDir/NXEngine/{ENG,RUS}/settings.dat
	cp nx "$appsDir/NXEngine/ENG/Cave Story"
	cp nx-rus "$appsDir/NXEngine/RUS/Cave Story"

	local APP_SIGNATURE="application/x-vnd.nxengine"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f4 | cut -c 1-1`"
	local LONG_INFO="$SUMMARY"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/nxengine.rdef.in > $sourceDir/build/nxengine-launcher.rdef
	rc nxengine-launcher.rdef
	resattr -o "$appsDir/NXEngine/Cave Story" nxengine-launcher.rsrc

	local APP_SIGNATURE="application/x-vnd.nxengine-eng"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/nxengine.rdef.in > $sourceDir/build/nxengine-eng.rdef
	addResourcesToBinaries  $sourceDir/build/nxengine-eng.rdef \
		"$appsDir/NXEngine/ENG/Cave Story"

	local APP_SIGNATURE="application/x-vnd.nxengine-rus"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		-e "s|@LONG_INFO@|$LONG_INFO|" \
		$portDir/additional-files/nxengine.rdef.in > $sourceDir/build/nxengine-rus.rdef
	addResourcesToBinaries  $sourceDir/build/nxengine-rus.rdef \
		"$appsDir/NXEngine/RUS/Cave Story"

	addAppDeskbarSymlink "$appsDir/NXEngine/Cave Story"
}
