# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>

pkgname=dotnet8-runtime
pkgver=8.0.1_pre1
_bldver=8.0.100_pre1-r0
pkgrel=0

# Following for dotnet build version 6.0 and up
_pkgver_macro=${pkgver%.*}
_pkgver_name=${_pkgver_macro//[.0]}
_bldver_ver=${_bldver%%-*}
pkgdesc="The .NET $_pkgver_macro Core runtime"
# x86: blocked by https://github.com/dotnet/runtime/issues/77667
# armhf: blocked by https://github.com/dotnet/runtime/issues/77663
# riscv64: port WIP https://github.com/dotnet/runtime/issues/36748
# s390x / ppc64le: no bootstrap crossbuilt yet
arch="all !x86 !armhf !riscv64 !s390x !ppc64le"
url=https://dotnet.microsoft.com
license="MIT"
depends="
	dotnet$_pkgver_name-hostfxr
	icu-data-full
	icu-libs
	"
makedepends="dotnet$_pkgver_name-build=$_bldver"
subpackages="
	aspnetcore$_pkgver_name-runtime:aspnetcore_runtime:noarch
	aspnetcore$_pkgver_name-targeting-pack:aspnetcore_targeting_pack:noarch
	dotnet$_pkgver_name-apphost-pack:apphost_pack
	dotnet$_pkgver_name-hostfxr
	dotnet$_pkgver_name-targeting-pack:targeting_pack:noarch
	dotnet-host:host
	"
provides="dotnet$_pkgver_name=$pkgver-r$pkgrel"
options="!check" # No test suite
source="dotnet.sh.in"
builddir="$srcdir"
_libdir="/usr/lib"

case $CARCH in
	x86_64) _dotnet_arch="x64";;
	aarch64) _dotnet_arch="arm64";;
	armv7) _dotnet_arch="arm";;
	armhf) _dotnet_arch="armv6";;
	*) _dotnet_arch="$CARCH";;
esac

build() {
	sed "s|%LIBDIR%|$_libdir/dotnet|" "$srcdir"/dotnet.sh.in > "$srcdir"/dotnet.sh
}

package() {
	install -dm 755 "$pkgdir"/$_libdir/dotnet/shared

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/shared/Microsoft.NETCore.App "$pkgdir"/$_libdir/dotnet/shared/.
}

host() {
	pkgdesc="A generic driver for the .NET Core Command Line Interface"
	depends=""

	install -dm 755 \
		"$subpkgdir"/etc/profile.d \
		"$subpkgdir"/etc/dotnet \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/$_libdir/dotnet

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/dotnet "$subpkgdir"/$_libdir/dotnet/.
	ln -s $_libdir/dotnet/dotnet "$subpkgdir"/usr/bin/dotnet
	echo "$_libdir/dotnet" > "$subpkgdir"/etc/dotnet/install_location
	echo "$_libdir/dotnet" > "$subpkgdir"/etc/dotnet/install_location_$_dotnet_arch
	install -Dm 644 "$srcdir"/dotnet.sh -t "$subpkgdir"/etc/profile.d/
}

hostfxr() {
	pkgdesc="The .NET host resolver contains the logic to resolve and select the right version of the .NET SDK or runtime to use."
	depends="dotnet-host"
	provides="dotnet-hostfxr-$_pkgver_macro=$pkgver-r$pkgrel"

	install -dm 755	"$subpkgdir"/$_libdir/dotnet

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/host "$subpkgdir"/$_libdir/dotnet/.
}

aspnetcore_runtime() {
	pkgdesc="The ASP.NET $_pkgver_macro Core runtime"
	depends="dotnet$_pkgver_name-runtime"
	provides="aspnetcore-runtime-$_pkgver_macro=$pkgver-r$pkgrel"

	install -dm 755 "$subpkgdir"/$_libdir/dotnet/shared

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/shared/Microsoft.AspNetCore.App "$subpkgdir"/$_libdir/dotnet/shared/.
}

apphost_pack() {
	pkgdesc="The .NET $_pkgver_macro Core apphost pack"
	provides="dotnet-apphost-pack-$_pkgver_macro=$pkgver-r$pkgrel"

	install -dm 755 "$subpkgdir"/$_libdir/dotnet/packs

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/Microsoft.NETCore.App.Host.* "$subpkgdir"/$_libdir/dotnet/packs/.
}

targeting_pack() {
	pkgdesc="The .NET $_pkgver_macro Core targeting pack"
	provides="dotnet-targeting-pack-$_pkgver_macro=$pkgver-r$pkgrel"

	install -dm 755 "$subpkgdir"/$_libdir/dotnet/packs

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/Microsoft.NETCore.App.Ref "$subpkgdir"/$_libdir/dotnet/packs/.
}

aspnetcore_targeting_pack() {
	pkgdesc="The ASP.NET $_pkgver_macro Core targeting pack"
	provides="aspnetcore-targeting-pack-$_pkgver_macro=$pkgver-r$pkgrel"

	install -dm 755	"$subpkgdir"/$_libdir/dotnet/packs

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/Microsoft.AspNetCore.App.Ref "$subpkgdir"/$_libdir/dotnet/packs/.
}

sha512sums="
c3f31956976b77198e72a3fef3427338b6b2961f8c121416040c1105de0ce8073e46f4c2e9ef22a001aee69cbe39621c2ddac988522693110071dfae42f2e2b7  dotnet.sh.in
"