SUMMARY="Open source clone of Theme Hospital"
DESCRIPTION="
CorsixTH aims to reimplement the game engine of Theme Hospital, and be able to \
load the original game data files. This means that you will need a purchased \
copy of Theme Hospital, or a copy of the demo, in order to use CorsixTH. After \
most of the original engine has been reimplemented in open source code, the \
project will serve as a base from which extensions and improvements to the \
original game can be made.
"
HOMEPAGE="https://github.com/CorsixTH"
COPYRIGHT="2005-2018 Corsix"
LICENSE="MIT"
REVISION="1"
SOURCE_URI="https://github.com/CorsixTH/CorsixTH/archive/v$portVersion.tar.gz"
CHECKSUM_SHA256="b219270310255493c881a09bb4a5569f34a72cdaf3c3be920c1036a2450317ce"
SOURCE_FILENAME="CorsixTH-$portVersion.tar.gz"
SOURCE_DIR="CorsixTH-$portVersion"
PATCHES="corsix_th-$portVersion.patchset"
ADDITIONAL_FILES="corsix_th.rdef.in"

ARCHITECTURES="all !x86_gcc2"
if [ "$targetArchitecture" = x86_gcc2 ]; then
SECONDARY_ARCHITECTURES="x86"
fi

PROVIDES="
	corsix_th$secondaryArchSuffix = $portVersion
	cmd:corsix_th = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:liblua$secondaryArchSuffix >= 5.3
	lib:libsdl2_2.0$secondaryArchSuffix
	lib:libsdl2_mixer_2.0$secondaryArchSuffix
	lib:lfs$secondaryArchSuffix
	lib:lpeg$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:liblua$secondaryArchSuffix >= 5.3
	devel:libsdl2$secondaryArchSuffix
	devel:libsdl2_mixer$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"

PATCH()
{
	LUAC_DIR=$(finddir B_SYSTEM_LIB_DIRECTORY)$secondaryArchSubDir/lua/5.3/?.so
	sed -i \
		-e "s|package.cpath = base_dir .. '?.so;' .. package.cpath|package.cpath = base_dir .. '?.so;' .. package.cpath .. ';$LUAC_DIR'|" \
		CorsixTH/CorsixTH.lua
}

BUILD()
{
	cmake . -DLUA_LIBRARY=/system/$relativeLibDir/liblua.so.5.3 \
		-DCMAKE_INSTALL_PREFIX=$prefix \
		-DCMAKE_INSTALL_DATAROOTDIR=$dataDir \
		-DSDL_INCLUDE_DIR=/system/$relativeIncludeDir/SDL2 \
		-DSDL_MIXER_INCLUDE_DIR=/system/$relativeIncludeDir/SDL2 \
		-DLUA_INCLUDE_DIR=/system/$relativeIncludeDir/lua53
	cd CorsixTH
	make $jobArgs
}

INSTALL()
{
	touch CorsixTH/LICENSE.txt
	make install
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	sed \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		$portDir/additional-files/corsix_th.rdef.in > $sourceDir/corsix_th.rdef
	addResourcesToBinaries $sourceDir/corsix_th.rdef $prefix/bin/corsix-th
	addAppDeskbarSymlink $prefix/bin/corsix-th "Corsix Theme Hospital"
}
