# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>
# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
pkgname=dotnet8-sdk
pkgver=8.0.108
_bldver=8.0.8-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 builds are not yet stable on musl
# loongarch64: blocked by dotnet8-runtime
arch="all !x86 !armhf !riscv64 !s390x !ppc64le !loongarch64"
url=https://dotnet.microsoft.com
license="MIT"
makedepends="dotnet$_pkgver_name-runtime-bootstrap=$_bldver"
subpackages="
	dotnet$_pkgver_name-templates:templates:noarch
	dotnet$_pkgver_name-sdk-dbg:dbg: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/packs

	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/sdk "$pkgdir"/$_libdir/dotnet/.
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/sdk-manifests "$pkgdir"/$_libdir/dotnet/.
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/Microsoft.NETCore.App.Runtime.* "$pkgdir"/$_libdir/dotnet/packs/.
	cp -r $_libdir/dotnet/bootstrap/$_bldver_ver/packs/Microsoft.AspNetCore.App.Runtime.* "$pkgdir"/$_libdir/dotnet/packs/.

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

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

dbg() {
	pkgdesc="The .NET $_pkgver_macro SDK - debug pdb files"
	depends="dotnet$_pkgver_name-sdk"
	provides="dotnet-sdk-dbg-$_pkgver_macro=$pkgver-r$pkgrel"

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

	# move pdb files
	cd "$pkgdir" || return 0
	find . -type f ! -name "$(printf "*\n*")" -name '*.pdb' > tmp
	while IFS= read -r i; do
		d="$subpkgdir/${i%/*}" # dirname $i
		mkdir -p "$d"
		mv "$pkgdir/$i" "$d"
		rmdir "$pkgdir/${i%/*}" 2>/dev/null || :
	done <  tmp
	rm tmp
}

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/

	# remove pdb files
	find "$subpkgdir" -type f -name '*.pdb' -delete
}

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/.

	# remove pdb files
	find "$subpkgdir" -type f -name '*.pdb' -delete
}

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/.
}