SUMMARY="The reference frontend of libretro"
DESCRIPTION="RetroArch is a frontend for emulators, game engines and media \
players. It enables you to run classic games on a wide range of computers and \
consoles through its slick graphical interface. It has advanced features like \
shaders, netplay, rewinding, next-frame response times, and more!"
HOMEPAGE="https://www.retroarch.com/"
COPYRIGHT="2010-2022 The RetroArch Team"
LICENSE="GNU GPL v3"
REVISION="1"
SOURCE_URI="https://github.com/libretro/RetroArch/archive/v$portVersion.tar.gz"
CHECKSUM_SHA256="ff9c31abae19528275e40fbe49ef40be9410b4108513ae7dbf325e210e5b5bec"
SOURCE_FILENAME="retroarch-$portVersion.tar.gz"
SOURCE_DIR="RetroArch-$portVersion"
ADDITIONAL_FILES="retroarch.rdef.in"

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

GLOBAL_WRITABLE_FILES="
	$relativeSettingsDir/retroarch.cfg auto-merge
	"
USER_SETTINGS_FILES="
	$relativeSettingsDir/retroarch/retroarch.cfg template \
		$sysconfDir/retroarch.cfg
	"

PROVIDES="
	retroarch$secondaryArchSuffix = $portVersion
	app:retroarch = $portVersion
	app:retroarch_cg2glsl = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	retroarch_assets
	lib:libavcodec$secondaryArchSuffix
	lib:libavdevice$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libsdl2_2.0$secondaryArchSuffix
	lib:libswresample$secondaryArchSuffix
	lib:libswscale$secondaryArchSuffix
	lib:libqt5$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libavdevice$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libsdl2$secondaryArchSuffix
	devel:libswresample$secondaryArchSuffix
	devel:libswscale$secondaryArchSuffix
	devel:libqt5$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:which
	"

PATCH()
{
	libretroDir="`finddir B_SYSTEM_DIRECTORY`/$relativeAddOnsDir/libretro"
	assetsDir="`finddir B_SYSTEM_DATA_DIRECTORY`/retroarch/assets"
	cacheDir="`finddir B_SYSTEM_CACHE_DIRECTORY`/retroarch"
	contentDir="`finddir B_SYSTEM_VAR_DIRECTORY`/retroarch"
	coreAssetsDir="`finddir B_SYSTEM_VAR_DIRECTORY`/retroarch/assets"
	sed -i \
		-e "s|^# \(libretro_directory =\)|\1 \"$libretroDir/\"|;" \
		-e "s|^# \(libretro_info_path =\)|\1 \"$libretroDir/\"|;" \
		-e "s|^# \(assets_directory =\)|\1 \"$assetsDir/\"|;" \
		-e "s|^# \(cache_directory =\)|\1 \"$cacheDir/\"|;" \
		-e "s|^# \(content_directory =\)|\1 \"$contentDir/\"|;" \
		-e "s|^# \(core_assets_directory =\)|\1 \"$coreAssetsDir/\"|;" \
		retroarch.cfg
}

BUILD()
{
	./configure --prefix="$prefix" --sysconfdir="$sysconfDir" \
		--bindir="$appsDir" --datarootdir="$dataRootDir" \
		--docdir="$docDir" --mandir="$manDir" \
		--with-assets_dir="$dataDir" --disable-discord
	make $jobArgs
}

INSTALL()
{
	make install

	install -m 0755 -d "$dataDir"/retroarch
	install -m 0644 -T retroarch.cfg "$settingsDir"/retroarch.cfg
	install -m 0644 -T retroarch.cfg "$dataDir"/retroarch/retroarch.cfg.default

	local APP_SIGNATURE="application/x-vnd.RetroArch"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2`"
	local MINOR="`echo "$portVersion" | cut -d. -f3`"
	sed \
		-e "s|@APP_SIGNATURE@|$APP_SIGNATURE|" \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		$portDir/additional-files/retroarch.rdef.in > retroarch.rdef

	addResourcesToBinaries retroarch.rdef \
		"$appsDir"/retroarch

	addAppDeskbarSymlink "$appsDir"/retroarch "RetroArch"
}
