SUMMARY="A PSP emulator written in C++"
DESCRIPTION="PPSSPP is a PSP (PlayStation Portable) emulator capable of playing the \
majority of the games made for Sony’s first portable console right on your favorite \
Android or PC device."
HOMEPAGE="https://www.ppsspp.org/"
COPYRIGHT="2012-2021 PPSSPP Project"
LICENSE="GNU GPL v2"
REVISION="1"
SOURCE_URI="https://github.com/hrydgard/ppsspp/archive/v$portVersion.tar.gz"
CHECKSUM_SHA256="c9458ab1946d7d665465f69aafdf8598179852fefdcd7aef9050f7278193bec5"
SOURCE_FILENAME="ppsspp-$portVersion.tar.gz"
SOURCE_DIR="ppsspp-$portVersion"
srcGitRev_2="7885552b208493a6a0f21663770c446c3ba65576"
SOURCE_URI_2="https://github.com/Kingcom/armips/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="f8a03906135fb6f2932b80b7ef5991f39ccac46b36ec3690776fb38c69775c3d"
SOURCE_FILENAME_2="armips-$srcGitRev_2.tar.gz"
SOURCE_DIR_2="armips-$srcGitRev_2"
srcGitRev_3="dc11adde23c455a24e13dd54de9b4ede8bdd7db8"
SOURCE_URI_3="https://github.com/hrydgard/glslang/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="32edc362238d5b1136b2b40b0abdeef5a832e599554d2fcf75d7b08e4b0ed0bb"
SOURCE_FILENAME_3="glslang-$srcGitRev_3.tar.gz"
SOURCE_DIR_3="glslang-$srcGitRev_3"
srcGitRev_4="9acb9ec31f5a8ef80ea6b994bb77be787b08d3d1"
SOURCE_URI_4="https://github.com/KhronosGroup/SPIRV-Cross/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="9423aebd6ea4251c6ea736f0291066eb2b99b6f923151edd1a78ce9c29b46757"
SOURCE_FILENAME_4="SPIRV-Cross-$srcGitRev_4.tar.gz"
SOURCE_DIR_4="SPIRV-Cross-$srcGitRev_4"
srcGitRev_5="52d180df0f4e7eee18e8e4846826806f10b16dcb"
SOURCE_URI_5="https://github.com/hrydgard/ppsspp-lang/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="3cbe1fe0438d780da3572927495df3e77560e4c3e9eb51f1ee38ba938138556f"
SOURCE_FILENAME_5="ppsspp-lang-$srcGitRev_5.tar.gz"
SOURCE_DIR_5="ppsspp-lang-$srcGitRev_5"
PATCHES="ppsspp-$portVersion.patchset"
ADDITIONAL_FILES="ppsspp.rdef.in"

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

PROVIDES="
	ppsspp$secondaryArchSuffix = $portVersion
	app:PPSSPP$secondaryArchSuffix
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavformat$secondaryArchSuffix
	lib:libavutil$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libglew$secondaryArchSuffix
	lib:libGLU$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libminiupnpc$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libSDL2_2.0$secondaryArchSuffix
	lib:libSDL2_image_2.0$secondaryArchSuffix
	lib:libsnappy$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	lib:libzip$secondaryArchSuffix
	lib:libzstd$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libavcodec$secondaryArchSuffix
	devel:libavformat$secondaryArchSuffix
	devel:libavutil$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libglew$secondaryArchSuffix >= 2.2
	devel:libGLU$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libminiupnpc$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libSDL2_2.0$secondaryArchSuffix
	devel:libSDL2_image_2.0$secondaryArchSuffix
	devel:libsnappy$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	devel:libzip$secondaryArchSuffix
	devel:libzstd$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:python3
	cmd:pkg_config$secondaryArchSuffix
	"

BUILD()
{
	rm -rf ext/{armips,glslang,SPIRV-Cross}
	ln -s $sourceDir2 ext/armips
	ln -s $sourceDir3 ext/glslang
	ln -s $sourceDir4 ext/SPIRV-Cross
	cp -rf $sourceDir5/*.ini assets/lang
	sed -i "s|unknown| v$portVersion|" git-version.cmake

	mkdir -p build
	cd build

	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DUSE_SYSTEM_FFMPEG=ON \
		-DUSE_SYSTEM_SNAPPY=ON \
		-DUSE_SYSTEM_LIBZIP=ON \
		-DUSE_MINIUPNPC=ON \
		-DUSE_DISCORD=OFF \
		-DCMAKE_C_FLAGS="-D_BSD_SOURCE" \
		-DCMAKE_CXX_FLAGS="-D_BSD_SOURCE"
	make $jobArgs
}

INSTALL()
{
	mkdir -p $appsDir/PPSSPP
	cp -f build/PPSSPPSDL $appsDir/PPSSPP/PPSSPP
	cp -rf assets $appsDir/PPSSPP

	local APP_SIGNATURE="application/x-vnd.ppsspp"
	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/ppsspp.rdef.in > ppsspp.rdef

	addResourcesToBinaries ppsspp.rdef $appsDir/PPSSPP/PPSSPP
	mimeset -f $appsDir/PPSSPP/PPSSPP
	addAppDeskbarSymlink $appsDir/PPSSPP/PPSSPP
}
