SUMMARY="A portable Nintendo 64 emulator"
DESCRIPTION="Mupen64Plus is a cross-platform plugin-based N64 emulator which is \
capable of accurately playing many games. Included are four MIPS R4300 CPU emulators, \
with dynamic recompilers for 32-bit x86 and 64-bit amd64 systems, and necessary plugins \
for audio, graphical rendering (RDP), signal co-processor (RSP), and input. \
There is 1 included OpenGL video plugin, called RiceVideo. There are 3 other excellent \
video plugins being maintained by wahrhaft, called Arachnoid, Glide64, and Z64."
HOMEPAGE="https://mupen64plus.org/"
COPYRIGHT="Mupen64Plus Team, Dan Hasting"
LICENSE="GNU GPL v2
	GNU LGPL v2.1
	BSD (3-clause)
	"
REVISION="2"
SOURCE_URI="https://github.com/mupen64plus/mupen64plus-core/releases/download/$portVersion/mupen64plus-bundle-src-$portVersion.tar.gz"
CHECKSUM_SHA256="d5243ddc00388ee2e538b3826a78a719dec2bd5da54ac6f3344fed861fb141a8"
SOURCE_DIR="mupen64plus-bundle-src-$portVersion"
guiVersion="1.14"
SOURCE_URI_2="https://github.com/dh4/mupen64plus-qt/archive/$guiVersion.tar.gz"
CHECKSUM_SHA256_2="467f4e5b75a0f63f23b1c8266746b3e7932b9dc4ee950c42b2bfaf9660e64614"
SOURCE_DIR_2="mupen64plus-qt-$guiVersion"
PATCHES="mupen64plus-$portVersion.patchset"
PATCHES_2="mupen64plus_qt-$guiVersion.patchset"
ADDITIONAL_FILES="mupen64plus.rdef.in"

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

M64P_COMPONENTS="core ui-console audio-sdl input-sdl rsp-hle video-rice video-glide64mk2"

PROVIDES="
	mupen64plus$secondaryArchSuffix = $portVersion
	app:mupen64plus = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libboost_filesystem$secondaryArchSuffix
	lib:libboost_system$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libglew$secondaryArchSuffix
	lib:libGLU$secondaryArchSuffix
	lib:libintl$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libQt5Core$secondaryArchSuffix
	lib:libQt5Gui$secondaryArchSuffix
	lib:libQt5Widgets$secondaryArchSuffix
	lib:libquazip5$secondaryArchSuffix
	lib:libSDL2_2.0$secondaryArchSuffix
	lib:libSDL2_image_2.0$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	lib:libzip$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libboost_filesystem$secondaryArchSuffix
	devel:libboost_system$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libglew$secondaryArchSuffix
	devel:libGLU$secondaryArchSuffix
	devel:libintl$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libQt5Core$secondaryArchSuffix
	devel:libquazip5$secondaryArchSuffix
	devel:libSDL2_2.0$secondaryArchSuffix
	devel:libSDL2_image_2.0$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	devel:libzip$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:awk
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:lrelease$secondaryArchSuffix >= 5
	cmd:make
	cmd:nasm
	cmd:pkg_config$secondaryArchSuffix
	cmd:qmake$secondaryArchSuffix >= 5
	cmd:which
	"

BUILD()
{
	APP_DIR="$appsDir/Mupen64Plus/"
	PARAMS="\
		PLUGINDIR=$APP_DIR/plugins \
		SHAREDIR=$APP_DIR/data \
		BINDIR=$APP_DIR/bin \
		MANDIR=$manDir \
		LIBDIR=$APP_DIR/lib \
		APPSDIR=$APP_DIR \
		ICONSDIR=$APP_DIR/icons \
		INCDIR=$APP_DIR/api \
		LDCONFIG=true "

	for component in ${M64P_COMPONENTS}; do
		make -C source/mupen64plus-${component}/projects/unix all $@ ${PARAMS}
	done

	mkdir -p $sourceDir2/build
	cd $sourceDir2/build

	qmake ../mupen64plus-qt.pro
	make
}

INSTALL()
{
	APP_DIR="$appsDir/Mupen64Plus/"
	mkdir -p $APP_DIR

	PARAMS="\
		PLUGINDIR=$APP_DIR/plugins \
		SHAREDIR=$APP_DIR/data \
		BINDIR=$APP_DIR/bin \
		MANDIR=$manDir \
		LIBDIR=$APP_DIR/lib \
		APPSDIR=$APP_DIR \
		ICONSDIR=$APP_DIR/icons \
		INCDIR=$APP_DIR/api \
		LDCONFIG=true "

	for component in ${M64P_COMPONENTS}; do
		make -C source/mupen64plus-${component}/projects/unix install $@ ${PARAMS}
	done

	mv  $APP_DIR/bin/mupen64plus  $APP_DIR/bin/Mupen64Plus
	cp source/mupen64plus-rom/m64p_test_rom.v64 $APP_DIR/data
	cp $sourceDir2/build/mupen64plus-qt $APP_DIR/Mupen64Plus
	rm -rf $APP_DIR/{mupen64plus.desktop,api,icons} $manDir

	local APP_SIGNATURE="application/x-vnd.mupen64plus"
	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/mupen64plus.rdef.in > mupen64plus.rdef
	addResourcesToBinaries mupen64plus.rdef $APP_DIR/bin/Mupen64Plus

	local APP_SIGNATURE="application/x-vnd.mupen64plus-qt"
	local MAJOR="`echo "$guiVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$guiVersion" | cut -d. -f2`"
	local MINOR="0"
	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/mupen64plus.rdef.in > mupen64plus-qt.rdef
	addResourcesToBinaries mupen64plus-qt.rdef $APP_DIR/Mupen64Plus

	addAppDeskbarSymlink $APP_DIR/Mupen64Plus
}
