SUMMARY="Free implementation of Heroes of the Might and Magic II engine"
DESCRIPTION="Free Heroes 2 is a turn based strategy video game.
The original game was voted the sixth best PC game of all time by PC Gamer \
magazine in May 1997."
HOMEPAGE="https://github.com/ihhub/fheroes2/"
COPYRIGHT="2006-2021 fheroes2 team"
LICENSE="GNU GPL v2"
REVISION="1"
SOURCE_URI="https://github.com/ihhub/fheroes2/archive/refs/tags/$portVersion.tar.gz"
CHECKSUM_SHA256="afd56848083a28effcc8eb5a10983abe27f8ec8a469f470b04317e822ac27cb4"
SOURCE_DIR="fheroes2-$portVersion"
srcGitRev2="4f376fe483a6881a44d2565c98a81b3c5c72daad"
SOURCE_URI_2="https://github.com/EXL/BeGameLauncher/archive/$srcGitRev2.tar.gz"
CHECKSUM_SHA256_2="4dbad52eca89eab9e5fadf1bfdfb88db29d471f4286995088df5d1278bba2c51"
SOURCE_DIR_2="BeGameLauncher-$srcGitRev2"
PATCHES="fheroes2-$portVersion.patchset"
ADDITIONAL_FILES="fheroes2.rdef.in"

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

PROVIDES="
	fheroes2$secondaryArchSuffix = $portVersion
	app:FHeroes2 = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libogg$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libSDL_1.2$secondaryArchSuffix
	lib:libSDL_image_1.2$secondaryArchSuffix
	lib:libSDL_mixer_1.2$secondaryArchSuffix
	lib:libSDL_net_1.2$secondaryArchSuffix
	lib:libSDL_ttf_2.0$secondaryArchSuffix
	lib:libvorbis$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libfreetype$secondaryArchSuffix
	devel:libgettextlib$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libogg$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libSDL_1.2$secondaryArchSuffix
	devel:libSDL_image_1.2$secondaryArchSuffix
	devel:libSDL_mixer_1.2$secondaryArchSuffix
	devel:libSDL_net_1.2$secondaryArchSuffix
	devel:libSDL_ttf_2.0$secondaryArchSuffix
	devel:libvorbis$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	"

BUILD()
{
	mkdir -p build
	cd build
	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=$appsDir/fHeroes2 \
		-DCMAKE_INSTALL_BINDIR=$appsDir/fHeroes2 \
		-DCONFIGURE_FHEROES2_DATA=$appsDir/fHeroes2 \
		-DCMAKE_INSTALL_DOCDIR=$appsDir/fHeroes2/docs \
		-DGET_HOMM2_DEMO=OFF \
		-DUSE_SDL_VERSION=SDL \
		-DENABLE_IMAGE=ON

	make $jobArgs

	cd $sourceDir2
	mkdir -p build
	cd build
	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DLAUNCHER=fheroes2

	make $jobArgs
}

INSTALL()
{
	cd build
	make install

	mv $appsDir/fHeroes2/fheroes2 $appsDir/fHeroes2/fHeroes2
	cp $sourceDir/fheroes2.key $appsDir/fHeroes2

	cp $sourceDir2/build/fHeroes2Launcher $appsDir/fHeroes2/fHeroes2-Launcher

	local APP_SIGNATURE="application/x-vnd.fheroes2"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	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/fheroes2.rdef.in > $sourceDir/fheroes2.rdef

	addResourcesToBinaries  $sourceDir/fheroes2.rdef $appsDir/fHeroes2/fHeroes2

	addAppDeskbarSymlink $appsDir/fHeroes2/fHeroes2-Launcher "Free Heroes 2"
}
