# Template file for 'kodi'
pkgname=kodi
version=21.1
revision=2
_codename="Omega"
_crossguid_ver="ca1bf4b810e2d188d04cb6286f957008ee1b7681"
_dvdcss_ver="1.4.3-Next-Nexus-Alpha2-2"
_dvdread_ver="6.1.3-Next-Nexus-Alpha2-2"
_dvdnav_ver="6.1.1-Next-Nexus-Alpha2-2"
_udfread_ver="1.1.2"
build_style=cmake
# XXX: some vendored libraries aren't found if CMAKE_BUILD_TYPE=None
# because there is special handling for =Release
configure_args="-DWITH_FFMPEG=/usr -DENABLE_LDGOLD=OFF -DAPP_RENDER_SYSTEM=gl -DCMAKE_BUILD_TYPE=Release
 -DENABLE_LDGOLD=OFF -DENABLE_AIRTUNES=ON -DENABLE_AVAHI=ON -DENABLE_BLURAY=ON -DENABLE_CEC=ON
 -DENABLE_DBUS=ON -DENABLE_DVDCSS=ON -DENABLE_EGL=ON -DENABLE_EVENTCLIENTS=ON -DENABLE_MICROHTTPD=ON
 -DENABLE_MYSQLCLIENT=ON -DENABLE_NFS=ON -DENABLE_OPTICAL=ON -DENABLE_SMBCLIENT=ON -DENABLE_UDEV=ON
 -DENABLE_UPNP=ON -DENABLE_VAAPI=ON -DENABLE_VDPAU=ON -DENABLE_XSLT=ON -DENABLE_LIRCCLIENT=OFF
 -DENABLE_INTERNAL_RapidJSON=OFF -DENABLE_INTERNAL_CROSSGUID=ON -DENABLE_INTERNAL_FSTRCMP=OFF
 -DENABLE_INTERNAL_FLATBUFFERS=OFF -DENABLE_INTERNAL_UDFREAD=ON
 -DCROSSGUID_URL=${XBPS_SRCDISTDIR}/${pkgname}-${version}/crossguid-${_crossguid_ver}.tar.gz
 -Dlibdvdcss_URL=${XBPS_SRCDISTDIR}/${pkgname}-${version}/libdvdcss-${_dvdcss_ver}.tar.gz
 -Dlibdvdnav_URL=${XBPS_SRCDISTDIR}/${pkgname}-${version}/libdvdnav-${_dvdnav_ver}.tar.gz
 -Dlibdvdread_URL=${XBPS_SRCDISTDIR}/${pkgname}-${version}/libdvdread-${_dvdread_ver}.tar.gz
 -DUDFREAD_URL=${XBPS_SRCDISTDIR}/${pkgname}-${version}/libudfread-${_udfread_ver}.tar.gz"
make_build_target="all"
hostmakedepends="automake libtool pkg-config gperf cmake gettext zip unzip nasm yasm python3-devel
 gettext-devel libltdl-devel curl flatbuffers swig openjdk11 which wayland-scanner++ cross-aarch64-linux-gnu cross-aarch64-linux-gnu-libc"
makedepends="eudev-libudev-devel pcre-devel expat-devel libpng-devel libjpeg-turbo-devel
 libXrandr-devel avahi-libs-devel pulseaudio-devel samba-devel tiff-devel
 libmariadbclient-devel libmpeg2-devel wavpack-devel zlib-devel lzo-devel
 libXmu-devel glew-devel fribidi-devel sqlite-devel libcdio-devel freetype-devel
 jasper-devel faac-devel SDL2_image-devel faad2-devel libmodplug-devel openssl-devel
 libmad-devel fontconfig-devel libXinerama-devel libsamplerate-devel libmms-devel
 enca-devel boost-devel libcurl-devel libva-devel libvdpau-devel libass-devel
 libbluetooth-devel yajl-devel libplist-devel librtmp-devel libdisplay-info-devel
 tinyxml-devel tinyxml2-devel taglib-devel libcap-devel lame-devel libbluray-devel
 libnfs-devel ffmpeg6-devel giflib-devel libxslt-devel gnutls-devel libssh-devel
 libmicrohttpd-devel libcec-devel dcadec-devel flatbuffers-devel fmt-devel lcms2-devel
 libfstrcmp-devel rapidjson libcdio-paranoia spdlog libwaylandpp-devel libinput-devel
 libdav1d-devel gtest-devel python3-devel"
# The following dependencies are dlopen(3)ed.
depends="libbluray libmad libvorbis libcurl libflac libmodplug libass libmpeg2
 lame librtmp libnfs>=1.9.7 libplist>=1.12 glxinfo hicolor-icon-theme
 desktop-file-utils xdpyinfo"
short_desc="Software media player and entertainment hub for digital media"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-or-later"
homepage="http://www.kodi.tv"
distfiles="https://github.com/xbmc/xbmc/archive/${version}-${_codename}.tar.gz
 http://mirrors.kodi.tv/build-deps/sources/crossguid-${_crossguid_ver}.tar.gz
 https://github.com/xbmc/libdvdcss/archive/${_dvdcss_ver}.tar.gz>libdvdcss-${_dvdcss_ver}.tar.gz
 https://github.com/xbmc/libdvdread/archive/${_dvdread_ver}.tar.gz>libdvdread-${_dvdread_ver}.tar.gz
 https://github.com/xbmc/libdvdnav/archive/${_dvdnav_ver}.tar.gz>libdvdnav-${_dvdnav_ver}.tar.gz
 https://mirrors.kodi.tv/build-deps/sources/libudfread-${_udfread_ver}.tar.gz"
