SUMMARY="A multi-purpose emulation framework"
DESCRIPTION="MAME\'s purpose is to preserve decades of software history. As \
electronic technology continues to rush forward, MAME prevents this important \
\"vintage\" software from being lost and forgotten. This is achieved by \
documenting the hardware and how it functions. The source code to MAME \
serves as this documentation. The fact that the software is usable serves \
primarily to validate the accuracy of the documentation (how else can you \
prove that you have recreated the hardware faithfully?). Over time, MAME \
(originally stood for Multiple Arcade Machine Emulator) absorbed the \
sister-project MESS (Multi Emulator Super System), so MAME now documents a \
wide variety of (mostly vintage) computers, video game consoles and \
calculators, in addition to the arcade video games that were its initial focus."
HOMEPAGE="https://www.mamedev.org/"
COPYRIGHT="1997-2020 MAMEDev and contributors"
LICENSE="GNU GPL v2"
REVISION="1"
SOURCE_URI="https://github.com/mamedev/mame/archive/mame${portVersion/./}.tar.gz"
CHECKSUM_SHA256="7c4c9ec232ba988e65fd29665c9b8e40b5ac3aa9f561eeb107cebbf08ba94baf"
SOURCE_FILENAME="mame-$portVersion.tar.gz"
SOURCE_DIR="mame-mame${portVersion/./}"
PATCHES="mame-$portVersion.patchset"
ADDITIONAL_FILES="
	mame.sh
	mame.rdef.in
	"

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

bits=64
optimize=3
makejobs=$jobArgs
commandBinDir=$binDir
commandSuffix=$secondaryArchSuffix
if [ "$targetArchitecture" = x86_gcc2 ]; then
	bits=
	commandBinDir=$prefix/bin
	commandSuffix=
	makejobs=1
	optimize=1
fi

PROVIDES="
	mame$secondaryArchSuffix = $portVersion
	cmd:castool$commandSuffix
	cmd:chdman$commandSuffix
	cmd:floptool$commandSuffix
	cmd:imgtool$commandSuffix
	cmd:jedutil$commandSuffix
	cmd:ldresample$commandSuffix
	cmd:ldverify$commandSuffix
	cmd:mame.sh
	cmd:mame$commandSuffix
	cmd:nltool$commandSuffix
	cmd:nlwav$commandSuffix
	cmd:pngcmp$commandSuffix
	cmd:regrep$commandSuffix
	cmd:romcmp$commandSuffix
	cmd:split$commandSuffix
	cmd:src2html$commandSuffix
	cmd:srcclean$commandSuffix
	cmd:testkeys$commandSuffix
	cmd:unidasm$commandSuffix
"

REQUIRES="
	haiku$secondaryArchSuffix
	lib:libexpat$secondaryArchSuffix
	lib:libflac$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:liblua$secondaryArchSuffix >= 5.3
	lib:libpugixml$secondaryArchSuffix
	lib:libSDL2_2.0$secondaryArchSuffix
	lib:libSDL2_ttf$secondaryArchSuffix
	lib:libsqlite3$secondaryArchSuffix
	lib:libutf8proc$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:glm
	devel:libexpat$secondaryArchSuffix
	devel:libflac$secondaryArchSuffix
	devel:libfontconfig$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:liblua$secondaryArchSuffix >= 5.3
	devel:libogg$secondaryArchSuffix
	devel:libpugixml$secondaryArchSuffix
	devel:librapidjson$secondaryArchSuffix
	devel:libSDL2_2.0$secondaryArchSuffix
	devel:libSDL2_ttf$secondaryArchSuffix
	devel:libsqlite3$secondaryArchSuffix
	devel:libutf8proc$secondaryArchSuffix >= 2
	devel:libz$secondaryArchSuffix
"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:libtoolize
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python2
	cmd:sed
	cmd:strip$secondaryArchSuffix
"

