SUMMARY="An alternative engine for Half-Life based games"
DESCRIPTION="Xash3D Engine is a custom Gold Source engine rewritten from scratch. Xash3D is \
compatible with many of the Gold Source games and mods and should be able to run almost any \
existing singleplayer Half-Life mod without a hitch. Xash3D FWGS is a fork of Xash3D Engine, \
which aims on crossplatform and compatibility with original Xash3D and Gold Source."
HOMEPAGE="https://xash.su"
COPYRIGHT="2007-2019 Uncle Mike, 2015-2021 Flying With Gauss Team"
LICENSE="GNU GPL v3"
REVISION="1"

srcGitRev="a1c948e591603012437976fe531a47d5f91fe707"
SOURCE_URI="https://github.com/FWGS/xash3d-fwgs/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="6660720c2e12911f836a63815617cf56a5fd22344505fb384fd990deca180583"
SOURCE_DIR="xash3d-fwgs-$srcGitRev"

srcGitRev2="47af806327114b777d87833232aaff050658a12c"
SOURCE_URI_2="https://github.com/FWGS/mainui_cpp/archive/$srcGitRev2.tar.gz"
CHECKSUM_SHA256_2="46e85251cd244f2711167361bb6b9a4cf82addf5d1ce71717d82c27e1d6fe433"
SOURCE_DIR_2="mainui_cpp-$srcGitRev2"

srcGitRev3="b2741298ceef8893ad75b8b96386075abdf5ca0c"
SOURCE_URI_3="https://github.com/FWGS/miniutl/archive/$srcGitRev3.tar.gz"
CHECKSUM_SHA256_3="ed950f608a6d834f29ec9553c94e16fb2e5803d82bac6ef4d705377810bab04a"
SOURCE_DIR_3="MiniUTL-$srcGitRev3"

# Half-Life
srcGitRev4="7fdc58743d1c3a4de6489232a233cf6f3a1b7a28"
SOURCE_URI_4="https://github.com/FWGS/hlsdk-xash3d/archive/$srcGitRev4.tar.gz"
CHECKSUM_SHA256_4="8dfc173899fcc4fa3449efefaf7bd91a886c7832ea05edf6c0e44d53e0afd754"
SOURCE_DIR_4="hlsdk-xash3d-$srcGitRev4"

# Blue Shift
srcGitRev5="06b530d9af95e9a1e110acb2f2429c0e80c663a8"
SOURCE_URI_5="https://github.com/FWGS/hlsdk-xash3d/archive/$srcGitRev5.tar.gz"
CHECKSUM_SHA256_5="bfcc90e00c47dcf1373879499a1e611f4f48814cabc8f895144b8e8115e82a0b"
SOURCE_DIR_5="hlsdk-xash3d-$srcGitRev5"

# Opposing Force
srcGitRev6="efa2dae5897a64d309396f9398ace9bba19547a8"
SOURCE_URI_6="https://github.com/FWGS/hlsdk-xash3d/archive/$srcGitRev6.tar.gz"
CHECKSUM_SHA256_6="6abeb50efa8d3b586f999f81e85c56426338ea348558c7e4621e5701e785b9a1"
SOURCE_DIR_6="hlsdk-xash3d-$srcGitRev6"

srcGitRev7="9a15200d91cacd3f93e55f94b36c882ea326e1ac"
SOURCE_URI_7="https://github.com/EXL/BeGameLauncher/archive/$srcGitRev7.tar.gz"
CHECKSUM_SHA256_7="5496d76e192598386bc47b4cf58cdbe039cd91f0a16117af1fe9d464bd0f636b"
SOURCE_DIR_7="BeGameLauncher-$srcGitRev7"

SOURCE_URI_8="https://github.com/FWGS/xash-extras/releases/download/v0.19.2/extras.pak#noarchive"
CHECKSUM_SHA256_8="b3783f366f785fb1ac9bdacc6c677324b0507a4d98358a3ec94af3a088e363ce"

ADDITIONAL_FILES="xash3d.rdef"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="!x86"
if [ "$targetArchitecture" = x86_64 ]; then
	archPostfix=amd64
else
	archPostfix=i386
fi

PROVIDES="
	xash3d$secondaryArchSuffix = $portVersion
	app:Xash3D
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libGL$secondaryArchSuffix
	lib:libSDL2_2.0$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libfontconfig$secondaryArchSuffix
	devel:libGL$secondaryArchSuffix
	devel:libSDL2_2.0$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:python2
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	"

BUILD()
{
	cd $sourceDir
	cp -aR $sourceDir2/* mainui/
	cp -aR $sourceDir3/* mainui/miniutl/

	./waf configure -T release
	./waf build $jobArgs

	# Half-Life
	cd $sourceDir4
	./waf configure -T release
	./waf build $jobArgs

	# Blue Shift
	cd $sourceDir5
	./waf configure -T release
	./waf build $jobArgs

	# Opposing Force
	cd $sourceDir6
	./waf configure -T release
	./waf build $jobArgs

	cd $sourceDir7
	mkdir -p build/
	cd build
	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DLAUNCHER=xash3d
	make $jobArgs
}

INSTALL()
{
	mkdir -p $appsDir/Xash3D/{engine,docs}
	mkdir -p $appsDir/Xash3D/engine/lib
	mkdir -p $appsDir/Xash3D/extras/valve/{cl_dlls,dlls}
	mkdir -p $appsDir/Xash3D/extras/bshift/{cl_dlls,dlls}
	mkdir -p $appsDir/Xash3D/extras/gearbox/{cl_dlls,dlls}

	cp $sourceDir4/build/cl_dll/libclient_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/valve/cl_dlls/client_haiku_$archPostfix.so
	cp $sourceDir4/build/dlls/libhl_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/valve/dlls/hl_haiku_$archPostfix.so

	cp $sourceDir5/build/cl_dll/libclient_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/bshift/cl_dlls/client_haiku_$archPostfix.so
	cp $sourceDir5/build/dlls/libbshift_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/bshift/dlls/bshift_haiku_$archPostfix.so

	cp $sourceDir6/build/cl_dll/libclient_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/gearbox/cl_dlls/client_haiku_$archPostfix.so
	cp $sourceDir6/build/dlls/libopfor_haiku_$archPostfix.so \
	   $appsDir/Xash3D/extras/gearbox/dlls/opfor_haiku_$archPostfix.so

	cp $sourceDir8/extras.pak $appsDir/Xash3D/extras/valve/

	cp $sourceDir/build/mainui/libmenu.so $appsDir/Xash3D/engine/lib/
	cp $sourceDir/build/engine/libxash.so $appsDir/Xash3D/engine/lib/
	cp $sourceDir/build/ref_gl/libref_gl.so $appsDir/Xash3D/engine/lib/
	cp $sourceDir/build/ref_soft/libref_soft.so $appsDir/Xash3D/engine/lib/
	cp $sourceDir/build/game_launch/xash3d $appsDir/Xash3D/engine/Xash3D
	cp $sourceDir/README.md $appsDir/Xash3D/docs/

	cp $sourceDir7/build/Xash3DLauncher $appsDir/Xash3D/Xash3D

	settype -t application/x-vnd.Be-elfexecutable $appsDir/Xash3D/engine/Xash3D
	rc $portDir/additional-files/xash3d.rdef -o xash3d.rsrc
	resattr -o $appsDir/Xash3D/engine/Xash3D xash3d.rsrc

	addAppDeskbarSymlink $appsDir/Xash3D/Xash3D
}