checksum="ad6e40b0912c8318635f0501dc1f7f27ce3a29e671f2ddb608cad34babae80ef
 6be27e0b3a4907f0cd3cfadec255ee1b925569e1bd06e67a4d2f4267299b69c4
 f38c4a4e7a4f4da6d8e83b8852489aa3bb6588a915dc41f5ee89d9aad305a06e
 719130091e3adc9725ba72df808f24a14737a009dca5a4c38c601c0c76449b62
 584f62a3896794408d46368e2ecf2c6217ab9c676ce85921b2d68b8961f49dfc
 2bf16726ac98d093156195bb049a663e07d3323e079c26912546f4e05c77bac5"
skip_extraction="crossguid-${_crossguid_ver}.tar.gz
 libdvdcss-${_dvdcss_ver}.tar.gz
 libdvdread-${_dvdread_ver}.tar.gz
 libdvdnav-${_dvdnav_ver}.tar.gz
 libudfread-${_udfread_ver}.tar.gz"
lib32disabled=yes
python_version=3
LDFLAGS="-Wl,-z,stack-size=1048576"

if [ "$XBPS_WORDSIZE" = 32 ]; then
	LDFLAGS+=" -Wl,--no-keep-memory"
fi

# Disable this test on all architectures
# https://github.com/void-linux/void-packages/commit/3c0332ab330e30a2c280de2258ed9bf60a9c2e09/checks/2101961295/logs
_gtest_filter="-TestCPUInfo.GetCPUFrequency"
_gtest_filter+=":TestWebServer.CanGetCachedFileWithOlderIfModifiedSince"
_gtest_filter+=":TestWebServer.CanGetCachedFileWithNewerIfModifiedSinceForcingNoCache"
_gtest_filter+=":TestWebServer.CanGetCachedFileWithExactIfUnmodifiedSince"
_gtest_filter+=":TestWebServer.CanGetCachedFileWithNewerIfUnmodifiedSince"
_gtest_filter+=":TestWebServer.CanGetRangedFileRange0_First"
_gtest_filter+=":TestWebServer.CanGetRangedFileRangeFirst_Second"
_gtest_filter+=":TestWebServer.CanGetRangedFileRange_Last"
_gtest_filter+=":TestWebServer.CanGetRangedFileRangeFirstSecond"
_gtest_filter+=":TestWebServer.CanGetRangedFileRangeFirstSecondLast"

case "$XBPS_TARGET_MACHINE" in
	i686)
		# Additionaly disable these tests on i686 architecutre
		# https://github.com/void-linux/void-packages/commit/3c0332ab330e30a2c280de2258ed9bf60a9c2e09/checks/2101961301/logs
		_gtest_filter+=":TestDateTime.SetFromDBTime"
		_gtest_filter+=":TestDateTime.SetDateTime"
		_gtest_filter+=":TestDateTime.Reset"
		;;
esac

if [ -z "$CROSS_BUILD" ]; then
	make_build_target+=" kodi-test"
else
	hostmakedepends+=" libmariadbclient-devel SDL2_image-devel lzo-devel
	 libwaylandpp-devel kodi"
	configure_args+=" -DWITH_TEXTUREPACKER=/usr/bin/TexturePacker"
fi

pre_configure() {
	if [ "$CROSS_BUILD" ]; then
		find -type f | \
		grep "configure\.ac\|Makefile\|cmake\|config\.site" | \
		xargs sed -i  -e "s;-isystem;-I;g"
	fi

	. /etc/profile.d/jdk.sh

	# Build tools needed to run on the host
	if [ "$CROSS_BUILD" ]; then
		cd tools/depends && ./bootstrap
		vsed -e "s|uclibc|musl|g" -i configure
		CC= LD= CXX= LDFLAGS= CFLAGS= CXXFLAGS= ./configure --exec_prefix=/usr \
			--prefix=/usr --with-toolchain=/usr --host=${XBPS_TRIPLET} \
			--build=${XBPS_TRIPLET} --target=${XBPS_TRIPLET} --with-rendersystem=gl
		cd ../..
		for i in JsonSchemaBuilder; do
			msg_normal "Building native tool $i"
			cd tools/depends/native/$i/src
			./autogen.sh
			CC= LD= CXX= LDFLAGS= CFLAGS= CXXFLAGS= \
				./configure --host=${XBPS_TRIPLET} --build=${XBPS_TRIPLET} \
				--target=${XBPS_TRIPLET}
			CC= LD= CXX= LDFLAGS= CFLAGS= CXXFLAGS= \
				make $makejobs
			cd ${wrksrc}
			cp -r tools/depends/native/$i/src/$i tools/$i
			make -C tools/depends/native/$i distclean
		done
		configure_args+=" -DWITH_JSONSCHEMABUILDER:PATH=$wrksrc/tools/JsonSchemaBuilder"
		configure_args+=" -DXBPS_TRIPLET=${XBPS_TRIPLET} -DXBPS_CROSS_TRIPLET=${XBPS_CROSS_TRIPLET}"
	fi
}

post_install() {
	mkdir -p "${DESTDIR}/${py3_sitelib}"
	mv "${DESTDIR}/kodi" "${DESTDIR}/${py3_sitelib}"
}

do_check() {
	./build/kodi-test --gtest_filter="${_gtest_filter}"
}

kodi-devel_package() {
	short_desc+=" - development files"
	depends="${sourcepkg}>=${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove usr/share/kodi/cmake
	}
}

kodi-rpi_package() {
	build_style=meta
	short_desc+=" (transitional dummy package)"
	depends="${sourcepkg}>=${version}_${revision}"
}
