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

pkgname=dotnet8-sdk
pkgver=8.0.100
_bldver=8.0.0-r0
pkgrel=0

# Following for dotnet build version 6.0 and up
_pkgver_macro=${pkgver%.*}
_pkgver_name=${_pkgver_macro//[.0]}
_bldver_ver=${_bldver%%-*}
_bldver_ver=${_bldver_ver/0./0.10}
pkgdesc="The .NET $_pkgver_macro SDK"
# x86: blocked by https://github.com/dotnet/runtime/issues/77667
# armhf: blocked by https://github.com/dotnet/runtime/issues/77663
# riscv64: blocked by https://github.com/dotnet/runtime/issues/84834
# s390x | ppc64le: mono-based runtime still has issues on musl
arch="all !x86 !armhf !riscv64 !s390x !ppc64le"
url=https://dotnet.microsoft.com
license="MIT"
makedepends="dotnet$_pkgver_name-runtime-bootstrap=$_bldver"
subpackages="
	dotnet$_pkgver_name-templates:templates:noarch
	dotnet-zsh-completion:zshcomp:noarch
	dotnet-bash-completion:bashcomp:noarch
	dotnet-doc
	netstandard21-targeting-pack:netstandard_targeting_pack:noarch
	"
provides="dotnet$_pkgver_name=$pkgver-r$pkgrel"
options="!check" # No test suite
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

package() {
	# libucontext isn't automatically detected on all platforms
	depends="
		aspnetcore$_pkgver_name-runtime
		aspnetcore$_pkgver_name-targeting-pack
		dotnet$_pkgver_name-apphost-pack
		dotnet$_pkgver_name-targeting-pack
		dotnet$_pkgver_name-templates
		netstandard21-targeting-pack
		libucontext
		"
	provides="
		dotnet-sdk-$_pkgver_macro=$pkgver-r$pkgrel
		dotnet$_pkgver_name-dev=$pkgver-r$pkgrel
		"

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

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/sdk "$pkgdir"/$_libdir/dotnet/.
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/sdk-manifests "$pkgdir"/$_libdir/dotnet/.

	# See https://github.com/dotnet/source-build/issues/2579
	find "$pkgdir" -type f -name 'testhost.x86' -delete
	find "$pkgdir" -type f -name 'vstest.console' -delete

	# docs
	install -dm 755 "$pkgdir"/usr/share/man/man1
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/docs/* "$pkgdir"/usr/share/man/man1/.
}

netstandard_targeting_pack() {
	pkgdesc="The .NET 2.1 Standard targeting pack"
	depends="dotnet-host"
	provides="netstandard-targeting-pack-2.1=$pkgver-r$pkgrel"

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

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/NETStandard.Library.Ref "$subpkgdir"/$_libdir/dotnet/packs/
}

templates() {
	pkgdesc="The .NET $_pkgver_macro templates"
	depends="dotnet-host"
	provides="dotnet-templates-$_pkgver_macro=$pkgver-r$pkgrel"

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

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

zshcomp() {
	depends=""
	pkgdesc="zsh completion for .NET"
	# netstandard21-targeting-pack will always be pulled by any dotnetx-sdk
	# thus pulling this with it for sdk
	install_if="netstandard21-targeting-pack zsh"
	depends="dotnet-host"

	install -dm 755 "$subpkgdir"/usr/share/zsh/site-functions

	install -m 755 "$_libdir"/dotnet/bootstrap/$_bldver_ver/register-completions.zsh "$subpkgdir"/usr/share/zsh/site-functions/_dotnet
}

bashcomp() {
	depends=""
	pkgdesc="bash completion for .NET"
	# netstandard21-targeting-pack will always be pulled by any dotnetx-sdk
	# thus pulling this with it for sdk
	install_if="netstandard21-targeting-pack bash-completion"
	depends="dotnet-host"

	install -dm 755	"$subpkgdir"/usr/share/bash-completion/completions

	install -m 755 "$_libdir"/dotnet/bootstrap/$_bldver_ver/register-completions.bash "$subpkgdir"/usr/share/bash-completion/completions/_dotnet
}

doc() {
	default_doc
	pkgdesc="Docs for .NET"

	# licenses
	install -dm 755 "$subpkgdir"/usr/share/licenses/dotnet
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/LICENSE.txt "$subpkgdir"/usr/share/licenses/dotnet/.
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/ThirdPartyNotices.txt "$subpkgdir"/usr/share/licenses/dotnet/.
}
