SUMMARY="Open source reimplementation of TES III: Morrowind"
DESCRIPTION="OpenMW is a new and modern engine based on the one that runs the \
2002 open-world RPG Morrowind. The engine (OpenMW) will come with its own editor \
(OpenCS) which will allow the user to edit or create their own games. Both OpenCS \
and OpenMW are written from scratch and aren’t made to support any third party \
programs the original Morrowind engine uses to improve its functionality."
HOMEPAGE="https://openmw.org/"
COPYRIGHT="2008-2021 OpenMW"
LICENSE="
	GNU GPL v3
	MIT
	Zlib
	"
REVISION="1"
SOURCE_URI="https://github.com/OpenMW/openmw/archive/openmw-$portVersion.tar.gz"
SOURCE_FILENAME="openmw-$portVersion.tar.gz"
CHECKSUM_SHA256="bd7f77e1527c2180e9b0dfcbe401d6fb48f24dbb37701dac7747697873d6edb4"
SOURCE_DIR="openmw-openmw-$portVersion"
osgVersion="3.6.5"
SOURCE_URI_2="https://github.com/openscenegraph/OpenSceneGraph/archive/OpenSceneGraph-$osgVersion.tar.gz"
CHECKSUM_SHA256_2="aea196550f02974d6d09291c5d83b51ca6a03b3767e234a8c0e21322927d1e12"
SOURCE_DIR_2="OpenSceneGraph-OpenSceneGraph-$osgVersion"
PATCHES="openmw-$portVersion.patchset"
PATCHES_2="osg-$osgVersion.patchset"

ADDITIONAL_FILES="
	openmw.rdef.in
	haikuicons.zip
	"

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

PROVIDES="
	openmw$secondaryArchSuffix = $portVersion
	app:OpenMW$secondaryArchSuffix = $portVersion
	app:OpenMW_Launcher$secondaryArchSuffix = $portVersion
	app:OpenMW_Wizard$secondaryArchSuffix = $portVersion
	app:OpenMW_CS$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libboost_filesystem$secondaryArchSuffix
	lib:libboost_system$secondaryArchSuffix
	lib:libboost_program_options$secondaryArchSuffix
	lib:libBulletCollision$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libGLU$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libLinearMath$secondaryArchSuffix
	lib:liblz4$secondaryArchSuffix
	lib:libMyGUIEngine$secondaryArchSuffix
	lib:libopenal$secondaryArchSuffix
	lib:libpng$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libSDL2_2.0$secondaryArchSuffix
	lib:libSDL2_image_2.0$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libunshield$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libboost_filesystem$secondaryArchSuffix
	devel:libboost_system$secondaryArchSuffix
	devel:libboost_program_options$secondaryArchSuffix
	devel:libBulletCollision$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libGLU$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libLinearMath$secondaryArchSuffix
	devel:liblz4$secondaryArchSuffix
	devel:libMyGUIEngine$secondaryArchSuffix
	devel:libopenal$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libQt5Gui$secondaryArchSuffix
	devel:libQt5Widgets$secondaryArchSuffix
	devel:libSDL2_2.0$secondaryArchSuffix
	devel:libSDL2_image_2.0$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libunshield$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:lrelease$secondaryArchSuffix >= 5
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:zip
	"

PATCH()
{
	unzip -o $sourceDir/../../additional-files/haikuicons.zip -d $sourceDir
}

BUILD()
{
	mkdir -p $sourceDir2/build
	cd $sourceDir2/build
	cmake .. \
		-DCMAKE_INSTALL_PREFIX=$appsDir/OpenMW \
		-DCMAKE_BUILD_TYPE=Release \
		-DSDL2_INCLUDE_DIR=/system/$relativeIncludeDir/SDL2 \
		-DBUILD_OSG_PLUGINS_BY_DEFAULT=OFF \
		-DBUILD_OSG_PLUGIN_OSG=ON \
		-DBUILD_OSG_PLUGIN_DDS=ON \
		-DBUILD_OSG_PLUGIN_TGA=ON \
		-DBUILD_OSG_PLUGIN_BMP=ON \
		-DBUILD_OSG_PLUGIN_JPEG=ON \
		-DBUILD_OSG_PLUGIN_PNG=ON \
		-DBUILD_OSG_DEPRECATED_SERIALIZERS=OFF \
		-DBUILD_OSG_APPLICATIONS=OFF \
		-DOSG_GL3_AVAILABLE=OFF \
		-DOSG_WINDOWING_SYSTEM=None
	make $jobArgs
	make install
	export OSG_DIR=$appsDir/OpenMW

	mkdir -p $sourceDir/build
	cd $sourceDir/build
	cmake .. \
		-DCMAKE_INSTALL_PREFIX=$appsDir/OpenMW \
		-DCMAKE_BUILD_TYPE=Release \
		-DBullet_INCLUDE_DIR=/system/$relativeIncludeDir/bullet \
		-DDESIRED_QT_VERSION=5 \
		-DBUILD_BSATOOL=ON \
		-DBUILD_DOCS=OFF \
		-DBUILD_ESMTOOL=OFF \
		-DBUILD_LAUNCHER=ON \
		-DBUILD_NIFTEST=OFF \
		-DBUILD_OPENCS=ON \
		-DBUILD_UNITTESTS=OFF \
		-DBUILD_WIZARD=ON
	make $jobArgs
}

INSTALL()
{
	cd $sourceDir2/build
	make install
	cd $sourceDir/build
	make install

	mv -f $appsDir/OpenMW/games/openmw/* $appsDir/OpenMW
	rm -rf $appsDir/OpenMW/{applications,data,games,include,licenses,lib/pkgconfig,metainfo,pixmaps}

	# capitalize application names
	mv $appsDir/OpenMW/openmw $appsDir/OpenMW/OpenMW
	mv $appsDir/OpenMW/openmw-launcher $appsDir/OpenMW/OpenMW-Launcher
	mv $appsDir/OpenMW/openmw-wizard $appsDir/OpenMW/OpenMW-Wizard
	mv $appsDir/OpenMW/openmw-cs $appsDir/OpenMW/OpenMW-CS

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

	local APP_SIGNATURE="application/x-vnd.openmw-launcher"
	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/openmw.rdef.in > $sourceDir/build/openmw-launcher.rdef
	addResourcesToBinaries  $sourceDir/build/openmw-launcher.rdef $appsDir/OpenMW/OpenMW-Launcher

	local APP_SIGNATURE="application/x-vnd.openmw-cs"
	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/openmw.rdef.in > $sourceDir/build/openmw-cs.rdef
	addResourcesToBinaries  $sourceDir/build/openmw-cs.rdef $appsDir/OpenMW/OpenMW-CS

	local APP_SIGNATURE="application/x-vnd.openmw-wizard"
	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/openmw.rdef.in > $sourceDir/build/openmw-wizard.rdef
	addResourcesToBinaries  $sourceDir/build/openmw-wizard.rdef $appsDir/OpenMW/OpenMW-Wizard
	mimeset -f $appsDir/OpenMW/OpenMW-Launcher

	addAppDeskbarSymlink $appsDir/OpenMW/OpenMW-Launcher Morrowind
}