defineDebugInfoPackage mame$secondaryArchSuffix \
	$commandBinDir/castool \
	$commandBinDir/chdman \
	$commandBinDir/floptool \
	$commandBinDir/imgtool \
	$commandBinDir/jedutil \
	$commandBinDir/ldresample \
	$commandBinDir/ldverify \
	$commandBinDir/mame \
	$commandBinDir/nltool \
	$commandBinDir/nlwav \
	$commandBinDir/pngcmp \
	$commandBinDir/regrep \
	$commandBinDir/romcmp \
	$commandBinDir/split

BUILD()
{
#	make clean

	# This is wrong on so many levels
	SYS_LDFLAGS="-lbsd -lnetwork " # trailing space required!
	for _i in flac libutf8proc lua RapidJSON sqlite3; do
		SYS_CFLAGS+="`pkg-config $_i --cflags` "
		SYS_LDFLAGS+="`pkg-config $_i --libs` "
	done

	ARCHOPTS="$SYS_CFLAGS $SYS_LDFLAGS" \
	make \
		REGENIE=1 \
		TOOLS=1 \
		PRECOMPILE=0 \
		NOWERROR=1 \
		OPTIMIZE=$optimize \
		USE_SYSTEM_LIB_EXPAT=1 \
		USE_SYSTEM_LIB_ZLIB=1 \
		USE_SYSTEM_LIB_JPEG=1 \
		USE_SYSTEM_LIB_FLAC=1 \
		USE_SYSTEM_LIB_LUA=1 \
		USE_SYSTEM_LIB_SQLITE3=1 \
		USE_SYSTEM_LIB_UTF8PROC=1 \
		USE_SYSTEM_LIB_GLM=1 \
		USE_SYSTEM_LIB_RAPIDJSON=1 \
		USE_SYSTEM_LIB_PUGIXML=1 \
		-j $makejobs
}

INSTALL()
{
	# Based on https://projects.archlinux.de/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/mame

	# Arch cleanup
	# 32 bit main binary called as "mame" which would collide with our symlink
	if [ "$targetArchitecture" = x86_64 ]; then
		mv mame$bits mame
	fi

	# Install the binaries
	mkdir -p "$commandBinDir"
	for _i in castool chdman floptool imgtool jedutil ldresample ldverify \
	mame nltool nlwav pngcmp regrep romcmp split; do
		install -m755 $_i -t "$commandBinDir"
		strip "$commandBinDir"/$_i
	done

	# Create the startscript
	cp $portDir/additional-files/mame.sh "$commandBinDir"/MAME
	chmod +x "$commandBinDir"/MAME

	# Add extra attributes to the binaries...
	local APP_SIGNATURE="application/x-vnd.mame"
	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | 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|@SUMMARY@|Multiple Arcade Machine Emulator|" \
		-e "s|@DESCRIPTION@|$SUMMARY|" \
		$portDir/additional-files/mame.rdef.in > mame.rdef

	addResourcesToBinaries mame.rdef "$commandBinDir/mame"

	# ...and to our startscript, but we can't use addResourcesToBinaries here
	rc mame.rdef
	resattr -o "$commandBinDir"/MAME mame.rsrc

	# Creating icon in Haiku's leaf-menu
	addAppDeskbarSymlink "$commandBinDir"/MAME

	# Install the extra bits
	install -Dm644 src/osd/modules/opengl/shader/glsl*.*h -t "$libDir/"/mame/shader/
	cp -ar {artwork,bgfx,plugins,language,ctrlr,keymaps,hash} "$libDir"/mame/

	# FS#28203 fix from AUR
	sed -i 's|KEYCODE_2_PAD|KEYCODE_2PAD|' "$libDir"/mame/ctrlr/*.cfg
	sed -i 's|KEYCODE_4_PAD|KEYCODE_4PAD|' "$libDir"/mame/ctrlr/*.cfg
	sed -i 's|KEYCODE_6_PAD|KEYCODE_6PAD|' "$libDir"/mame/ctrlr/*.cfg
	sed -i 's|KEYCODE_8_PAD|KEYCODE_8PAD|' "$libDir"/mame/ctrlr/*.cfg

	# Install man-pages
	install -dm0755 "$manDir"/man{1,6}
	install -m644 docs/man/*.1* "$manDir"/man1
	install -m644 docs/man/*.6* "$manDir"/man6
}

TEST()
{
	make tests
}
